1 package org.jbpm.scheduler.impl; 2 3 import java.util.ArrayList ; 4 import java.util.Date ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.jbpm.calendar.BusinessCalendar; 11 import org.jbpm.calendar.Duration; 12 import org.jbpm.db.JbpmSession; 13 import org.jbpm.db.JbpmSessionFactory; 14 import org.jbpm.db.SchedulerSession; 15 import org.jbpm.scheduler.exe.Timer; 16 17 public class SchedulerThread extends Thread { 18 19 static JbpmSessionFactory jbpmSessionFactory = JbpmSessionFactory.getInstance(); 20 static BusinessCalendar businessCalendar = new BusinessCalendar(); 21 22 List listeners = new ArrayList (); 23 boolean keepRunning = true; 24 long interval = 5000; 25 26 public SchedulerThread() { 27 super("jbpm scheduler"); 28 } 29 30 public void run() { 31 while (keepRunning) { 32 long millisToWait = interval; 33 try { 34 millisToWait = executeTimers(); 35 36 if (millisToWait < 0) { 38 millisToWait = interval; 39 } 40 millisToWait = Math.min(millisToWait, interval); 41 42 } catch (RuntimeException e) { 43 log.info("runtime exception while executing timers", e); 44 } finally { 45 try { 46 Thread.sleep(millisToWait); 47 } catch (InterruptedException e) { 48 log.info("waiting for timers got interuppted"); 49 } 50 } 51 } 52 log.info("ending scheduler thread"); 53 } 54 55 60 public long executeTimers() { 61 long millisTillNextTimerIsDue = -1; 62 boolean isDueDateInPast = true; 63 64 JbpmSession jbpmSession = jbpmSessionFactory.openJbpmSessionAndBeginTransaction(); 65 try { 66 SchedulerSession schedulerSession = new SchedulerSession(jbpmSession); 67 68 log.debug("checking for timers"); 69 Iterator iter = schedulerSession.findTimersByDueDate(); 70 while( (iter.hasNext()) 71 && (isDueDateInPast) 72 ) { 73 Timer timer = (Timer) iter.next(); 74 log.debug("found timer "+timer); 75 76 if (timer.isDue()) { 78 log.debug("executing timer '"+timer+"'"); 79 80 timer.execute(); 82 83 notifyListeners(timer); 85 86 if (timer.getException()!=null) { 88 schedulerSession.saveTimer(timer); 89 90 } else if (timer.getRepeat()!=null) { 92 Date dueDate = timer.getDueDate(); 94 95 while (dueDate.getTime()<=System.currentTimeMillis()) { 99 dueDate = businessCalendar 100 .add(dueDate, 101 new Duration(timer.getRepeat())); 102 } 103 timer.setDueDate( dueDate ); 104 log.debug("saving updated timer for repetition '"+timer+"' in '"+(dueDate.getTime()-System.currentTimeMillis())+"' millis"); 106 schedulerSession.saveTimer(timer); 107 108 } else { 109 log.debug("deleting timer '"+timer+"'"); 111 schedulerSession.deleteTimer(timer); 112 } 113 114 } else { isDueDateInPast = false; 116 millisTillNextTimerIsDue = timer.getDueDate().getTime() - System.currentTimeMillis(); 117 } 118 } 119 120 } finally { 121 jbpmSession.commitTransactionAndClose(); 122 } 123 124 return millisTillNextTimerIsDue; 125 } 126 127 129 public void addListener(SchedulerListener listener) { 130 if (listeners==null) listeners = new ArrayList (); 131 listeners.add(listener); 132 } 133 134 public void removeListener(SchedulerListener listener) { 135 listeners.remove(listener); 136 if (listeners.isEmpty()) { 137 listeners = null; 138 } 139 } 140 141 private void notifyListeners(Timer timer) { 142 if (listeners!=null) { 143 Date now = new Date (); 144 Iterator iter = new ArrayList (listeners).iterator(); 145 while (iter.hasNext()) { 146 SchedulerListener timerRunnerListener = (SchedulerListener) iter.next(); 147 timerRunnerListener.timerExecuted(now, timer); 148 } 149 } 150 } 151 152 public void setInterval(long interval) { 153 this.interval = interval; 154 } 155 156 private static final Log log = LogFactory.getLog(SchedulerThread.class); 157 } 158 | Popular Tags |