JBPM中 使用JobExecutor执行timer定义的job
Job executor在jbpm.cfg.xml中是被缺省注释的,所以只要去掉此行即可通过JobExecutor来定时触发timer中的event-handler了
<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 {StringdeploymentId;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();}} }}
<?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 /> </on> <transition name="go on" to="next step" g="-16,-17"/> </state> <state name="next step" g="283,46,83,53"/></process>
public class Escalate implements EventListener {private static final longserialVersionUID= 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);}}}