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 {@Overridepublic 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 {@Overridepublic 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—多实例任务:会签

flowable学习笔记(四):动态多实例相关推荐

  1. Flowable学习笔记(二、BPMN 2.0-基础 )

    转载自  Flowable学习笔记(二.BPMN 2.0-基础 ) 1.BPMN简介 业务流程模型和标记法(BPMN, Business Process Model and Notation)是一套图 ...

  2. Flowable学习笔记(一、入门)

    转载自  Flowable学习笔记(一.入门) 一.Flowable简介 1.Flowable是什么 Flowable是一个使用Java编写的轻量级业务流程引擎.Flowable流程引擎可用于部署BP ...

  3. C#可扩展编程之MEF学习笔记(四):见证奇迹的时刻

    前面三篇讲了MEF的基础和基本到导入导出方法,下面就是见证MEF真正魅力所在的时刻.如果没有看过前面的文章,请到我的博客首页查看. 前面我们都是在一个项目中写了一个类来测试的,但实际开发中,我们往往要 ...

  4. MySQL高级学习笔记(四)

    文章目录 MySQL高级学习笔记(四) 1. MySql中常用工具 1.1 mysql 1.1.1 连接选项 1.1.2 执行选项 1.2 mysqladmin 1.3 mysqlbinlog 1.4 ...

  5. IOS学习笔记(四)之UITextField和UITextView控件学习

    IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...

  6. RabbitMQ学习笔记四:RabbitMQ命令(附疑难问题解决)

    RabbitMQ学习笔记四:RabbitMQ命令(附疑难问题解决) 参考文章: (1)RabbitMQ学习笔记四:RabbitMQ命令(附疑难问题解决) (2)https://www.cnblogs. ...

  7. xcode 学习笔记2:动态添加view

    xcode 学习笔记2:动态添加view 2011-07-06 16:48:39|  分类: 计算机学习|字号 订阅 前面说的都是用的Interface Builder来编辑.xib文件来给窗口添加各 ...

  8. JSP学习笔记(四十九):抛弃POI,使用iText生成Word文档

    POI操作excel的确很优秀,操作word的功能却不敢令人恭维.我们可以利用iText生成rtf文档,扩展名使用doc即可. 使用iText生成rtf,除了iText的包外,还需要额外的一个支持rt ...

  9. Ethernet/IP 学习笔记四

    Ethernet/IP 学习笔记四 EtherNet/IP Quick Start for Vendors Handbook (PUB213R0): https://www.odva.org/Port ...

最新文章

  1. Oracle笔记 六、PL/SQL简单语句块、变量定义
  2. Hashtable的遍历
  3. python使用教程cmd啥意思-Python 中的cmd模块学习
  4. 快速记忆python函数-python之格式化字符串速记整理
  5. 商业周刊:摩托罗拉下注Android 不成功便成仁
  6. 文件系统:使用 yum 安装软件包
  7. Underscore.js (1.7.0)-函数预览
  8. php淘客发单_如何使用PHP的curl函数调用维易淘客接口
  9. toj 4604 搞笑版费马大定理
  10. VB 获取路径名各部分 (获取文件路径,获取文件名,获取文件扩展名)自编
  11. 一步步实现windows版ijkplayer系列文章之一Windows10平台编译ffmpeg 4.0.2,生成ffplay
  12. 总结web压力测试工具
  13. 视频教程-2020年软考网络工程师基础知识历年真题详解软考视频教程-软考
  14. 那些便宜的vps,你敢用吗?企业该如何选择云服务器?
  15. 关于ArcMap中道路、河道中心线提取过程
  16. 什么是全栈工程师?前端后端是做什么的?
  17. 区块链游戏- Solcery(Summoner 召唤者)
  18. payoneer企业账号授权代表验证函怎么写
  19. 集宁一中高122班聚会议程安排
  20. AP5126 DC/DC 平均电流型 降压恒流驱动芯片

热门文章

  1. 从瑞星和360打架说起...
  2. 第六章 程序数据集散地:数据库
  3. docker容器挂载权限问题 导致日志文件不生成
  4. 嵌入式入门必看,看看老鸟如何华丽蜕变!(干货分享帖)
  5. jq+插件实现循环播放弹幕弹幕
  6. Java开发面试问题,2021年Java进阶者的新篇章,终于搞明白了
  7. 《炬丰科技-半导体工艺》湿法刻蚀硅片表面性能的变化
  8. 挂机佣兵团-单机挂机游戏
  9. 未发现缺陷(NDF)定义及预防
  10. 为什么计算机无法访问u盘,小编告诉大家为什么u盘连接电脑无法识别