Activiti5第十一弹,流程监听器与任务监听器
首先创建流程监听器和任务监听器的实体类,个人比较喜欢使用Delegate Expression方式,其他两种方式也可以
流程监听器
package org.mpc.final_activiti; import java.io.Serializable; import org.activiti.engine.delegate.DelegateExecution; import org.activiti.engine.delegate.ExecutionListener; import org.activiti.engine.delegate.Expression; /** * * 可以使用 CLASS ,EXPRESSION,DELEGATE EXPRESSSION三种方式来创建监听器,这里使用第三种方式,其他两种方式和 * 在servicetask中的使用方式相同 * * */ public class ExectuionListenerDemo implements Serializable, ExecutionListener { /** * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么) */ private static final long serialVersionUID = 8513750196548027535L; private Expression message; public Expression getMessage() { return message; } public void setMessage(Expression message) { this.message = message; } @Override public void notify(DelegateExecution execution) throws Exception { System.out.println("流程监听器" + message.getValue(execution)); } }
任务监听器
package org.mpc.final_activiti; import java.io.Serializable; import org.activiti.engine.delegate.DelegateTask; import org.activiti.engine.delegate.Expression; import org.activiti.engine.delegate.TaskListener; /** * * 任务监听器,实现TaskListener接口 * * */ public class TaskListenerDemo implements Serializable, TaskListener { private Expression arg; public Expression getArg() { return arg; } public void setArg(Expression arg) { this.arg = arg; } @Override public void notify(DelegateTask delegateTask) { System.out.println("任务监听器:" + arg.getValue(delegateTask)); } }
这两个监听器只是简单的表明了自己的身份,并打印出在流程中注入给他们的值
接着是流程定义图
从左到右依次是 空开始事件--排他网关--用户任务--空中间抛出事件--空结束事件
首先在空白处点击鼠标,接着在properties选项卡中做如下操作:
然后在排他网关、空中间抛出事件、以及节点之间的箭头上点击鼠标同样可以做如上的操作,这些都流程监听事件的配置
在usertask上我们特别配置usertask才有的任务监听器,配置过程如下图所示
和流程监听器的配置大同小异呢
展示一下配置结果的xml文件,特别注意一下加注释的地方
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test"> <process id="myProcess" name="My process" isExecutable="true"> <extensionElements> <activiti:executionListener event="start" delegateExpression="${myprocessListener}"> <activiti:field name="message"> <activiti:string><![CDATA[流程启动]]></activiti:string> </activiti:field> </activiti:executionListener> <activiti:executionListener event="end" delegateExpression="${myprocessListener}"> <activiti:field name="message"> <activiti:string><![CDATA[流程结束]]></activiti:string> </activiti:field> </activiti:executionListener> </extensionElements> <startEvent id="startevent1" name="Start"></startEvent> <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"> <extensionElements> <activiti:executionListener event="start" delegateExpression="${myprocessListener}"> <activiti:field name="message"> <activiti:string><![CDATA[排他网关开始]]></activiti:string> </activiti:field> </activiti:executionListener> <activiti:executionListener event="end" delegateExpression="${myprocessListener}"> <activiti:field name="message"> <activiti:string><![CDATA[排他网关结束]]></activiti:string> </activiti:field> </activiti:executionListener> </extensionElements> </exclusiveGateway> <sequenceFlow id="flow3" sourceRef="startevent1" targetRef="exclusivegateway1"></sequenceFlow> <userTask id="usertask2" name="User Task" activiti:assignee="${user}"> <extensionElements> <activiti:taskListener event="create" delegateExpression="${mytaskListener}"> <activiti:field name="arg"> <activiti:string><![CDATA[任务启动]]></activiti:string> </activiti:field> </activiti:taskListener> <activiti:taskListener event="assignment" delegateExpression="${mytaskListener}"> <activiti:field name="arg"> <activiti:string><![CDATA[分配人员]]></activiti:string> </activiti:field> </activiti:taskListener> <activiti:taskListener event="complete" delegateExpression="${mytaskListener}"> <activiti:field name="arg"> <activiti:string><![CDATA[任务完成]]></activiti:string> </activiti:field> </activiti:taskListener> <!-- 在可视化工具中无法给usertask加上流程监听器,但是,我们在xml中可以自己敲进去· --> <activiti:executionListener event="start" delegateExpression="${myprocessListener}"> <activiti:field name="message"> <activiti:string><![CDATA[流程启动任务]]></activiti:string> </activiti:field> </activiti:executionListener> <activiti:executionListener event="end" delegateExpression="${myprocessListener}"> <activiti:field name="message"> <activiti:string><![CDATA[流程关闭任务]]></activiti:string> </activiti:field> </activiti:executionListener> <!-- ........................................................................................................................................ --> </extensionElements> </userTask> <sequenceFlow id="flow4" sourceRef="exclusivegateway1" targetRef="usertask2"> <extensionElements> <activiti:executionListener event="take" delegateExpression="${myprocessListener}"> <activiti:field name="message"> <activiti:string><![CDATA[从排他网关到用户任务,好长的一段路]]></activiti:string> </activiti:field> </activiti:executionListener> </extensionElements> </sequenceFlow> <intermediateThrowEvent id="noneintermediatethrowevent1" name="NoneThrowEvent"> <extensionElements> <activiti:executionListener event="start" delegateExpression="${myprocessListener}"> <activiti:field name="message"> <activiti:string><![CDATA[中间事件开始]]></activiti:string> </activiti:field> </activiti:executionListener> <activiti:executionListener event="end" delegateExpression="${myprocessListener}"> <activiti:field name="message"> <activiti:string><![CDATA[中间事件结束]]></activiti:string> </activiti:field> </activiti:executionListener> </extensionElements> </intermediateThrowEvent> <sequenceFlow id="flow5" sourceRef="usertask2" targetRef="noneintermediatethrowevent1"></sequenceFlow> <endEvent id="endevent1" name="End"></endEvent> <sequenceFlow id="flow6" sourceRef="noneintermediatethrowevent1" targetRef="endevent1"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess"> <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess"> <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"> <omgdc:Bounds height="35.0" width="35.0" x="160.0" y="180.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1"> <omgdc:Bounds height="40.0" width="40.0" x="240.0" y="130.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2"> <omgdc:Bounds height="55.0" width="105.0" x="325.0" y="123.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="noneintermediatethrowevent1" id="BPMNShape_noneintermediatethrowevent1"> <omgdc:Bounds height="35.0" width="35.0" x="475.0" y="133.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"> <omgdc:Bounds height="35.0" width="35.0" x="555.0" y="133.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> <omgdi:waypoint x="195.0" y="197.0"></omgdi:waypoint> <omgdi:waypoint x="260.0" y="170.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"> <omgdi:waypoint x="280.0" y="150.0"></omgdi:waypoint> <omgdi:waypoint x="325.0" y="150.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5"> <omgdi:waypoint x="430.0" y="150.0"></omgdi:waypoint> <omgdi:waypoint x="475.0" y="150.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6"> <omgdi:waypoint x="510.0" y="150.0"></omgdi:waypoint> <omgdi:waypoint x="555.0" y="150.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
为了让usertak产生一个指定人员的事件,我们给这个usertask弄一个指定人员
然后就是测试代码
package final_activiti.progress; import java.util.HashMap; import java.util.Map; import org.activiti.engine.impl.test.PluggableActivitiTestCase; import org.activiti.engine.test.Deployment; import org.junit.Test; import org.mpc.final_activiti.ExectuionListenerDemo; import org.mpc.final_activiti.TaskListenerDemo; public class ListenersTest extends PluggableActivitiTestCase { @Test @Deployment(resources = "final_activiti/progress/listeners.bpmn") public void test() { Map<String, Object> map = new HashMap<String, Object>(); map.put("myprocessListener", new ExectuionListenerDemo()); map.put("mytaskListener", new TaskListenerDemo()); map.put("user", "mpc"); runtimeService.startProcessInstanceByKey("myProcess", map); taskService.complete(taskService.createTaskQuery().singleResult() .getId()); ; } }
测试结果:
分析:
1.我们给usertask定义的流程监听实现了
2.usertask中先执行流程监听的start、然后是分配人员、任务启动、任务完成、流程关闭任务;任务是包含在流程中的