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