Quartz jobs - how to prevent multiple job instances from running?

Hi,
I have a job that is scheduled to run every 2 minutes. But sometimes it takes for the job more than two minutes to be executed due to heavy calculations that are performed. So the next job is started before the previous one is finished and it results into some conflicts.
Is it possible to run a job only if the previous instance of the job has been already finished?

Regards,
George

In another (non-jmix) project I accomplished this with essentially a static AtomicBoolean that indicates if the job class is running. I don’t know if quartz has anything built in to help.

Hi,

I think you should check @DisallowConcurrentExecution annotation:

@DisallowConcurrentExecution is an annotation that can be added to the Job class that tells Quartz not to execute multiple instances of a given job definition (that refers to the given job class) concurrently.
Notice the wording there, as it was chosen very carefully. In the example from the previous section, if “SalesReportJob” has this annotation, than only one instance of “SalesReportForJoe” can execute at a given time, but it can execute concurrently with an instance of “SalesReportForMike”. The constraint is based upon an instance definition (JobDetail), not on instances of the job class. However, it was decided (during the design of Quartz) to have the annotation carried on the class itself, because it does often make a difference to how the class is coded.

Documentation here.

3 Likes

Thank you very much. I will try this particular annotation to check it.

Regards,
George