001: package org.jbpm.scheduler.ejbtimer;
002:
003: import java.io.Serializable;
004: import java.rmi.RemoteException;
005: import java.util.Iterator;
006:
007: import javax.ejb.EJBException;
008: import javax.ejb.SessionBean;
009: import javax.ejb.SessionContext;
010: import javax.ejb.TimedObject;
011: import javax.ejb.TimerService;
012: import javax.naming.Context;
013: import javax.naming.InitialContext;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.jbpm.JbpmException;
018: import org.jbpm.ejb.LocalCommandService;
019: import org.jbpm.ejb.LocalCommandServiceHome;
020: import org.jbpm.graph.exe.ProcessInstance;
021: import org.jbpm.graph.exe.Token;
022: import org.jbpm.job.Timer;
023:
024: public class TimerServiceBean implements SessionBean, TimedObject {
025:
026: private static final long serialVersionUID = 1L;
027:
028: SessionContext sessionContext;
029:
030: public void ejbCreate() {
031: }
032:
033: public void createTimer(Timer timer) {
034: TimerService timerService = sessionContext.getTimerService();
035: log.debug("creating timer " + timer
036: + " in the ejb timer service");
037: timerService.createTimer(timer.getDueDate(), new TimerInfo(
038: timer));
039: }
040:
041: public void cancelTimersByName(String timerName, Token token) {
042:
043: // TODO make the scanning of timers for cancellation optional by only deleting the timerjobs in the db.
044: // of course, the corresponding ejb timer notifications have to be ignored.
045:
046: log.debug("cancelling timers with name " + timerName
047: + " from the ejb timer service");
048:
049: TimerService timerService = sessionContext.getTimerService();
050: Iterator iter = timerService.getTimers().iterator();
051: while (iter.hasNext()) {
052: javax.ejb.Timer ejbTimer = (javax.ejb.Timer) iter.next();
053: if (ejbTimer.getInfo() instanceof TimerInfo) {
054: TimerInfo timerInfo = (TimerInfo) ejbTimer.getInfo();
055: if (timerInfo.matchesName(timerName, token)) {
056: ejbTimer.cancel();
057: }
058: }
059: }
060: }
061:
062: public void deleteTimersForProcessInstance(
063: ProcessInstance processInstance) {
064:
065: // TODO make the scanning of timers for cancellation optional by only deleting the timerjobs in the db.
066: // of course, the corresponding ejb timer notifications have to be ignored.
067:
068: log.debug("deleting timers for process instance "
069: + processInstance + " from the ejb timer service");
070:
071: TimerService timerService = sessionContext.getTimerService();
072: Iterator iter = timerService.getTimers().iterator();
073: while (iter.hasNext()) {
074: javax.ejb.Timer ejbTimer = (javax.ejb.Timer) iter.next();
075: if (ejbTimer.getInfo() instanceof TimerInfo) {
076: TimerInfo timerInfo = (TimerInfo) ejbTimer.getInfo();
077: if (timerInfo.matchesProcessInstance(processInstance)) {
078: ejbTimer.cancel();
079: }
080: }
081: }
082: }
083:
084: public void ejbTimeout(javax.ejb.Timer ejbTimer) {
085: log.debug("ejb timer " + ejbTimer + " fires");
086: String localCommandServiceJndiName = "java:comp/env/ejb/LocalCommandServiceBean";
087: try {
088: Context initial = new InitialContext();
089: LocalCommandServiceHome localCommandServiceHome = (LocalCommandServiceHome) initial
090: .lookup(localCommandServiceJndiName);
091: LocalCommandService localCommandService = localCommandServiceHome
092: .create();
093: Serializable info = ejbTimer.getInfo();
094: if (!(info instanceof TimerInfo)) {
095: if (info == null) {
096: throw new NullPointerException("timer info is null");
097: } else {
098: throw new ClassCastException("timer info ("
099: + info.getClass().getName()
100: + ") is not of the expected class "
101: + TimerInfo.class.getName());
102: }
103: }
104: TimerInfo timerInfo = (TimerInfo) info;
105: Timer timer = (Timer) localCommandService
106: .execute(new ExecuteTimerCommand(timerInfo
107: .getTimerId()));
108: // if the timer has repeat
109: if ((timer != null) && (timer.getRepeat() != null)) {
110: // create a new timer
111: log.debug("scheduling timer for repeat at "
112: + timer.getDueDate());
113: createTimer(timer);
114: }
115: } catch (Exception e) {
116: JbpmException jbpmException = new JbpmException(
117: "couldn't execute timer", e);
118: log.error(jbpmException);
119: throw jbpmException;
120: }
121: }
122:
123: public void setSessionContext(SessionContext sessionContext)
124: throws EJBException, RemoteException {
125: this .sessionContext = sessionContext;
126: }
127:
128: public void ejbActivate() throws EJBException, RemoteException {
129: }
130:
131: public void ejbPassivate() throws EJBException, RemoteException {
132: }
133:
134: public void ejbRemove() throws EJBException, RemoteException {
135: }
136:
137: private static Log log = LogFactory.getLog(TimerServiceBean.class);
138: }
|