I am having trouble with Quartz Scheduler. To be practical here you find my code of unit test:
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.getListenerManager().addSchedulerListener(schedulerTestListener);
Trigger trigger = newTrigger()
.withIdentity(CoreTestConstants.TEST_TRIGGER, CoreTestConstants.TEST_TRIGGER_GROUP)
.withSchedule(simpleSchedule()
.withMisfireHandlingInstructionIgnoreMisfires()
.withIntervalInMilliseconds(1000)
.repeatForever())
.forJob(CoreTestConstants.TEST_JOB, CoreTestConstants.TEST_JOB_GROUP)
.build();
scheduler.scheduleJob(getJobDetail(), trigger);
Thread.sleep(20000L);
scheduler.start();
The problem is, when the scheduler.scheduleJob(…) is executed I recognize that the task is not immediately started however when the scheduler.start() is executed I see that the 20 tasks is being executed immediately.
I mean, normally to my mind tasks should not be recorded or start until scheduler.start() is executed. But somehow the quartz system holds the state of tasks even before scheduler.start() is not executed, then when scheduler.start() call the missing task executions is triggered immediately.
Here the log:
09:32:45,005 INFO StdSchedulerFactory:1310 - Quartz scheduler version: 2.1.5
09:32:51,013 INFO SchedulerTest:47 - [test] Scheduler starting..
09:32:51,014 INFO QuartzScheduler:534 - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
*** [TIMER-TEST] 2012-10-10 09:32:51 / Test task is executed. Count: 1 / 1349850771037
*** [TIMER-TEST] 2012-10-10 09:32:51 / Test task is executed. Count: 2 / 1349850771040
*** [TIMER-TEST] 2012-10-10 09:32:51 / Test task is executed. Count: 3 / 1349850771040
*** [TIMER-TEST] 2012-10-10 09:32:51 / Test task is executed. Count: 4 / 1349850771041
*** [TIMER-TEST] 2012-10-10 09:32:51 / Test task is executed. Count: 5 / 1349850771042
*** [TIMER-TEST] 2012-10-10 09:32:51 / Test task is executed. Count: 6 / 1349850771043
*** [TIMER-TEST] 2012-10-10 09:32:51 / Test task is executed. Count: 7 / 1349850771044
As you can see even if I set the trigger to execute with interval in 1 seconds, it executed the missing tasks during thread sleep.
I tried to play around misfire instructions but it doesnt make a sense, it does not change the behaviour of the code in my case.
Any help would be appreciated. Thanks in advance.
OK your problem is with
Thread.sleep(20000L);while waiting actually the jobs are triggered and scheduled therefore when start is fired all scheduled jobs are starting, you can put Thread.sleep up toTrigger triggerso that it will begin triggering and scheduling after waiting. Take a look for misfire policy