Job executor在jbpm.cfg.xml中是被缺省注釋的,所以只要去掉此行即可通過JobExecutor來定時觸發timer中的event-handler了?
Xml代碼
<jbpm-configuration><import resource="jbpm.default.cfg.xml" /><import resource="jbpm.businesscalendar.cfg.xml" /><import resource="jbpm.tx.hibernate.cfg.xml" /><import resource="jbpm.jpdl.cfg.xml" /><import resource="jbpm.bpmn.cfg.xml" /><import resource="jbpm.identity.cfg.xml" /><!-- Job executor is excluded for running the example test cases. --><!-- To enable timers and messages in production use, this should be included. --><import resource="jbpm.jobexecutor.cfg.xml" />
</jbpm-configuration>
測試代碼
/*** @author hzhlu*/
public class CopyOfTimerRepeatTest extends JbpmTestCase {String deploymentId;protected void setUp() throws Exception {super.setUp();deploymentId = repositoryService.createDeployment().addResourceFromClasspath("org/jbpm/examples/timer/repeat/process.jpdl.xml").deploy();}protected void tearDown() throws Exception {repositoryService.deleteDeploymentCascade(deploymentId);super.tearDown();}public void testTimerRepeat() {ProcessInstance processInstance = executionService.startProcessInstanceByKey("TimerRepeat");// 查詢進入狀態后是否已經建立起timer jobJob job = managementService.createJobQuery().processInstanceId(processInstance.getId()).uniqueResult();System.out.println("job info:" + job.getDueDate() + " " + job.toString());assertNull(executionService.getVariable(processInstance.getId(), "escalations"));String msg;for (int i = 0; i < 10; i++) {long difference = job.getDueDate().getTime() - System.currentTimeMillis();msg = "Job觸發倒計時: " + difference + " check escalations:"+ executionService.getVariable(processInstance.getId(), "escalations");System.out.println(">>> " + msg);// 延時1秒try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}
}
JPDL 定義文件,3秒鐘后觸發,然后每隔5秒再觸發事件?
XML代碼
<?xml version="1.0" encoding="UTF-8"?><process name="TimerRepeat" xmlns="http://jbpm.org/4.4/jpdl"><start g="19,50,48,48"><transition to="guardedWait" /></start><state name="guardedWait" g="98,46,127,52"><on event="timeout1"><timer duedate="3 seconds" repeat="5 seconds" /><event-listener class="org.jbpm.examples.timer.repeat.Escalate" /></on><transition name="go on" to="next step" g="-16,-17"/></state><state name="next step" g="283,46,83,53"/></process>
事件處理程序?
Java代碼?
public class Escalate implements EventListener {private static final long serialVersionUID = 1L;public void notify(EventListenerExecution execution) {System.out.println("Escalate.notify()");Integer escalations = (Integer) execution.getVariable("escalations");if (escalations == null) {execution.setVariable("escalations", 1);} else {execution.setVariable("escalations", escalations + 1);}}
}
執行結果?
job info:2010-07-27 14:55:43.0 timer[9|2010-07-27 14:55:43|timeout1]?
>>> Job觸發倒計時: 2907 check escalations:null?
>>> Job觸發倒計時: 1907 check escalations:null?
>>> Job觸發倒計時: 891 check escalations:null?
Escalate.notify()?
>>> Job觸發倒計時: -109 check escalations:1?
>>> Job觸發倒計時: -1125 check escalations:1?
>>> Job觸發倒計時: -2125 check escalations:1?
>>> Job觸發倒計時: -3125 check escalations:1?
>>> Job觸發倒計時: -4140 check escalations:1?
Escalate.notify()?
>>> Job觸發倒計時: -5140 check escalations:2?
>>> Job觸發倒計時: -6140 check escalations:2?