读书人

JBPM中 施用JobExecutor执行timer定义

发布时间: 2012-10-08 19:54:56 作者: rapoo

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();}} }}



JPDL 定义文件,3秒钟后触发,然后每隔5秒再触发事件
<?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);}}}


执行结果
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

2 楼 shi12957 2012-08-08 您好,我现在用JBPM4.4用到其定时器功能,我发现按照例子里面写的不能用,老提示inactive-scope,请问您遇到过类似的问题没?如何解决?谢谢

读书人网 >软件架构设计

热点推荐