在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);
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);
运行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(); } }
Before Timer - 02013-20-30 03:20:29 After Timer - 02013-20-30 03:20:34 The Process Execute Time: 5006