01: package org.jbpm.scheduler.ejbtimer;
02:
03: import javax.naming.Context;
04: import javax.naming.InitialContext;
05:
06: import org.apache.commons.logging.Log;
07: import org.apache.commons.logging.LogFactory;
08: import org.jbpm.JbpmContext;
09: import org.jbpm.JbpmException;
10: import org.jbpm.db.JobSession;
11: import org.jbpm.graph.exe.ProcessInstance;
12: import org.jbpm.graph.exe.Token;
13: import org.jbpm.job.Timer;
14: import org.jbpm.scheduler.SchedulerService;
15:
16: public class EntitySchedulerService implements SchedulerService {
17:
18: private static final long serialVersionUID = 1L;
19:
20: JobSession jobSession;
21: LocalTimerService localTimerService;
22:
23: public EntitySchedulerService() {
24: JbpmContext jbpmContext = JbpmContext.getCurrentJbpmContext();
25: if (jbpmContext == null) {
26: throw new JbpmException(
27: "instantiation of the EjbSchedulerService requires a current JbpmContext");
28: }
29: this .jobSession = jbpmContext.getJobSession();
30:
31: try {
32: Context initial = new InitialContext();
33: LocalTimerServiceHome localTimerServiceHome = (LocalTimerServiceHome) initial
34: .lookup("java:comp/env/ejb/LocalTimerServiceBean");
35: localTimerService = localTimerServiceHome.create();
36: } catch (Exception e) {
37: JbpmException jbpmException = new JbpmException(
38: "ejb local timer lookup problem", e);
39: log.error(e);
40: throw jbpmException;
41: }
42: }
43:
44: public void createTimer(Timer timer) {
45: log.debug("creating timer " + timer);
46: jobSession.saveJob(timer);
47: localTimerService.createTimer(timer);
48: }
49:
50: public void deleteTimersByName(String timerName, Token token) {
51: log.debug("deleting timers by name " + timerName);
52: jobSession.cancelTimersByName(timerName, token);
53: localTimerService.cancelTimersByName(timerName, token);
54: }
55:
56: public void deleteTimersByProcessInstance(
57: ProcessInstance processInstance) {
58: log.debug("deleting timers for process instance "
59: + processInstance);
60: jobSession.deleteJobsForProcessInstance(processInstance);
61: localTimerService
62: .deleteTimersForProcessInstance(processInstance);
63: }
64:
65: public void close() {
66: try {
67: log.debug("removing the timer service session bean");
68: localTimerService.remove();
69: } catch (Exception e) {
70: JbpmException jbpmException = new JbpmException(
71: "ejb local timer service close problem", e);
72: log.error(e);
73: throw jbpmException;
74: }
75: }
76:
77: private static Log log = LogFactory
78: .getLog(EntitySchedulerService.class);
79: }
|