41

I am using

SchedulerFactory schedulerFactory = new StdSchedulerFactory();
scheduler = schedulerFactory.getScheduler();
scheduler.start();
Trigger asapTrigger = getAsapTrigger();
JobDetail asapJob = getAsapJobDetails();
scheduler.scheduleJob(asapJob, asapTrigger);

This is working but when I go for cluster environment, 2 threads are running for the same job.

I am using annotations not properties file. I want to run only one thread. Can someone help on this. How to configure?

my code almost look like : http://k2java.blogspot.com/2011/04/quartz.html

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
venkat
  • 509
  • 1
  • 4
  • 5

1 Answers1

52

You have to configure Quartz to run in a clustered environment. Clustering currently only works with the JDBC jobstore, and works by having each node of the cluster to share the same database.

  • Set the org.quartz.jobStore.isClustered property to true if you have multiple instances of Quartz that use the same set of database tables. This property is used to turn on the clustering features.
  • Set the org.quartz.jobStore.clusterCheckinInterval property (milliseconds) which is the frequency at which this instance checks in with the other instances of the cluster.
  • Set the org.quartz.scheduler.instanceId to AUTO so that each node in the cluster will have a unique instanceId.

Please note that each instance in the cluster should use the same copy of the quartz.properties file. Furthermore if you use clustering on separate machines ensure that their clocks are synchronized.

For more information check the official documentation which contains a sample properties file for a clustered scheduler.

Kevin Pullin
  • 13,122
  • 3
  • 24
  • 33
Apostolos Emmanouilidis
  • 7,179
  • 1
  • 24
  • 35
  • Configuration of Quartz is done through the use of a properties file quartz.properties. You have to set the above mentioned properties in the quartz.properties file. – Apostolos Emmanouilidis Oct 21 '12 at 17:23
  • if i have multiple quartz jobs on clustered environment, do i have to define a separate quartz.properties for each one ? – Mahmoud Saleh Aug 11 '16 at 13:36
  • @Mahmoud. Your "each" is ambiguous. "for each JOB" the answer is NO. "for each quartz-instance" the answer is YES. See my next comment as well. – granadaCoder Aug 24 '17 at 13:27
  • 8
    Great answer. One clarification I believe. "same copy of the quartz.properties file". I think it would be better stated as "the contents and settings of the properties-file for each instance must have the same values" (its splitting hairs, but the phrase "same file" might throw some off).. Also, next comment will have note from documentation – granadaCoder Aug 24 '17 at 13:31
  • 1
    Here is a note from the documentation about setting: "Each instance in the cluster should use the same copy of the quartz properties. Exceptions of this would be to use properties that are identical, with the following allowable exceptions: Different thread pool size, and different value for the “quartz.scheduler.instanceId” property." source : https://www.quartz-scheduler.net/documentation/quartz-2.x/tutorial/advanced-enterprise-features.html – granadaCoder Aug 24 '17 at 13:32
  • When we use @Schedule and run some method of a service every X seconds and if deploy that code on 10 separate instances, code gets deployed 10 times, in case of quartz scheduler, all jobdetails and associated triggers are registered in scheduler. What happens when we deploy such code on 10 instances of a cluster? Does that code gets executed 10 times? Or does it execute only once and what's the reason behind that? – Akki Jul 18 '19 at 09:01