View Javadoc
1 package org.smartcomps.twister.engine.priv.timer; 2 3 import org.smartcomps.twister.engine.priv.timer.po.ECTaskPersister; 4 import org.smartcomps.twister.engine.priv.timer.dao.ECTaskDAO; 5 import org.smartcomps.twister.common.persistence.PersistentDataAccess; 6 import org.smartcomps.twister.common.persistence.DBSessionException; 7 import org.smartcomps.twister.common.util.logger.Logger; 8 import org.smartcomps.twister.common.lifecycle.LifecycleAwareResource; 9 import org.smartcomps.twister.common.lifecycle.LifecycleException; 10 import org.smartcomps.twister.common.transaction.TransactionManager; 11 import org.smartcomps.twister.common.transaction.TransactionException; 12 13 import java.util.*; 14 15 /*** 16 * A synchronized static timer that should be used by all instances of the Process 17 * Engine to schedule its tasks. 18 */ 19 public class EngineTimer implements LifecycleAwareResource { 20 21 private static Logger log = Logger.getLogger(EngineTimer.class); 22 23 private static Collection tasks; 24 public static Timer timer; 25 26 public static void schedule(ExecutionContextTask task, Date time) { 27 tasks.add(task); 28 persist(task, time); 29 timer.schedule(task, time); 30 } 31 32 public static void cancelTaskFor(Long ecId) { 33 for (Iterator tasksIter = tasks.iterator(); tasksIter.hasNext();) { 34 ExecutionContextTask contextTask = (ExecutionContextTask) tasksIter.next(); 35 if (contextTask.getContextId().equals(ecId)) { 36 contextTask.cancel(); 37 remove(contextTask); 38 } 39 } 40 } 41 42 private static void persist(ExecutionContextTask task, Date time) { 43 ECTaskPersister taskPersister = new ECTaskPersister(); 44 taskPersister.setDueDate(time); 45 taskPersister.setEcId(task.getContextId()); 46 taskPersister.setAdditionalInfo(task.getAdditionalInfo()); 47 try { 48 PersistentDataAccess.create(taskPersister); 49 } catch (DBSessionException e) { 50 log.warn("Could not create the task persister for a new task.", e); 51 } 52 } 53 54 // TODO remove also executed tasks 55 private static void remove(ExecutionContextTask task) { 56 ECTaskPersister taskPersister = null; 57 try { 58 taskPersister = ECTaskDAO.findByECId(task.getContextId()); 59 PersistentDataAccess.remove(taskPersister); 60 } catch (DBSessionException e) { 61 log.warn("Could not remove the task persister for a new task.", e); 62 } 63 } 64 65 public void create() throws LifecycleException { 66 tasks = new ArrayList(); 67 timer = new Timer(); 68 } 69 70 public void start() throws LifecycleException { 71 try { 72 TransactionManager.beginTransaction(); 73 } catch (TransactionException e) { 74 throw new LifecycleException("Unable to start EngineTimer", e); 75 } 76 List ecTasks = null; 77 try { 78 ecTasks = PersistentDataAccess.findAll(ECTaskPersister.class); 79 } catch (DBSessionException e) { 80 throw new LifecycleException("Unable to start EngineTimer", e); 81 } 82 for (int m = 0; m < ecTasks.size(); m++) { 83 ECTaskPersister taskPersister = (ECTaskPersister) ecTasks.get(m); 84 ExecutionContextTask ecTask = null; 85 if (taskPersister.getAdditionalInfo() == null || taskPersister.getAdditionalInfo().length() == 0) { 86 ecTask = new WaitECTask(taskPersister.getEcId()); 87 } else { 88 ecTask = new PickECTask(taskPersister.getEcId(), new Integer(taskPersister.getAdditionalInfo()).intValue()); 89 } 90 tasks.add(ecTask); 91 timer.schedule(ecTask, taskPersister.getDueDate()); 92 } 93 try { 94 TransactionManager.commitTransaction(); 95 } catch (TransactionException e) { 96 throw new LifecycleException("Unable to start EngineTimer", e); 97 } 98 } 99 100 public void stop() throws LifecycleException { } 101 102 public void destroy() throws LifecycleException { 103 tasks = null; 104 timer = null; 105 } 106 }

This page was automatically generated by Maven