1 package org.jbpm.db; 2 3 import java.util.Iterator ; 4 5 import org.apache.commons.logging.Log; 6 import org.apache.commons.logging.LogFactory; 7 import org.hibernate.HibernateException; 8 import org.hibernate.Query; 9 import org.hibernate.Session; 10 import org.jbpm.graph.exe.ProcessInstance; 11 import org.jbpm.scheduler.exe.SchedulerInstance; 12 import org.jbpm.scheduler.exe.Timer; 13 14 public class SchedulerSession { 15 16 JbpmSession jbpmSession = null; 17 Session session = null; 18 19 public SchedulerSession(JbpmSession jbpmSession) { 20 this.jbpmSession = jbpmSession; 21 this.session = jbpmSession.getSession(); 22 } 23 24 public void saveTimer(Timer timer) { 25 try { 26 session.save(timer); 27 } catch (Exception e) { 28 log.error(e); 29 jbpmSession.handleException(); 30 throw new RuntimeException ("couldn't save timer '"+timer+"' to the database", e); 31 } 32 } 33 34 public void deleteTimer(Timer timer) { 35 try { 36 session.delete(timer); 37 } catch (Exception e) { 38 log.error(e); 39 jbpmSession.handleException(); 40 throw new RuntimeException ("couldn't delete timer '"+timer+"' from the database", e); 41 } 42 } 43 44 private static final String findTimersByDueDate = 45 "select ti " + 46 "from org.jbpm.scheduler.exe.Timer as ti " + 47 "where ti.exception is null " + 48 "order by ti.dueDate asc"; 49 public Iterator findTimersByDueDate() { 50 try { 51 return session.createQuery(findTimersByDueDate).iterate(); 52 } catch (Exception e) { 53 log.error(e); 54 jbpmSession.handleException(); 55 throw new RuntimeException ("couldn't find timers from the database", e); 56 } 57 } 58 59 private static final String findFailedTimers = 60 "select ti " + 61 "from org.jbpm.scheduler.exe.Timer as ti " + 62 "where ti.exception is not null " + 63 "order by ti.dueDate asc"; 64 public Iterator findFailedTimers() { 65 try { 66 return session.createQuery(findFailedTimers).iterate(); 67 } catch (Exception e) { 68 log.error(e); 69 jbpmSession.handleException(); 70 throw new RuntimeException ("couldn't find failed timers from the database", e); 71 } 72 } 73 74 void saveTimers(ProcessInstance processInstance) { 75 SchedulerInstance schedulerInstance = processInstance.getSchedulerInstance(); 76 77 if (schedulerInstance.isProcessEnded()) { 79 cancelTimersForProcessInstance(processInstance); 81 82 } else { 83 Iterator iter = schedulerInstance.getScheduledTimers().iterator(); 85 while (iter.hasNext()) { 86 Timer timer = (Timer) iter.next(); 87 log.debug("saving timer "+timer); 88 session.save(timer); 89 } 90 91 iter = schedulerInstance.getCancelledTimerNames().iterator(); 93 while (iter.hasNext()) { 94 SchedulerInstance.CancelledTimer cancelledTimer = (SchedulerInstance.CancelledTimer) iter.next(); 95 cancelTimers(cancelledTimer); 96 } 97 } 98 } 99 100 private static final String deleteTimersQuery = 101 "delete from org.jbpm.scheduler.exe.Timer " + 102 "where name = :timerName" + 103 " and token = :token"; 104 public void cancelTimers(SchedulerInstance.CancelledTimer cancelledTimer) { 105 try { 106 Query query = session.createQuery(deleteTimersQuery); 107 query.setString("timerName", cancelledTimer.getTimerName()); 108 query.setEntity("token", cancelledTimer.getToken()); 109 query.executeUpdate(); 110 111 } catch (Exception e) { 112 log.error(e); 113 jbpmSession.handleException(); 114 throw new RuntimeException ("couldn't delete timers '"+cancelledTimer.getTimerName()+"' on token '"+cancelledTimer.getToken().getId()+"' from the database", e); 115 } 116 } 117 118 private static final String deleteTimersForProcessInstanceQuery = 119 "delete from org.jbpm.scheduler.exe.Timer " + 120 "where processInstance = :processInstance"; 121 public void cancelTimersForProcessInstance(ProcessInstance processInstance) { 122 try { 123 Query query = session.createQuery(deleteTimersForProcessInstanceQuery); 124 query.setEntity("processInstance", processInstance); 125 query.executeUpdate(); 126 127 } catch (Exception e) { 128 log.error(e); 129 jbpmSession.handleException(); 130 throw new RuntimeException ("couldn't delete timers for process instance '"+processInstance+"'", e); 131 } 132 } 133 134 private static final Log log = LogFactory.getLog(SchedulerSession.class); 135 } 136 | Popular Tags |