定义流程监听器
package com.example.activiti.listener;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.Expression;
import java.io.Serializable;
public class ExectuionListenerDemo implements Serializable, ExecutionListener {
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) {
System.out.println("流程监听器 : " + message.getValue(execution));
}
}
定义task监听器
package com.example.activiti.listener;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.TaskListener;
import java.io.Serializable;
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));
}
}
所用流程图:
<?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>
测试类:
package com.example.activiti.service;
import com.example.activiti.listener.ExectuionListenerDemo;
import com.example.activiti.listener.TaskListenerDemo;
import com.google.common.collect.Maps;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.impl.test.PluggableActivitiTestCase;
import org.activiti.engine.test.ActivitiRule;
import org.activiti.engine.test.Deployment;
import org.junit.Rule;
import org.junit.Test;
import java.util.Map;
/**
* @Author: 史皓燃
* @UpdateUser:
* @CreateDate 2019/9/22 16:10
* @Description: <h1>监听器测试</h1>
* @ModifyDate: 2019/9/22 16:10
* @Version: 1.0
*/
public class ListenerTest extends PluggableActivitiTestCase{
@Test
@Deployment(resources = {"my-process-listener.bpmn20.xml"})
public void test() {
//流程监听器
ExectuionListenerDemo demo = new ExectuionListenerDemo();
TaskListenerDemo taskListenerDemo = new TaskListenerDemo();
Map<String, Object> var = Maps.newHashMap();
var.put("myprocessListener", demo);
var.put("mytaskListener", taskListenerDemo);
var.put("user", "shr");
runtimeService.startProcessInstanceByKey("myProcess", var);
taskService.complete(taskService.createTaskQuery().singleResult().getId());
}
}
转载:https://blog.csdn.net/qq_42046342/article/details/101163075
查看评论