flowable学习笔记(四):动态多实例

1.定义流程模板

在这里插入图片描述
【测试用户任务多实例】任务节点配置了以下属性:
集合(多实例):userList。这个创建流程实例时入参需要加上这个参数。
元素变量(多实例):user。工作流创建多实例时会将集合(多实例)的值拆分成元素变量(多实例),这个例子每个实例会有一个入参变量名称是user。
多实例类型:我设置成Parallel(并行)。
完成条件(多实例):${nrOfCompletedInstances/nrOfInstances == 1} 表示全部实例通过提交才算完成。${nrOfCompletedInstances/nrOfInstances >= 0.5} 表示一半的实例通过提交才算完成。
nrOfCompletedInstances:完成实例数。number of Completed Instances
nrOfInstances:总实例数。number of Instances
执行监听器:配置一个执行监听器CommonMoreInstancesExecutionListener,用于打印流程变量。
任务监听器:配置一个任务监听器CommonMoreInstancesTaskListener,用于打印流程变量。

在这里插入图片描述

结束节点配置了以下属性:
执行监听器:配置一个执行监听器CommonEndExecutionListener,用于判断任务节点结束条件。

2.代码
(1)CommonMoreInstancesExecutionListener
package gdut.listener;

import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Random;

@Component
public class CommonMoreInstancesExecutionListener implements ExecutionListener {

    @Override
    public void notify(DelegateExecution delegateExecution) {
        System.out.println("CommonMoreInstancesExecutionListener start");
        Map<String, Object> variables = delegateExecution.getVariables();
        System.out.println("CommonMoreInstancesExecutionListener variables is:" + variables);
        System.out.println("CommonMoreInstancesExecutionListener executionId is:" + delegateExecution.getId());
    }
}
(2)CommonMoreInstancesTaskListener
package gdut.listener;

import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class CommonMoreInstancesTaskListener implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        System.out.println("CommonMoreInstancesTaskListener start");
        Map<String, Object> variables = delegateTask.getVariables();
        System.out.println("CommonMoreInstancesTaskListener variables is:" + variables);
        System.out.println("CommonMoreInstancesTaskListener executionId is:" + delegateTask.getExecutionId());
        System.out.println("CommonMoreInstancesTaskListener taskId is:" + delegateTask.getId());
    }
}
(3)创建流程实例接口

    //创建流程实例
    @PostMapping("/createFlowInstanceByMap")
    public String createFlowInstanceByMap(@RequestBody JSONObject params) {
        String flowDefinitionName = params.getStr("flowDefinitionName");
        Map<String, Object> paramsMap = BeanUtil.beanToMap(params);

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(flowDefinitionName, paramsMap);
        System.out.println("createFlowInstanceByMap instanceId is:" + processInstance.getProcessInstanceId());
        return processInstance.getProcessInstanceId();
    }
3.测试

部署流程定义:
http://localhost:8081/flowable/deployFlowDefinition?fileName=TestMoreInstances.bpmn20.xml

创建流程实例:
http://localhost:8081/flowable/createFlowInstanceByMap
{
“flowDefinitionName”:“TestMoreInstances”,
“userList”:[“liufeifei”, “liushanshan”, “liuhao”]
}

打印输出:

