在BPM流程中,经常需要在执行某个任务之前或之后停顿一段时间,jBPM5中使用Timer Event完成这一操作,如下为DelayTimerEvent流程:
如图,DelayTimerEvent流程共有三个节点,Before Timer,Timer,After Timer。Before Timer为Script Task节点,运行状态该节点执行的Java代码如下:
timeBeforeTimer = System.currentTimeMillis(); System.out.println("Before Timer - " + new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss").format( new Date((Long)timeBeforeTimer))); kcontext.setVariable("timeBeforeTimer", timeBeforeTimer);
Timer为Timer Event节点,该节点设定的Timer Delay为5秒钟,即当流程运行到此节点后停顿5秒钟后继续进行。
After Timer为Script Task节点,运行状态该节点执行的Java代码如下:
Long timeAfter = System.currentTimeMillis(); System.out.println("After Timer - " + new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss").format( new Date(timeAfter))); timerExecutionTime = timeAfter - (Long) timeBeforeTimer; kcontext.setVariable("timerExecutionTime", timerExecutionTime);
- DelayTimerEvent流程运行代码:https://github.com/kylinsoong/.../DelayTimerEventStart.java
- DelayTimerEvent流程:https://github.com/kylinsoong/.../delayTimerEventProcess.bpmn
运行org.jbpm.quickstarts.event.DelayTimerEventStart会运行DelayTimerEvent流程,DelayTimerEventStart代码如下:
package org.jbpm.quickstarts.event; import org.drools.runtime.StatefulKnowledgeSession; import org.drools.runtime.process.ProcessInstance; import org.drools.runtime.process.WorkflowProcessInstance; import org.jbpm.quickstarts.QuickStartBase; public class DelayTimerEventStart extends QuickStartBase { public static void main(String[] args) { new DelayTimerEventStart().test(); } @SuppressWarnings("static-access") public void test() { StatefulKnowledgeSession ksession = createKnowledgeSession("quickstarts/delayTimerEventProcess.bpmn"); ProcessInstance process = ksession.startProcess("org.jbpm.quickstarts.delaytimereventprocess"); try { Thread.currentThread().sleep(6000); } catch (InterruptedException e) { } Long timerExecutionTime = (Long) ((WorkflowProcessInstance)process).getVariable("timerExecutionTime"); System.out.println("The Process Execute Time: " + timerExecutionTime); ksession.dispose(); } }
DelayTimerEvent流程运行输出结果如下:
Before Timer - 02013-20-30 03:20:29 After Timer - 02013-20-30 03:20:34 The Process Execute Time: 5006
如上结果流程执行的时间为5006毫秒,即是由于Timer Delay节点设定5秒钟导致。