并行网关用于处理流程并发的场景,可以将流程分发到多个执行流程,也可以让多个执行流程合并为一个执行流(但不是必须一起使用)
,也就是说并行网关有两种行为,分叉和合并。下面我们来一起看下。
场景:学生请假同时由班长和班主任审批,等到班长和班主任都审批完成后,由校长审批,通过这个场景来演示其使用。
<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="csdnParallelGateway" name="csdnParallelGateway" isExecutable="true">
<parallelGateway id="parallelgateway1" name="Parallel Gateway">parallelGateway>
<userTask id="usertask1" name="班长审批" activiti:assignee="班长">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler">falsemodeler:initiator-can-complete>
extensionElements>
userTask>
<userTask id="usertask2" name="班主任审批" activiti:assignee="班主任">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler">falsemodeler:initiator-can-complete>
extensionElements>
userTask>
<startEvent id="startevent1" name="Start">startEvent>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="parallelgateway1">sequenceFlow>
<sequenceFlow id="flow2" sourceRef="parallelgateway1" targetRef="usertask1">sequenceFlow>
<sequenceFlow id="flow3" sourceRef="parallelgateway1" targetRef="usertask2">sequenceFlow>
<parallelGateway id="parallelgateway2" name="Parallel Gateway">parallelGateway>
<sequenceFlow id="flow4" sourceRef="usertask1" targetRef="parallelgateway2">sequenceFlow>
<sequenceFlow id="flow5" sourceRef="usertask2" targetRef="parallelgateway2">sequenceFlow>
<userTask id="usertask3" name="校长审批" activiti:assignee="校长">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler">falsemodeler:initiator-can-complete>
extensionElements>
userTask>
<sequenceFlow id="flow6" sourceRef="parallelgateway2" targetRef="usertask3">sequenceFlow>
<endEvent id="endevent1" name="End">endEvent>
<sequenceFlow id="flow7" sourceRef="usertask3" targetRef="endevent1">sequenceFlow>
process>
<bpmndi:BPMNDiagram id="BPMNDiagram_csdnParallelGateway">
<bpmndi:BPMNPlane bpmnElement="csdnParallelGateway" id="BPMNPlane_csdnParallelGateway">
<bpmndi:BPMNShape bpmnElement="parallelgateway1" id="BPMNShape_parallelgateway1">
<omgdc:Bounds height="40.0" width="40.0" x="250.0" y="290.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="380.0" y="190.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="380.0" y="360.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="80.0" y="290.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="parallelgateway2" id="BPMNShape_parallelgateway2">
<omgdc:Bounds height="40.0" width="40.0" x="580.0" y="290.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
<omgdc:Bounds height="55.0" width="105.0" x="720.0" y="280.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="930.0" y="290.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="115.0" y="307.0">omgdi:waypoint>
<omgdi:waypoint x="250.0" y="310.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="270.0" y="290.0">omgdi:waypoint>
<omgdi:waypoint x="270.0" y="217.0">omgdi:waypoint>
<omgdi:waypoint x="380.0" y="217.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="270.0" y="330.0">omgdi:waypoint>
<omgdi:waypoint x="270.0" y="387.0">omgdi:waypoint>
<omgdi:waypoint x="380.0" y="387.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="485.0" y="217.0">omgdi:waypoint>
<omgdi:waypoint x="599.0" y="217.0">omgdi:waypoint>
<omgdi:waypoint x="600.0" y="290.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
<omgdi:waypoint x="485.0" y="387.0">omgdi:waypoint>
<omgdi:waypoint x="600.0" y="387.0">omgdi:waypoint>
<omgdi:waypoint x="600.0" y="330.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
<omgdi:waypoint x="620.0" y="310.0">omgdi:waypoint>
<omgdi:waypoint x="720.0" y="307.0">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
<omgdi:waypoint x="825.0" y="307.0">omgdi:waypoint>
<omgdi:waypoint x="930.0" y="307.0">omgdi:waypoint>
bpmndi:BPMNEdge>
bpmndi:BPMNPlane>
bpmndi:BPMNDiagram>
definitions>
/**
* 部署流程定义
*/
@Test
public void deploy() {
Deployment deployment = repositoryService.createDeployment() // 创建部署
.addClasspathResource("com/jh/activiti/csdnParallelGateway.bpmn20.xml") // 加载流程资源文件
.name("csdn并行网关测试") // 流程名称
.deploy(); // 部署
System.out.println("流程部署ID:" + deployment.getId());
System.out.println("流程部署Name:" + deployment.getName());
}
// csdnParallelGateway
@Test
public void startInstance() {
runtimeService.startProcessInstanceByKey("csdnParallelGateway");
}
这里先完成班长审批,还是先完成办主任审批没有区别,都是在二人完成审批后流转到校长审批节点。
@Test
public void complete() {
taskService.complete("917512");
}
完成后,如下图:
@Test
public void complete() {
taskService.complete("917515");
}
如果是继续完成校长审批任务流程就自动结束了,这里不再测试,感兴趣的可以自己尝试下。