在实际的业务中,可能存在存在这么一种情况,当流程运行到某一个环节时,可能需要同时多个人的参与,才可以完成此环节。此时就可以用到activiti的多实例来解决此问题。
要把一个节点设置为多实例,节点xml元素必须设置一个 multiInstanceLoopCharacteristics
子元素。
当isSequential=true时,表示的顺序执行,即虽然该节点有多条任务,但只有上一条执行完,才可以执行下一条。
当isSequential=false时,表示的并行执行,即该节点下的多条任务可以同时执行。
二、设置会签环节的参与者:
activiti:collection:用于执行该会签环节的参与参与的人,此处是使用的一个名叫pers的流程变量
activiti:elementVariable:此处表示的是每一个分支都有一个名叫per的流程变量,和上方的activiti:assignee结合使用就可以执行该分支应该由谁来处理。
三.指定会签环节的结束条件:
当画红线的部分返回一个 true 的时候,该会签环节结束。进入下一个流程执行的环节.
completionCondition中写的是juel表达式。其中的mulitiInstance如果是和spring整合了,就是spring管理的bean的id,否则就是流程变量的key.
四、会签环节中涉及的几个默认的流程变量
nrOfActiveInstances 当前活动的实例的数量,即还没有 完成的实例数量。
nrOfCompletedInstances 已经完成的实例的数量
五、代码如下:
1.分配会签环节的人:
/** * 分配下一环节会签的人 * * @author huan * */ public class AssgineeMultiInstancePer implements JavaDelegate { @Override public void execute(DelegateExecution execution) throws Exception { System.out.println("设置会签环节的人员."); execution.setVariable("pers", Arrays.asList("张三", "李四", "王五", "赵六")); } }
2.多实例判断完成的条件:( 注意:有于我没有和spring整合,所以此类要实现Serializable接口 )
/** * 多实例完成的条件判断 * @author huan */ public class MulitiInstanceCompleteTask implements Serializable { private static final long serialVersionUID = 1L; public boolean completeTask(DelegateExecution execution) { System.out.println("总的会签任务数量:" + execution.getVariable("nrOfInstances") + "当前获取的会签任务数量:" + execution.getVariable("nrOfActiveInstances") + " - " + "已经完成的会签任务数量:" + execution.getVariable("nrOfCompletedInstances")); System.out.println("I am invoked."); return false; } }
3.会签环节中的一个监听器:
/** * 测试会签过程中监听器的执行情况 * @author huan */ public class TestLinstener implements TaskListener { private static final long serialVersionUID = -5754522101489239675L; @Override public void notify(DelegateTask delegateTask) { System.out.print(delegateTask.getId() + " - " + delegateTask.getProcessInstanceId() + " - " + delegateTask.getEventName() + " - " + delegateTask.getTaskDefinitionKey()); } }
4.测试代码:
/** * 测试会签 * @author huan */ public class TestMultiInstance { @Test public void testProcess() { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); RuntimeService runtimeService = processEngine.getRuntimeService(); TaskService taskService = processEngine.getTaskService(); Deployment deploy = repositoryService.createDeployment()// .name("会签流程测试")// .addInputStream("multiInstances.bpmn", this.getClass().getResourceAsStream("multiInstances.bpmn"))// .addInputStream("multiInstances.png", this.getClass().getResourceAsStream("multiInstances.png"))// .deploy(); System.out.println(deploy.getId() + " " + deploy.getName()); Map<String, Object> variables = new HashMap<String, Object>(); variables.put("mulitiInstance", new MulitiInstanceCompleteTask()); ProcessInstance pi = runtimeService.startProcessInstanceByKey("multiInstances",variables); System.out.println(pi.getId() + " " + pi.getActivityId()); Task task1 = taskService.createTaskQuery().processInstanceId(pi.getId()).taskAssignee("张三").singleResult(); System.out.println(task1.getId() + " - " + task1.getAssignee() + " - " + task1.getProcessInstanceId() + " - " + task1.getProcessDefinitionId()); Task task2 = taskService.createTaskQuery().processInstanceId(pi.getId()).taskAssignee("李四").singleResult(); System.out.println(task2.getId() + " - " + task2.getAssignee() + " - " + task2.getProcessInstanceId() + " - " + task2.getProcessDefinitionId()); Task task3 = taskService.createTaskQuery().processInstanceId(pi.getId()).taskAssignee("王五").singleResult(); System.out.println(task3.getId() + " - " + task3.getAssignee() + " - " + task3.getProcessInstanceId() + " - " + task3.getProcessDefinitionId()); Task task4 = taskService.createTaskQuery().processInstanceId(pi.getId()).taskAssignee("赵六").singleResult(); if (task4 != null) { System.out.println(task4.getId() + " - " + task4.getAssignee() + " - " + task4.getProcessInstanceId() + " - " + task4.getProcessDefinitionId()); } Task task5 = taskService.createTaskQuery().processInstanceId(pi.getId()).taskAssignee("钱七").singleResult(); System.out.println(task5); taskService.complete(task1.getId()); taskService.complete(task2.getId()); taskService.complete(task3.getId()); Task task6 = taskService.createTaskQuery().processInstanceId(pi.getId()).taskAssignee("钱七").singleResult(); System.out.println(task6); taskService.complete(task4.getId()); Task task7 = taskService.createTaskQuery().processInstanceId(pi.getId()).taskAssignee("钱七").singleResult(); System.out.println(task7); taskService.complete(task7.getId()); ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(pi.getId()).singleResult(); if (null == processInstance) { System.out.println("流程完成."); } } }
5.流程文件:
<?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="multiInstances" name="流程会签测试" isExecutable="true"> <startEvent id="startevent1" name="Start"></startEvent> <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="A001"></sequenceFlow> <serviceTask id="A001" name="设置下一环节的人" activiti:class="com.huan.activiti.liuyang.会签.AssgineeMultiInstancePer"></serviceTask> <userTask id="B001" name="会签环节" activiti:assignee="${per}"> <extensionElements> <activiti:taskListener event="complete" class="com.huan.activiti.liuyang.会签.TestLinstener"></activiti:taskListener> </extensionElements> <multiInstanceLoopCharacteristics isSequential="false" activiti:collection="pers" activiti:elementVariable="per"> <completionCondition>${mulitiInstance.completeTask(execution)}</completionCondition> </multiInstanceLoopCharacteristics> </userTask> <sequenceFlow id="flow2" sourceRef="A001" targetRef="B001"></sequenceFlow> <userTask id="C001" name="会签后的环节" activiti:assignee="钱七"></userTask> <sequenceFlow id="flow3" sourceRef="B001" targetRef="C001"></sequenceFlow> <endEvent id="endevent1" name="End"></endEvent> <sequenceFlow id="flow4" sourceRef="C001" targetRef="endevent1"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_multiInstances"> <bpmndi:BPMNPlane bpmnElement="multiInstances" id="BPMNPlane_multiInstances"> <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"> <omgdc:Bounds height="35.0" width="35.0" x="100.0" y="240.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="A001" id="BPMNShape_A001"> <omgdc:Bounds height="71.0" width="117.0" x="190.0" y="222.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="B001" id="BPMNShape_B001"> <omgdc:Bounds height="55.0" width="105.0" x="380.0" y="230.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="C001" id="BPMNShape_C001"> <omgdc:Bounds height="55.0" width="105.0" x="561.0" y="230.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"> <omgdc:Bounds height="35.0" width="35.0" x="740.0" y="240.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"> <omgdi:waypoint x="135.0" y="257.0"></omgdi:waypoint> <omgdi:waypoint x="190.0" y="257.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"> <omgdi:waypoint x="307.0" y="257.0"></omgdi:waypoint> <omgdi:waypoint x="380.0" y="257.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> <omgdi:waypoint x="485.0" y="257.0"></omgdi:waypoint> <omgdi:waypoint x="561.0" y="257.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"> <omgdi:waypoint x="666.0" y="257.0"></omgdi:waypoint> <omgdi:waypoint x="740.0" y="257.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
7.流程图:
如若那个地方写的不对,欢迎指出。