【Flowable】 18-多人会签

Metadata

title: 【Flowable】 18-多人会签
date: 2023-01-23 17:48
tags:
  - 行动阶段/完成
  - 主题场景/组件
  - 笔记空间/KnowladgeSpace/ProgramSpace/ModuleSpace
  - 细化主题/Module/Flowable
categories:
  - Flowable
keywords:
  - Flowable
description: 【Flowable】 18-多人会签

【Flowable】 18-多人会签

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="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="User Task">
        <extensionElements>
            <activiti:taskListener event="create" expression="${mulitiInstanceTaskListener.completeListener(execution)}"></activiti:taskListener>
        </extensionElements>
        <multiInstanceLoopCharacteristics isSequential="false" activiti:collection="persons" activiti:elementVariable="person">
            <loopCardinality>3</loopCardinality>
            <completionCondition>${mulitiInstanceCompleteTask.completeTask(execution)}</completionCondition>
        </multiInstanceLoopCharacteristics>
    </userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
    <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
        <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
            <omgdc:Bounds height="35.0" width="35.0" x="420.0" y="310.0"></omgdc:Bounds>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
            <omgdc:Bounds height="55.0" width="105.0" x="700.0" y="300.0"></omgdc:Bounds>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
            <omgdc:Bounds height="35.0" width="35.0" x="950.0" y="310.0"></omgdc:Bounds>
        </bpmndi:BPMNShape>
        <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
            <omgdi:waypoint x="455.0" y="327.0"></omgdi:waypoint>
            <omgdi:waypoint x="700.0" y="327.0"></omgdi:waypoint>
        </bpmndi:BPMNEdge>
        <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
            <omgdi:waypoint x="805.0" y="327.0"></omgdi:waypoint>
            <omgdi:waypoint x="950.0" y="327.0"></omgdi:waypoint>
        </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>

2. 流程说明

  1. 在用户任务节点绑定了一个监听器,监听create行为,该监听器我们是通过 UEL 表达式来实现的,mulitiInstanceTaskListener是我们注入到 Spring 容器中的对象

对应的监听的代码如下:

@Component("mulitiInstanceTaskListener")
public class MulitiInstanceTaskListener implements Serializable {

public void completeListener(DelegateExecution execution){
    System.out.println("任务:"+execution.getId());
    System.out.println("persons:" + execution.getVariable("persons"));
    System.out.println("person" + execution.getVariable("person"));
}
}
  1. 在 Multi instance 中的配置
  • Loop cardinality: 设置为 3 表示只循环 3 次,也就是三个人会签
  • Collection:表示要循环的集合,我们给的是 persons,后面需要在流程变量中赋值
  • Element variable:表示循环的变量
  • Completion condition:表示任务结束的条件,也就是多人会签的结束条件,在此处我们用的是 UEL 表达式,mulitiInstanceCompleteTask表示的是我们注入到 Spring 容器中的对象

mulitiInstanceCompleteTask对象的完整代码为:

@Component("mulitiInstanceCompleteTask")
public class MulitiInstanceCompleteTask implements Serializable {
/**
 * 完成任务是需要触发的方法
 * @param execution
 * @return
 *     false 表示会签任务还没有结束
 *     true 表示会签任务结束了
 */
public boolean completeTask(DelegateExecution execution) {
    System.out.println("总的会签任务数量:" + execution.getVariable("nrOfInstances")
            + "当前获取的会签任务数量:" + execution.getVariable("nrOfActiveInstances")
            + " - " + "已经完成的会签任务数量:" + execution.getVariable("nrOfCompletedInstances"));
    //有一个人同意就通过
    Boolean flag = (Boolean) execution.getVariable("flag");
    System.out.println("当前意见:"+flag);
    return flag;
}
}

上面的三个变量是 Flowable 中自带的可用变量

  1. nrOfInstances: 该会签环节中总共有多少个实例

  2. nrOfActiveInstances: 当前活动的实例的数量,即还没有完成的实例数量。

  3. nrOfCompletedInstances: 已经完成的实例的数量

3. 案例演示

3.1 部署流程

/**
 * Deploy
 */
@Test
void testDeploy() throws Exception {
    Deployment deploy = repositoryService.createDeployment()
            .addClasspathResource("会签案例.bpmn20.xml")
            .name("会签案例")
            .deploy();
    System.out.println("deploy.getId() = " + deploy.getId());
    System.out.println("deploy.getName() = " + deploy.getName());
    System.out.println("部署开始的时间:" + new Date());
    //TimeUnit.MINUTES.sleep(3);
}

3.2 启动流程

在启动流程实例的时候,我们需要设置相关的参数,在流程定义的时候设置的 persons 在此处我们就需要设置了,设置为 Arrays.asList(“张三”,”李四”,”王五”,”赵六”),这里设置了 4 个元素,在流程定义里定义了 3 个,表示只会循环 3 次,启动流程后,在 Task 中可以看到只有 3 个任务

@Test
void startFlow() throws Exception{
    Map<String,Object> map = new HashMap<>();
    // 设置多人会签的数据
    map.put("persons", Arrays.asList("张三","李四","王五","赵六"));
    ProcessInstance processInstance = runtimeService
            .startProcessInstanceById("myProcess:1:ba1518fc-b22d-11ec-9313-c03c59ad2248",map);
}

同时控制也有对应的输出,触发了 Task 的创建事件

3.3 会签处理任务

启动流程后我们发下在 Task 中产生了 3 条任务,我们先通过 TaskService 来完成其中一个任务,设置一个标志 flag 为 false,来控制会签还没有结束,同时 Task 中另外两个任务还在

@Test
void completeTask1(){
    Map<String,Object> map = new HashMap<>();
    map.put("flag",false);
    taskService.complete("71337501-b22e-11ec-a534-c03c59ad2248",map);
    System.out.println("complete ....");
}

当任务执行完成时会同步触发会签完成表达式中对象方法。有如下的输出

同时 Task 表中的记录还有两条

然后当我们在完成一个任务,这时设置 flag 为 true,会发现在这个多人处理中,最多 3 个人处理在第二个人处理后就结束了

@Test
void completeTask1(){
    Map<String,Object> map = new HashMap<>();
    map.put("flag",true); // 设置为true 结束多人会签
    taskService.complete("713570d4-b22e-11ec-a534-c03c59ad2248",map);
    System.out.println("complete ....");
}

同时来看看表结构中的记录,发现没有了