消息开始事件
一、定义
接受特定的消息发起流程实例。
1. 图形标记
2. XML标记
消息开始事件的XML
<!-- 定义消息 -->
<message id="theMessage" name="dataReportingMessage"></message>
<process id="MessageStartEventProcess" name="消息开始事件示例流程" isExecutable="true">
<!-- 定义消息开始事件 -->
<startEvent id="messageStart">
<!-- 这里的messageRef是指向一个消息id -->
<messageEventDefinition messageRef="theMessage"/>
</startEvent>
二、测试用例
2.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: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/processdef">
<!-- 定义消息 -->
<message id="theMessage" name="dataReportingMessage"></message>
<process id="MessageStartEventProcess" name="消息开始事件示例流程" isExecutable="true">
<!-- 定义消息开始事件 -->
<startEvent id="messageStart">
<messageEventDefinition messageRef="theMessage"/>
</startEvent>
<userTask id="task1" name="数据上报"></userTask>
<endEvent id="end"></endEvent>
<sequenceFlow id="sequenceFlow1" sourceRef="task1" targetRef="end"></sequenceFlow>
<sequenceFlow id="sequenceFlow2" sourceRef="messageStart" targetRef="task1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_MessageStartEventProcess">
<bpmndi:BPMNPlane bpmnElement="MessageStartEventProcess" id="BPMNPlane_MessageStartEventProcess">
<bpmndi:BPMNShape bpmnElement="messageStart" id="BPMNShape_messageStart">
<omgdc:Bounds height="30.0" width="30.5" x="60.0" y="150.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="task1" id="BPMNShape_task1">
<omgdc:Bounds height="80.0" width="100.0" x="191.0" y="125.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end">
<omgdc:Bounds height="28.0" width="28.0" x="405.0" y="151.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sequenceFlow1" id="BPMNEdge_sequenceFlow1">
<omgdi:waypoint x="291.0" y="165.0"></omgdi:waypoint>
<omgdi:waypoint x="405.0" y="165.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sequenceFlow2" id="BPMNEdge_sequenceFlow2">
<omgdi:waypoint x="90.5" y="165.0"></omgdi:waypoint>
<omgdi:waypoint x="191.0" y="165.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
2.2 消息开始事件测试用例
ProcessDefinition processDefinition = this.deployByClasspathResource("startEvent/MessageStart1EventProcess.bpmn20.xml");
//注意这里的messageName就是定义的消息名称非ID
runtimeService.startProcessInstanceByMessage("dataReportingMessage");
List<Task> list = taskService.createTaskQuery().processDefinitionId(processDefinition.getId()).list();
if (CollectionUtils.isNotEmpty(list)) {
list.forEach(task -> log.info("任务名称为:{}", task.getName()));
}
操作界面
总结
消息启动事件,我们可以利用mq的形式接受消息然后启动流程。
视频地址:
演示demo
本文中内容和案例出自贺波老师的书《深入Activiti流程引擎:核心原理与高阶实战》,书中的介绍更全面、详细,推荐给大家。