监听器是Activiti在BPMN 2.0规范基础上扩展的功能,是业务与流程的“非侵入性粘合剂”。在Activiti中开发人员可以通过配置监听器的方式监听各种动作,若流程的启动和结束,任务的创建和完成,某个顺序流的经过等。
监听器分为两种:
- 执行监听器
- 任务监听器
执行监听器主要功能为监听流程的启动、结束和节点连接线的流经。
任务监听器主要是对任务创建和完成的监控。
监听器事件主要分为三种:
- start(开始事件)
- end(结束事件)
- take(捕获事件)
下面通过代码实例来学习监听器,下面所有的代码均在spring boot框架下编写。
1、绘制流程图
新建一个流程图,该流程图包含开始、系统任务、结束三个节点,分别给三个节点设置监听器
流程开始节点
系统任务节点
流程结束节点
给系统任务设置执行方法
流程图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: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="listenerProcess" name="Listener Process" isExecutable="true">
<startEvent id="startevent1" name="Start">
<extensionElements>
<activiti:executionListener event="start" expression="${listenerService.start()}"></activiti:executionListener>
</extensionElements>
</startEvent>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
<endEvent id="endevent1" name="End">
<extensionElements>
<activiti:executionListener event="end" expression="${listenerService.end()}"></activiti:executionListener>
</extensionElements>
</endEvent>
<sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
<serviceTask id="servicetask1" name="User Task" activiti:expression="${resumeService.storeResume()}">
<extensionElements>
<activiti:executionListener event="start" expression="${listenerService.taskStart()}"></activiti:executionListener>
<activiti:executionListener event="end" expression="${listenerService.taskEnd()}"></activiti:executionListener>
</extensionElements>
</serviceTask>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
<bpmndi:BPMNPlane bpmnElement="listenerProcess" id="BPMNPlane_myProcess">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="150.0" y="190.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="380.0" y="190.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
<omgdc:Bounds height="55.0" width="105.0" x="230.0" y="180.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="185.0" y="207.0"></omgdi:waypoint>
<omgdi:waypoint x="230.0" y="207.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="335.0" y="207.0"></omgdi:waypoint>
<omgdi:waypoint x="380.0" y="207.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
2、编写代码
监听器接口代码如下
public interface ListenerService {
void start();
void end();
void taskStart();
void taskEnd();
void taskExecute();
}
监听器实现类代码如下
@Service("listenerService")
public class ListenerServiceImpl implements ListenerService {
/**
* 流程开始
*/
@Override
public void start() {
System.out.println("Activiti监听器:流程开始...................................");
}
/**
* 流程结束
*/
@Override
public void end() {
System.out.println("Activiti监听器:流程结束...................................");
}
/**
* 任务开始
*/
@Override
public void taskStart() {
System.out.println("Activiti监听器:任务开始...................................");
}
/**
* 任务结束
*/
@Override
public void taskEnd() {
System.out.println("Activiti监听器:任务结束...................................");
}
/**
* 任务执行
*/
@Override
public void taskExecute() {
System.out.println("Activiti系统任务:任务执行...................................");
}
}
3、编写单元测试
单元测试类代码如下:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootActivitiApplication.class)
public class ListenerTest {
@Autowired
RuntimeService runtimeService;
/**
* 启动流程
*/
@Test
public void testStartProcess() {
runtimeService.startProcessInstanceByKey("listenerProcess");
}
}