相关类:
org.activiti.engine.delegate.ExecutionListener
org.activiti.engine.delegate.TaskListener
org.activiti.engine.delegate.Expression
在流程实例执行的过程中触发某个事件时,执行监听器允许你去执行额外的java代码或者对指定的表达式求值。可以被捕捉的事件有:
-
Start and ending of a process instance.
-
Taking a transition.
-
Start and ending of an activity.
-
Start and ending of a gateway.
-
Start and ending of intermediate events.
-
Ending a start event or starting an end event.
包含3个执行监听器的流程定义:
<process id="executionListenersProcess"> <extensionElements> <activiti:executionListener class="org.activiti.examples.bpmn.executionlistener.ExampleExecutionListenerOne" event="start" /> </extensionElements> <startEvent id="theStart" /> <sequenceFlow sourceRef="theStart" targetRef="firstTask" /> <userTask id="firstTask" /> <sequenceFlow sourceRef="firstTask" targetRef="secondTask"> <extensionElements> <activiti:executionListener class="org.activiti.examples.bpmn.executionListener.ExampleExecutionListenerTwo" /> </extensionElements> </sequenceFlow> <userTask id="secondTask" > <extensionElements> <activiti:executionListener expression="${myPojo.myMethod(execution.event)}" event="end" /> </extensionElements> </userTask> <sequenceFlow sourceRef="secondTask" targetRef="thirdTask" /> <userTask id="thirdTask" /> <sequenceFlow sourceRef="thirdTask" targetRef="theEnd" /> <endEvent id="theEnd" /> </process>
当进程启动时,将通知第一个执行侦听器。侦听器是一个外部java类(如ExampleExecutionListenerOne),它应该实现org.activiti.engine.delegate.ExecutionListener接口。当事件发生时(在本用例中为end事件),方法nitify会被调用。
public class ExampleExecutionListenerOne implements ExecutionListener { public void notify(ExecutionListenerExecution execution) throws Exception { execution.setVariable("variableSetInExecutionListener", "firstValue"); execution.setVariable("eventReceived", execution.getEventName()); } }
它也可以是一个实现了org.activiti.engine.delegate.JavaDelegate接口的委托类,这些委托类可以在其它的结构中重用,比如serviceTask的delegation。
当执行过渡时( when the transition is taken)第二个侦听器会被调用。注意listener
元素没有定义event属性,因为在过渡的时候,只会触发take事件。当在过渡中(sequenceFlow 元素)定义侦听器时,event属性中的值将被忽略。
当活动secondTask结束时会调用第三个执行侦听器。在侦听器的申明中没有使用class属性,而是使用expression
属性定义了一个表达式
<activiti:executionListener expression="${myPojo.myMethod(execution.eventName)}" event="end" />
与其他表达式一样,执行变量execution 可以被解析使用。因为执行实现对象拥有一个暴露事件名的属性,所以可以使用execution.eventName将事件名称传递给您的方法。
执行侦听器也支持delegateExpression 类似于service Task
<activiti:executionListener event="start" delegateExpression="${myExecutionListenerBean}" />
执行侦听器还支持script execution listener,需要实现 org.activiti.engine.impl.bpmn.listener.ScriptExecutionListener接口
<activiti:executionListener event="start" class="org.activiti.engine.impl.bpmn.listener.ScriptExecutionListener" > <activiti:field name="script"> <activiti:string> def bar = "BAR"; // local variable foo = "FOO"; // pushes variable to execution context execution.setVariable("var1", "test"); // test access to execution instance bar // implicit return value </activiti:string> </activiti:field> <activiti:field name="language" stringValue="groovy" /> <activiti:field name="resultVariable" stringValue="myVar" /> </activiti:executionListener>
将属性注入到执行侦听器中
当执行侦听器是通过class属性指定的,我们可以为实例化后的对象注入属性。
执行侦听器属性注入的一个简单例子:
<process id="executionListenersProcess"> <extensionElements> <activiti:executionListener class="org.activiti.examples.bpmn.executionListener.ExampleFieldInjectedExecutionListener" event="start"> <activiti:field name="fixedValue" stringValue="Yes, I am " /> <activiti:field name="dynamicValue" expression="${myVar}" /> </activiti:executionListener> </extensionElements> <startEvent id="theStart" /> <sequenceFlow sourceRef="theStart" targetRef="firstTask" /> <userTask id="firstTask" /> <sequenceFlow sourceRef="firstTask" targetRef="theEnd" /> <endEvent id="theEnd" /> </process>
public class ExampleFieldInjectedExecutionListener implements ExecutionListener { private Expression fixedValue; private Expression dynamicValue; public void notify(ExecutionListenerExecution execution) throws Exception { execution.setVariable("var", fixedValue.getValue(execution).toString() + dynamicValue.getValue(execution).toString()); } }
@Deployment(resources = {"org/activiti/examples/bpmn/executionListener/ExecutionListenersFieldInjectionProcess.bpmn20.xml"}) public void testExecutionListenerFieldInjection() { Map<String, Object> variables = new HashMap<String, Object>(); variables.put("myVar", "listening!"); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("executionListenersProcess", variables); Object varSetByListener = runtimeService.getVariable(processInstance.getId(), "var"); assertNotNull(varSetByListener); assertTrue(varSetByListener instanceof String); // Result is a concatenation of fixed injected field and injected expression assertEquals("Yes, I am listening!", varSetByListener); }