CommonStartExecutionListener start
CommonStartExecutionListener variables is:{userList=["liufeifei","liushanshan","liuhao"], flowDefinitionName=TestMoreInstances}
CommonMoreInstancesExecutionListener start
CommonMoreInstancesExecutionListener variables is:{userList=["liufeifei","liushanshan","liuhao"], flowDefinitionName=TestMoreInstances, commonStartExecutionListener=CommonStartExecutionListener}
CommonMoreInstancesExecutionListener executionId is:caa751e3-875b-11ed-a341-c03c5949a6ca
CommonMoreInstancesExecutionListener start
CommonMoreInstancesExecutionListener variables is:{nrOfActiveInstances=3, userList=["liufeifei","liushanshan","liuhao"], flowDefinitionName=TestMoreInstances, commonStartExecutionListener=CommonStartExecutionListener, loopCounter=0, user=liufeifei, nrOfInstances=3, nrOfCompletedInstances=0}
CommonMoreInstancesExecutionListener executionId is:caaaad4b-875b-11ed-a341-c03c5949a6ca
CommonMoreInstancesTaskListener start
CommonMoreInstancesTaskListener variables is:{nrOfActiveInstances=3, userList=["liufeifei","liushanshan","liuhao"], flowDefinitionName=TestMoreInstances, commonStartExecutionListener=CommonStartExecutionListener, loopCounter=0, user=liufeifei, nrOfInstances=3, nrOfCompletedInstances=0}
CommonMoreInstancesTaskListener executionId is:caaaad4b-875b-11ed-a341-c03c5949a6ca
CommonMoreInstancesTaskListener taskId is:caab4993-875b-11ed-a341-c03c5949a6ca
CommonMoreInstancesExecutionListener start
CommonMoreInstancesExecutionListener variables is:{nrOfActiveInstances=3, userList=["liufeifei","liushanshan","liuhao"], flowDefinitionName=TestMoreInstances, commonStartExecutionListener=CommonStartExecutionListener, loopCounter=1, user=liushanshan, nrOfInstances=3, nrOfCompletedInstances=0}
CommonMoreInstancesExecutionListener executionId is:caaaad4c-875b-11ed-a341-c03c5949a6ca
CommonMoreInstancesTaskListener start
CommonMoreInstancesTaskListener variables is:{nrOfActiveInstances=3, userList=["liufeifei","liushanshan","liuhao"], flowDefinitionName=TestMoreInstances, commonStartExecutionListener=CommonStartExecutionListener, loopCounter=1, user=liushanshan, nrOfInstances=3, nrOfCompletedInstances=0}
CommonMoreInstancesTaskListener executionId is:caaaad4c-875b-11ed-a341-c03c5949a6ca
CommonMoreInstancesTaskListener taskId is:caae7de6-875b-11ed-a341-c03c5949a6ca
CommonMoreInstancesExecutionListener start
CommonMoreInstancesExecutionListener variables is:{nrOfActiveInstances=3, userList=["liufeifei","liushanshan","liuhao"], flowDefinitionName=TestMoreInstances, commonStartExecutionListener=CommonStartExecutionListener, loopCounter=2, user=liuhao, nrOfInstances=3, nrOfCompletedInstances=0}
CommonMoreInstancesExecutionListener executionId is:caaaad4d-875b-11ed-a341-c03c5949a6ca
CommonMoreInstancesTaskListener start
CommonMoreInstancesTaskListener variables is:{nrOfActiveInstances=3, userList=["liufeifei","liushanshan","liuhao"], flowDefinitionName=TestMoreInstances, commonStartExecutionListener=CommonStartExecutionListener, loopCounter=2, user=liuhao, nrOfInstances=3, nrOfCompletedInstances=0}
CommonMoreInstancesTaskListener executionId is:caaaad4d-875b-11ed-a341-c03c5949a6ca
CommonMoreInstancesTaskListener taskId is:cab02b99-875b-11ed-a341-c03c5949a6ca
createFlowInstanceByMap instanceId is:caa5a42e-875b-11ed-a341-c03c5949a6ca

总结:
(1)从上面的打印输出可以发现:flowable是先调用了一次执行监听器,再创建了3个用户任务实例(我请求的参数userList数组长度是3)。
(2)创建的多实例的执行监听器和任务监听器获取到的流程参数,除了创建流程实例的入参,还增加了以下几个变量:
nrOfActiveInstances 活跃的实例数
loopCounter 类似for循环下标 for(int i = 0;i < userList.size();i++) 表示i的值。
nrOfInstances 总的实例数
nrOfCompletedInstances 完成实例数
user 元素变量 获取userList.get(i)的值。
(3)查看flowable数据库表ACT_RU_TASK有三条记录:
select * from ACT_RU_TASK where PROC_INST_ID_ = ‘caa5a42e-875b-11ed-a341-c03c5949a6ca’;
在这里插入图片描述
提交任务
http://localhost:8081/flowable/completeTaskId?taskId=caab4993-875b-11ed-a341-c03c5949a6ca
完成一个任务,结束节点监听器没有执行。ACT_RU_TASK表还有2条记录。
在这里插入图片描述
http://localhost:8081/flowable/completeTaskId?taskId=caae7de6-875b-11ed-a341-c03c5949a6ca
完成第二个任务,结束节点监听器没有执行。ACT_RU_TASK表还有1条记录。
在这里插入图片描述
http://localhost:8081/flowable/completeTaskId?taskId=cab02b99-875b-11ed-a341-c03c5949a6ca
提交第三个任务,结束节点监听器执行了,ACT_RU_TASK表没有记录。
在这里插入图片描述
结束节点执行监听器打印输出:

