文章目录
- 消息事件
- 开始事件
- 中间事件
- 边界事件
- 代码实现
- xml文件
- 测试流程
- 流程执行步骤
消息事件
消息事件只有一个接收者,消息具有名字与载荷。
信息会储存在 act_ru_event_subscr
表中。
<!-- 定义消息 -->
<message id="msgId1" name="msgName1"/>
<message id="msgId2" name="msgName2"/>
<message id="msgId3" name="msgName3"/>
<message id="msgId4" name="msgName4"/>
开始事件
使用场景:比如接收到一封邮件,一条短信后启动流程
<!-- 消息开始事件 -->
<startEvent id="sid-2aac151d-e888-467a-b8e9-2f512e365228" name="消息开始事件">
<!-- 引用消息 -->
<messageEventDefinition messageRef="msgId1"/>
</startEvent>
中间事件
使用场景:需要接收特定的消息之后才能进入下一个环节
<!-- 消息中间事件 -->
<intermediateCatchEvent id="sid-b3ed607b-e3ea-4695-87af-54c71bd85820" name="消息中间事件">
<messageEventDefinition messageRef="msgId2"/>
</intermediateCatchEvent>
边界事件
使用场景:当前用户任务还没有完成,触发了边界事件(发送消息),会根据cancelActivity属性完成下一个节点。
<!-- 消息边界事件1 -->
<boundaryEvent id="sid-32b97e42-5b52-45fa-9bc3-ec8bd4461087" attachedToRef="sid-c504e710-651a-4f5a-a0a1-6e2e3d3e992a" cancelActivity="false" name="消息边界事件1">
<messageEventDefinition messageRef="msgId3"/>
</boundaryEvent>
代码实现
消息开始事件 - 自动任务 - 消息中间事件 - 用户任务(消息边界事件非中断) - 用户任务(消息边界事件中断)- 结束流程
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="msgId1" name="msgName1"/>
<message id="msgId2" name="msgName2"/>
<message id="msgId3" name="msgName3"/>
<message id="msgId4" name="msgName4"/>
<process id="message" name="消息事件" isExecutable="true">
<documentation>消息事件流程</documentation>
<!-- 消息开始事件 -->
<startEvent id="sid-2aac151d-e888-467a-b8e9-2f512e365228" name="消息开始事件">
<!-- 引用消息 -->
<messageEventDefinition messageRef="msgId1"/>
</startEvent>
<!-- 服务任务 -->
<serviceTask id="sid-bdcd3e82-e9d3-41ee-ae69-f658ea3cf1d1" activiti:exclusive="true" name="服务任务" activiti:delegateExpression="${myServiceTaskListener}"/>
<sequenceFlow id="sid-1bcd8e94-8f7b-4265-a3b7-b2e113bb3d09" sourceRef="sid-2aac151d-e888-467a-b8e9-2f512e365228" targetRef="sid-bdcd3e82-e9d3-41ee-ae69-f658ea3cf1d1"/>
<!-- 结束事件1 -->
<endEvent id="sid-9376d8dd-c6fd-4c8c-94b0-549ca543194c" name="结束事件1"/>
<sequenceFlow id="sid-9360e3fe-a092-46f4-a6d0-b68cc66f7b57" sourceRef="sid-bdcd3e82-e9d3-41ee-ae69-f658ea3cf1d1" targetRef="sid-b3ed607b-e3ea-4695-87af-54c71bd85820"/>
<!-- 消息中间事件 -->
<intermediateCatchEvent id="sid-b3ed607b-e3ea-4695-87af-54c71bd85820" name="消息中间事件">
<!-- 引用消息 -->
<messageEventDefinition messageRef="msgId2"/>
</intermediateCatchEvent>
<!-- 用户任务1 -->
<userTask id="sid-c504e710-651a-4f5a-a0a1-6e2e3d3e992a" name="用户任务1">
<extensionElements>
<activiti:taskListener event="create" delegateExpression="${myUserTaskListener}"/>
</extensionElements>
</userTask>
<sequenceFlow id="sid-fc3da049-2f76-4188-8c33-27a7572c263e" sourceRef="sid-b3ed607b-e3ea-4695-87af-54c71bd85820" targetRef="sid-c504e710-651a-4f5a-a0a1-6e2e3d3e992a"/>
<sequenceFlow id="sid-d164986f-7551-43b3-b18e-da7b2830dc7f" sourceRef="sid-c504e710-651a-4f5a-a0a1-6e2e3d3e992a" targetRef="sid-1dbfc3d6-a8ae-4989-9dab-343a2ab573f5"/>
<!-- 消息边界事件1 -->
<boundaryEvent id="sid-32b97e42-5b52-45fa-9bc3-ec8bd4461087" attachedToRef="sid-c504e710-651a-4f5a-a0a1-6e2e3d3e992a" cancelActivity="false" name="消息边界事件1">
<messageEventDefinition messageRef="msgId3"/>
</boundaryEvent>
<!-- 服务任务 通知服务 -->
<serviceTask id="sid-813f9e98-aa9a-4744-b4ff-503f31cd5af8" activiti:exclusive="true" name="通知服务" activiti:delegateExpression="${myServiceTaskListener}"/>
<sequenceFlow id="sid-a05f2ac4-4cbb-4091-b32a-2251e3d59c34" sourceRef="sid-32b97e42-5b52-45fa-9bc3-ec8bd4461087" targetRef="sid-813f9e98-aa9a-4744-b4ff-503f31cd5af8"/>
<!-- 用户任务2 -->
<userTask id="sid-1dbfc3d6-a8ae-4989-9dab-343a2ab573f5" name="用户任务2">
<extensionElements>
<activiti:taskListener event="create" delegateExpression="${myUserTaskListener}"/>
</extensionElements>
</userTask>
<!-- 消息边界事件2 -->
<boundaryEvent id="sid-e9d7d45d-9d06-4b01-8315-e50b66c2dfbd" attachedToRef="sid-1dbfc3d6-a8ae-4989-9dab-343a2ab573f5" cancelActivity="true" name="消息边界事件2">
<messageEventDefinition messageRef="msgId4"/>
</boundaryEvent>
<!-- 用户任务3 -->
<userTask id="sid-fb470629-bb4f-4c15-8031-629bf6b23474" name="用户任务3">
<extensionElements>
<activiti:taskListener event="create" delegateExpression="${myUserTaskListener}"/>
</extensionElements>
</userTask>
<!-- 结束事件2 -->
<endEvent id="sid-59e5b9e6-1579-464f-bebf-939a6925894b" name="结束事件2"/>
<sequenceFlow id="sid-4796fc97-3d81-44ac-a372-1ec5107ba121" sourceRef="sid-813f9e98-aa9a-4744-b4ff-503f31cd5af8" targetRef="sid-9376d8dd-c6fd-4c8c-94b0-549ca543194c"/>
<sequenceFlow id="sid-7b257b24-9a92-4cca-9280-50f057d3b756" sourceRef="sid-1dbfc3d6-a8ae-4989-9dab-343a2ab573f5" targetRef="sid-59e5b9e6-1579-464f-bebf-939a6925894b"/>
<sequenceFlow id="sid-81b3aace-e1af-4f69-8a77-a1ed9a659c40" sourceRef="sid-e9d7d45d-9d06-4b01-8315-e50b66c2dfbd" targetRef="sid-fb470629-bb4f-4c15-8031-629bf6b23474"/>
<sequenceFlow id="sid-b307971c-2cdf-4af0-bebc-a2943945cc8c" sourceRef="sid-fb470629-bb4f-4c15-8031-629bf6b23474" targetRef="sid-59e5b9e6-1579-464f-bebf-939a6925894b"/>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_message">
<bpmndi:BPMNPlane bpmnElement="message" id="BPMNPlane_message">
<bpmndi:BPMNShape id="shape-2389bc8b-6dca-4ad0-ae30-7f18f1df4ff4" bpmnElement="sid-2aac151d-e888-467a-b8e9-2f512e365228">
<omgdc:Bounds x="-292.75" y="-38.5" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-6933f3c2-9e66-478d-a2bf-22042e7198c2" bpmnElement="sid-bdcd3e82-e9d3-41ee-ae69-f658ea3cf1d1">
<omgdc:Bounds x="-234.25" y="-63.5" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-c3bd0815-b0e9-4f3a-a4a8-d531063d5ee1" bpmnElement="sid-1bcd8e94-8f7b-4265-a3b7-b2e113bb3d09">
<omgdi:waypoint x="-262.75" y="-23.5"/>
<omgdi:waypoint x="-234.25" y="-23.5"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-369198b3-92b5-41dc-be13-161bb96aceeb" bpmnElement="sid-59e5b9e6-1579-464f-bebf-939a6925894b">
<omgdc:Bounds x="388.9129" y="-38.5" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-ea561676-eb7f-4e24-b93e-55253ef99de6" bpmnElement="sid-9360e3fe-a092-46f4-a6d0-b68cc66f7b57">
<omgdi:waypoint x="-134.25" y="-23.5"/>
<omgdi:waypoint x="-106.75" y="-23.5"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-18ea56ca-4b72-4eb3-bc65-02515550d26d" bpmnElement="sid-b3ed607b-e3ea-4695-87af-54c71bd85820">
<omgdc:Bounds x="-106.75" y="-38.5" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-5f517e63-3511-4b12-bb7e-3190a8e458fb" bpmnElement="sid-c504e710-651a-4f5a-a0a1-6e2e3d3e992a">
<omgdc:Bounds x="-49.35" y="-63.5" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-a788f528-47cb-4a02-8ac1-18214cbe2277" bpmnElement="sid-fc3da049-2f76-4188-8c33-27a7572c263e">
<omgdi:waypoint x="-76.75" y="-23.5"/>
<omgdi:waypoint x="-49.35" y="-23.5"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-8a99dde1-060b-4fcc-8053-6e9f8ea853f9" bpmnElement="sid-d164986f-7551-43b3-b18e-da7b2830dc7f">
<omgdi:waypoint x="50.65" y="-23.5"/>
<omgdi:waypoint x="184.43443" y="-23.5"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-9a8d9303-59ce-472d-8ba5-92d68589673c" bpmnElement="sid-813f9e98-aa9a-4744-b4ff-503f31cd5af8">
<omgdc:Bounds x="36.874165" y="61.529808" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-5e7d2270-7207-4b9c-a151-90b8ae3dbec4" bpmnElement="sid-a05f2ac4-4cbb-4091-b32a-2251e3d59c34">
<omgdi:waypoint x="0.7840462" y="10.115295"/>
<omgdi:waypoint x="0.6500015" y="101.5298"/>
<omgdi:waypoint x="36.87416" y="101.52981"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-c3ae777e-7677-4666-9568-82afcd8f105c" bpmnElement="sid-1dbfc3d6-a8ae-4989-9dab-343a2ab573f5">
<omgdc:Bounds x="184.43442" y="-63.5" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-6c351baf-210d-4254-b775-90d3d849503b" bpmnElement="sid-fb470629-bb4f-4c15-8031-629bf6b23474">
<omgdc:Bounds x="276.2087" y="61.529793" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-78f32f48-e45e-4280-8f9e-8da9b1aa8137" bpmnElement="sid-9376d8dd-c6fd-4c8c-94b0-549ca543194c">
<omgdc:Bounds x="174.23729" y="86.5298" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-a27dffa7-5cdb-4725-b416-935122444e28" bpmnElement="sid-4796fc97-3d81-44ac-a372-1ec5107ba121">
<omgdi:waypoint x="136.87416" y="101.52981"/>
<omgdi:waypoint x="174.23729" y="101.5298"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-1c9fe7c0-1a25-4713-964b-c21c7e7da939" bpmnElement="sid-7b257b24-9a92-4cca-9280-50f057d3b756">
<omgdi:waypoint x="284.43442" y="-23.5"/>
<omgdi:waypoint x="388.9129" y="-23.5"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-f224127e-7bf1-4c6b-a9cb-71d3779a2095" bpmnElement="sid-81b3aace-e1af-4f69-8a77-a1ed9a659c40">
<omgdi:waypoint x="243.56537" y="12.270161"/>
<omgdi:waypoint x="244.85805" y="101.5298"/>
<omgdi:waypoint x="276.2087" y="101.529785"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-236d755e-c900-4a3e-ac03-c67efc67fe2c" bpmnElement="sid-b307971c-2cdf-4af0-bebc-a2943945cc8c">
<omgdi:waypoint x="376.20874" y="101.529785"/>
<omgdi:waypoint x="403.91287" y="101.529785"/>
<omgdi:waypoint x="403.9129" y="-8.5"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-0e367d18-9323-48cf-9862-2050042aaa3a" bpmnElement="sid-32b97e42-5b52-45fa-9bc3-ec8bd4461087">
<omgdc:Bounds x="-14.215954" y="-19.884705" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-8741f847-bbde-4157-8d80-729f8858eec8" bpmnElement="sid-e9d7d45d-9d06-4b01-8315-e50b66c2dfbd">
<omgdc:Bounds x="228.56535" y="-17.72984" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
测试流程
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestActivityMessage {
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
private static final String PROCESS_INSTANCE_ID = "bb77d92a-3e55-11ee-8497-18c04dcd4aee";
@Test
public void deployAndStartProcess() {
// 部署流程
Deployment deploy = repositoryService.createDeployment()
.addClasspathResource("processes/message.bpmn20.xml")
.deploy();
System.out.println("deploy = " + deploy);
// 启动流程 根据消息名称触发启动,定义的消息会记录在act_ru_event_subscr,事件结束后自动删除记录。
ProcessInstance processInstance = runtimeService.startProcessInstanceByMessage("msgName1", "message:10001");
System.out.println("processInstance = " + processInstance);
}
@Test
public void receivedMiddleMessage() {
// 查询执行
Execution execution = runtimeService.createExecutionQuery().processInstanceId(PROCESS_INSTANCE_ID).onlyChildExecutions().singleResult();
System.out.println("execution = " + execution);
// 发送消息 触发消息中间事件
runtimeService.messageEventReceived("msgName2", execution.getId());
}
@Test
public void receivedBoundMessage() {
String messageName = "msgName4";
// 查询执行 根据消息名字查询
Execution execution = runtimeService.createExecutionQuery().processInstanceId(PROCESS_INSTANCE_ID).messageEventSubscriptionName(messageName).onlyChildExecutions().singleResult();
System.out.println("execution = " + execution);
// 发送消息
runtimeService.messageEventReceived(messageName, execution.getId());
}
@Test
public void completeUserTask() {
// 查询任务
Task task = taskService.createTaskQuery().processInstanceId(PROCESS_INSTANCE_ID).singleResult();
System.out.println("task = " + task);
// 完成任务
taskService.complete(task.getId());
}
}
流程执行步骤
// 1.部署流程并根据消息开始事件-名字msgName1启动流程
// 2.触发服务任务
// 3.触发消息中间事件msgName2
// 4.查询execution,并发送msgName2消息
// 5.当前任务为用户任务1,触发消息边界事件1-msgName3(非中断)
// 6.根据消息名字msgName3查询execution,并发送msgName3消息
// 7.触发通知服务任务
// 8.完成用户任务1,当前任务变更为用户任务2,触发消息边界事件2-msgName4(中断)
// 9.根据消息名字msgName4查询execution,并发送msgName4消息
// 10.当前任务变更为用户任务3
// 11.完成用户任务3结束流程