写在前面
在实际的业务场景中可能一个节点同时需要多个人来办理,比如开会签到的场景,或者是投票的场景,此时就需要使用到多实例任务,下面我们一起来看下。
1:测试
1.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/test">
<process id="csdnMultiInstance" name="csdnMultiInstance" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="正常任务节点" activiti:assignee="张三">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler">false</modeler:initiator-can-complete>
</extensionElements>
</userTask>
<userTask id="usertask2" name="多实例任务节点" activiti:assignee="${user}">
<extensionElements>
<modeler:initiator-can-complete xmlns:modeler="http://activiti.com/modeler">false</modeler:initiator-can-complete>
</extensionElements>
<multiInstanceLoopCharacteristics isSequential="false" activiti:collection="users" activiti:elementVariable="user">
<completionCondition>${nrOfCompletedInstances/nrOfInstances >= 0.5}</completionCondition>
</multiInstanceLoopCharacteristics>
</userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_csdnMultiInstance">
<bpmndi:BPMNPlane bpmnElement="csdnMultiInstance" id="BPMNPlane_csdnMultiInstance">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="110.0" y="270.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="320.0" y="260.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="610.0" y="260.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="870.0" y="270.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="145.0" y="287.0"></omgdi:waypoint>
<omgdi:waypoint x="320.0" y="287.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="425.0" y="287.0"></omgdi:waypoint>
<omgdi:waypoint x="610.0" y="287.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="715.0" y="287.0"></omgdi:waypoint>
<omgdi:waypoint x="870.0" y="287.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
1.2:部署流程
/**
* 部署流程定义
*/
@Test
public void deploy() {
Deployment deployment = repositoryService.createDeployment() // 创建部署
.addClasspathResource("com/jh/activiti/csdnMultiInstance.bpmn20.xml") // 加载流程资源文件
.name("csdn多实例测试") // 流程名称
.deploy(); // 部署
System.out.println("流程部署ID:" + deployment.getId());
System.out.println("流程部署Name:" + deployment.getName());
}
1.3:创建流程定义
@Test
public void startInstance() {
runtimeService.startProcessInstanceByKey("csdnMultiInstance");
}
生成的任务:
1.4:完成任务
完成任务并指定多实例任务"多实例任务节点"的办理人:
@Test
public void complete() {
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("users", Arrays.asList("test1,test2,test3,test4".split(",")));
taskService.complete("855007", variables);
}
针对"test1,test2,test3,test4"生成了4个任务,如下图:
因为我们设置了${nrOfCompletedInstances/nrOfInstances >= 0.5}
所以有2个人完成任务,该节点就会结束,直接使用taskService.complete(taskId)
来测试:
首先执行taskService.complete("857520");
完成一个任务,变为如下图:
首先执行taskService.complete("857525");
完成一个任务,可以看到剩余的2个任务就自动结束了,变为如下图: