Assume I have Cron expression like 0 0 12 1/1 * ? * Due to some reasons server got stopped between 11:50 to 12:05 and again its get started after 12:05 on this scenario how can I fire the missed scheduled Cron expression once the server get starts? Is quartz have any solution to handle the above problem.
2 Answers
See Quartz documentation tutorials or this great article for "Misfire Instructions" that are policies used from Quartz triggers to recover missed job executions.
If you want to tolerate a server shutdown/crash, you need to configure Quartz to use a persistent Job Store to like JDBCJobStore to "remember" last job execution before the crash.
Update: See this answer that explain how to create Recoverable jobs.

- 327
- 1
- 6
- 13

- 1,993
- 1
- 18
- 21
-
:Thanks for your reply.we are using quartz-1.8.6 jar when i am using the below code i think some classes are not in the jar. did i have to update the newer quartz version? trigger = newTrigger() .withIdentity("trigger3", "group1") .withSchedule(cronSchedule("0 0/2 8-17 * * ?")) .forJob("myJob", "group1") .build(); your help would be appreciated. – Loganathan Sep 19 '13 at 11:29
-
Edit your question and add some code and info so I can help you better :) – zerologiko Sep 19 '13 at 13:32
I too have faced the problem of wanting to run the last scheuled job on a server restart.
The solution I have found is to go back one time interval for the trigger and calculate what would have been the next firing time. By iterating through all the triggers the most recent time that a trigger should have fired in the past can be determined.
Calculate the interval between each firing:
Date nextFireTime = trigger.getNextFireTime();
Date subsequentFireTime = trigger.getFireTimeAfter(nextFireTime);
long interval = subsequentFireTime.getTime() - nextFireTime.getTime();
Find the next firing time for one time until interval in the past:
Date previousPeriodTime = new Date(System.currentTimeMillis() - interval);
Date previousFireTime = trigger.getFireTimeAfter(previousPeriodTime);
I have found that if you are using a CronTrigger
this prevents you asking for a fire time in the past. To work around this I modify the start time, so the above snippet becomes:
Date originalStartTime = trigger.getStartTime(); // save the start time
Date previousPeriodTime = new Date(originalStartTime.getTime() - interval);
trigger.setStartTime(previousPeriodTime);
Date previousFireTime = trigger.getFireTimeAfter(previousPeriodTime);
trigger.setStartTime(originalStartTime); // reset the start time to be nice
Iterate through all of the triggers and find the one that is most recently in the past:
for (String groupName : scheduler.getTriggerGroupNames()) {
for (String triggerName : scheduler.getTriggerNames(groupName)) {
Trigger trigger = scheduler.getTrigger(triggerName, groupName);
// code as detailed above...
interval = ...
previousFireTime = ...
}
}
This code can be refactored into helper methods or classes. I actually use the above algorithm in a subclassed delegating trigger that I then place in a set sorted by previous firing times.

- 1,721
- 1
- 19
- 29