How to avoid two jobs running simultaneously in Quartz?

I have a transcript below the code in which I run two jobs. First with an interval of 10 seconds, and with an interval of 3 seconds. But ultimately at some point they will be executed simultaneously. Is there any mechanism to avoid this situation.

JobDetail jDetail = new JobDetail("Job1", "group1", MyJob.class); CronTrigger crTrigger = new CronTrigger("cronTrigger", "group1", "0/10 * * * * ?"); sche.scheduleJob(jDetail, crTrigger); jDetail = new JobDetail("Job2","group2",MyJob2.class); crTrigger = new CronTrigger("cronTrigger2","group2","0/3 * * * * ?"); sche.scheduleJob(jDetail, crTrigger); 
+6
source share
4 answers

You can create a helper object to synchronize two tasks:

 //In the base class public static Object lock = new Object(); //In the first class public void execute() { synchronized(lock) { //do stuff } } //In the second class public void execute() { synchronized(lock) { //do stuff } } 

Read more about synchronization at: http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html

+2
source

Not completely answering your question, but you can request something that is done in a thread-safe way:

 //sched is your org.quartz.Scheduler synchronized (sched) { JobDetail existingJobDetail = sched.getJobDetail(jobName, jobGroup); if (existingJobDetail != null) { List<JobExecutionContext> currentlyExecutingJobs = (List<JobExecutionContext>) sched.getCurrentlyExecutingJobs(); for (JobExecutionContext jec : currentlyExecutingJobs) { if (existingJobDetail.equals(jec.getJobDetail())) { // This job is currently executing } } } 
+3
source

Configure Quartz threadpool to have only one thread.

 org.quartz.threadPool.threadCount=1 
+3
source

You tried:

 org.quartz.jobStore.isClustered: true 

Alternatively, you do your job in the task with the task (and set isClustered to true), and this will solve your problem. (Oops, StatefulJob is deprecated, use DisallowConcurrentExecution.)

0
source

Source: https://habr.com/ru/post/903582/


All Articles