Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA Quartz - Skip job if previous is still running AND wait for the next schedule time

I have a Java solution that uses Quartz 2.2.3, and what I have is:

  • My job class is annotated @DisallowConcurrentExecution to avoid concurrence, so the same job just can run once per time (OK)
  • It is a CRON and runs every 1 hour (OK)
  • The time is 1pm and the the job starts run (OK)
  • Now the time is 2pm and the previous job didn't finish yet (OK)
  • As the class is annotated the next job will not start (IT IS GREAT - OK)
  • Now the time is 2h:15min and the first job just finished (OK)
  • Now the issue, as the second job didn't start at 2pm BUT now the first just finished, the second one will start.

This is my problem, I don't want to make the second job wait if the previous didn't finish, I want to skip the second one and when the time turns 3pm the job can run. Reading javadoc I added the withMisfireHandlingInstructionDoNothing() but it didn't work.

I think I'm doing something wrong or missing something.

My code:

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

JobDetail job = JobBuilder.newJob(TestCronService.class).withIdentity("testA","testB").build();


CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("testA","testB")
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 * * * ?")
.withMisfireHandlingInstructionDoNothing())
.build();


scheduler.scheduleJob(job, trigger);
scheduler.start();
like image 970
user3046156 Avatar asked Oct 15 '16 00:10

user3046156


1 Answers

Change the function :

withMisfireHandlingInstructionDoNothing() 

with

withMisfireHandlingInstructionNextWithRemainingCount()
like image 99
nava Avatar answered Dec 05 '22 22:12

nava