CommonEndExecutionListener end
CommonEndExecutionListener variables is:{userList=["liufeifei","liushanshan","liuhao"], flowDefinitionName=TestMoreInstances, commonStartExecutionListener=CommonStartExecutionListener}

符合我配置的完成条件,3个实例全部完成,该任务节点才算通过。

bpmn.xml

(1)TestMoreInstances.bpmn20.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:flowable="http://flowable.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.flowable.org/processdef" exporter="Flowable Open Source Modeler" exporterVersion="6.7.2">
  <process id="TestMoreInstances" name="TestMoreInstances" isExecutable="true">
    <documentation>测试多实例</documentation>
    <startEvent id="testMoreInstancesStart" name="测试用户任务多实例开始节点" flowable:formFieldValidation="true">
      <extensionElements>
        <flowable:executionListener event="start" delegateExpression="${commonStartExecutionListener}"></flowable:executionListener>
      </extensionElements>
    </startEvent>
    <userTask id="TestMoreUserTaskInstances" name="测试用户任务多实例" flowable:formFieldValidation="true">
      <extensionElements>
        <flowable:executionListener event="start" delegateExpression="${commonMoreInstancesExecutionListener}"></flowable:executionListener>
        <flowable:taskListener event="create" delegateExpression="${commonMoreInstancesTaskListener}"></flowable:taskListener>
      </extensionElements>
      <multiInstanceLoopCharacteristics isSequential="false" flowable:collection="userList" flowable:elementVariable="user">
        <extensionElements></extensionElements>
        <completionCondition>${nrOfCompletedInstances/nrOfInstances == 1}</completionCondition>
      </multiInstanceLoopCharacteristics>
    </userTask>
    <endEvent id="testMoreInstancesEnd" name="测试用户任务多实例结束节点">
      <extensionElements>
        <flowable:executionListener event="start" delegateExpression="${commonEndExecutionListener}"></flowable:executionListener>
      </extensionElements>
    </endEvent>
    <sequenceFlow id="sid-FF142143-F573-4FD7-905A-BBA5E118F13F" sourceRef="testMoreInstancesStart" targetRef="TestMoreUserTaskInstances"></sequenceFlow>
    <sequenceFlow id="sid-90EEDCBB-5375-4A8E-A1C3-C239743F9D96" sourceRef="TestMoreUserTaskInstances" targetRef="testMoreInstancesEnd"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_TestMoreInstances">
    <bpmndi:BPMNPlane bpmnElement="TestMoreInstances" id="BPMNPlane_TestMoreInstances">
      <bpmndi:BPMNShape bpmnElement="testMoreInstancesStart" id="BPMNShape_testMoreInstancesStart">
        <omgdc:Bounds height="30.0" width="30.0" x="135.0" y="70.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="TestMoreUserTaskInstances" id="BPMNShape_TestMoreUserTaskInstances">
        <omgdc:Bounds height="80.0" width="100.0" x="285.0" y="45.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="testMoreInstancesEnd" id="BPMNShape_testMoreInstancesEnd">
        <omgdc:Bounds height="28.0" width="28.0" x="510.0" y="71.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-FF142143-F573-4FD7-905A-BBA5E118F13F" id="BPMNEdge_sid-FF142143-F573-4FD7-905A-BBA5E118F13F" flowable:sourceDockerX="15.0" flowable:sourceDockerY="15.0" flowable:targetDockerX="50.0" flowable:targetDockerY="40.0">
        <omgdi:waypoint x="164.94999946593478" y="85.0"></omgdi:waypoint>
        <omgdi:waypoint x="285.0" y="85.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-90EEDCBB-5375-4A8E-A1C3-C239743F9D96" id="BPMNEdge_sid-90EEDCBB-5375-4A8E-A1C3-C239743F9D96" flowable:sourceDockerX="50.0" flowable:sourceDockerY="40.0" flowable:targetDockerX="14.0" flowable:targetDockerY="14.0">
        <omgdi:waypoint x="384.94999999998174" y="85.0"></omgdi:waypoint>
        <omgdi:waypoint x="510.0" y="85.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>
参考

flowable 报错 Waiting for changelog lock…
flowable多实例任务注意事项
Flowable—多实例任务:会签

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值