KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > scheduler > impl > SchedulerThread


1 package org.jbpm.scheduler.impl;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Date JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
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 JavaDoc {
18   
19   static JbpmSessionFactory jbpmSessionFactory = JbpmSessionFactory.getInstance();
20   static BusinessCalendar businessCalendar = new BusinessCalendar();
21   
22   List JavaDoc listeners = new ArrayList JavaDoc();
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         // calculate the milliseconds to wait...
37
if (millisToWait < 0) {
38           millisToWait = interval;
39         }
40         millisToWait = Math.min(millisToWait, interval);
41
42       } catch (RuntimeException JavaDoc e) {
43         log.info("runtime exception while executing timers", e);
44       } finally {
45         try {
46           Thread.sleep(millisToWait);
47         } catch (InterruptedException JavaDoc e) {
48           log.info("waiting for timers got interuppted");
49         }
50       }
51     }
52     log.info("ending scheduler thread");
53   }
54   
55   /**
56    * executes due timers and calculates the time before the next timer is due.
57    * @return the number of milliseconds till the next job is due or -1 if no timer is
58    * schedulerd in the future.
59    */

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 JavaDoc 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 this timer is due
77
if (timer.isDue()) {
78           log.debug("executing timer '"+timer+"'");
79             
80           // execute
81
timer.execute();
82             
83           // notify the listeners (e.g. the scheduler servlet)
84
notifyListeners(timer);
85             
86           // if there was an exception, just save the timer
87
if (timer.getException()!=null) {
88             schedulerSession.saveTimer(timer);
89             
90           // if repeat is specified
91
} else if (timer.getRepeat()!=null) {
92             // update timer by adding the repeat duration
93
Date JavaDoc dueDate = timer.getDueDate();
94             
95             // suppose that it took the timer runner thread a
96
// very long time to execute the timers.
97
// then the repeat action dueDate could already have passed.
98
while (dueDate.getTime()<=System.currentTimeMillis()) {
99               dueDate = businessCalendar
100                     .add(dueDate,
101                       new Duration(timer.getRepeat()));
102             }
103             timer.setDueDate( dueDate );
104             // save the updated timer in the database
105
log.debug("saving updated timer for repetition '"+timer+"' in '"+(dueDate.getTime()-System.currentTimeMillis())+"' millis");
106             schedulerSession.saveTimer(timer);
107             
108           } else {
109             // delete this timer
110
log.debug("deleting timer '"+timer+"'");
111             schedulerSession.deleteTimer(timer);
112           }
113
114         } else { // this is the first timer that is not yet due
115
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   // listeners ////////////////////////////////////////////////////////////////
128

129   public void addListener(SchedulerListener listener) {
130     if (listeners==null) listeners = new ArrayList JavaDoc();
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 JavaDoc now = new Date JavaDoc();
144       Iterator JavaDoc iter = new ArrayList JavaDoc(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