boolean isDueDateInPast = true;
JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(jbpmContextName);
try {
SchedulerSession schedulerSession = jbpmContext.getSchedulerSession();
log.debug("checking for timers");
Iterator iter = schedulerSession.findTimersByDueDate();
while( (iter.hasNext())
&& (isDueDateInPast)
) {
Timer timer = (Timer) iter.next();
log.debug("found timer "+timer);
// if this timer is due
if (timer.isDue()) {
log.debug("executing timer '"+timer+"'");
// execute
timer.execute();
// save the process instance
jbpmContext.save(timer.getProcessInstance());
// notify the listeners (e.g. the scheduler servlet)
notifyListeners(timer);
// if there was an exception, just save the timer
if (timer.getException()!=null) {
schedulerSession.saveTimer(timer);
// if repeat is specified
} else if (timer.getRepeat()!=null) {
// update timer by adding the repeat duration
Date dueDate = timer.getDueDate();
// suppose that it took the timer runner thread a
// very long time to execute the timers.
// then the repeat action dueDate could already have passed.
while (dueDate.getTime()<=System.currentTimeMillis()) {
dueDate = businessCalendar
.add(dueDate,
new Duration(timer.getRepeat()));
}
timer.setDueDate( dueDate );
// save the updated timer in the database
log.debug("saving updated timer for repetition '"+timer+"' in '"+(dueDate.getTime()-System.currentTimeMillis())+"' millis");
schedulerSession.saveTimer(timer);
} else {
// delete this timer
log.debug("deleting timer '"+timer+"'");
schedulerSession.deleteTimer(timer);
}
} else { // this is the first timer that is not yet due
isDueDateInPast = false;
millisTillNextTimerIsDue = timer.getDueDate().getTime() - System.currentTimeMillis();