activiti学习(十七)——多实例任务的使用(会签功能)

概述

会签在中国式流程中是非常常见的需求。例如部门领导下发一个文件,需要部门里面每个员工看完之后提交流程以表示已阅。这时候需要使用activiti中的多实例任务。

多实例任务的图标如下:

左边的userTask下方是三条竖线,代表并行多实例,右边的userTask下方是三条横线,代表串行多实例。多实例任务,可以理解为该环节要提交多次才会通过,串行多实例一次只有个任务可提交,并行多实例多个任务可并行提交。

 

串行多实例举例

下面进行串行多实例举例,流程图如下,在员工互评环节为串行多实例

Assignee属性设置为表达式${assignee}

Sequential设置为true,代表串行多实例。loop cardinality设置数量3,代表需要循环提交3次。Collection设置为Assignee的集合,Element variable设置为集合中变量名称,Element variable与Assignee的表达式变量是关联的。Element variable与Collection的关系,类似java中for(String assignee in assigneeList)这样的关系。因此这个流程需要设置多实例任务的Assignee时,我们实际上需要传入流程变量assigneeList。

具体的bpmn文件如下:

<?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="串行多实例任务" name="串行多实例任务" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="员工互评" name="员工互评" activiti:assignee="${assignee}">
      <multiInstanceLoopCharacteristics isSequential="true" activiti:collection="assigneeList" activiti:elementVariable="assignee">
        <loopCardinality>3</loopCardinality>
      </multiInstanceLoopCharacteristics>
    </userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="员工互评"></sequenceFlow>
    <userTask id="领导审核" name="领导审核" activiti:assignee="丁总"></userTask>
    <sequenceFlow id="flow2" sourceRef="员工互评" targetRef="领导审核"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow3" sourceRef="领导审核" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_串行多实例任务">
    <bpmndi:BPMNPlane bpmnElement="串行多实例任务" id="BPMNPlane_串行多实例任务">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="150.0" y="320.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="员工互评" id="BPMNShape_员工互评">
        <omgdc:Bounds height="55.0" width="105.0" x="230.0" y="310.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="领导审核" id="BPMNShape_领导审核">
        <omgdc:Bounds height="55.0" width="105.0" x="380.0" y="310.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="530.0" y="320.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="185.0" y="337.0"></omgdi:waypoint>
        <omgdi:waypoint x="230.0" y="337.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="335.0" y="337.0"></omgdi:waypoint>
        <omgdi:waypoint x="380.0" y="337.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="485.0" y="337.0"></omgdi:waypoint>
        <omgdi:waypoint x="530.0" y="337.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

 

接下来我们启动流程,并配置assigneeList变量:

public void startProcessByIdWithVar() {
	RuntimeService runtimeService = pe.getRuntimeService();
	List<String> assigneeList = new ArrayList<String>();
	assigneeList.add("张三");
	assigneeList.add("李四");
	assigneeList.add("王五");
	Map<String, Object> vars = new HashMap<String, Object>();
	vars.put("assigneeList", assigneeList);
	ProcessInstance pi = runtimeService.startProcessInstanceById("串行多实例任务:2:5004", vars);
	System.out.println("流程定义ID:" + pi.getProcessInstanceId());
	System.out.println("流程实例ID:" + pi.getId());
}

 

启动流程后查看act_ru_execution表,会有两个execution,一个是父execution 2501,其IS_ACTIVE值为0,表示非活动状态。其子execution 2506为活动状态,为子execution,处于员工互评环节。

 

由于是串行多实例任务,act_ru_task表同时只会有一个任务。此时任务处理人为张三。提交了该任务后会到李四,然后再提交就到王五。串行多实例任务是一个接一个的处理方式。

 

act_ru_variable变量表比较多记录,在流程处于多实例任务时,系统会有几个内置的变量,其中几个变量的含义分别是:

nrOfInstances:多实例任务总数;

nrOfCompletedInstances:当前已完成实例数;

loopCounter:循环计数;

nrOfActiveInstances:当前活动状态的实例数。像串行多实例,自然当前活动状态的实例数就是1了。

 

并行多实例举例

并行多实例与串行多实例差不多,仅在Sequential

<?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="并行多实例" name="并行多实例" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="员工互评" name="员工互评" activiti:assignee="${assignee}">
      <multiInstanceLoopCharacteristics isSequential="false" activiti:collection="assigneeList" activiti:elementVariable="assignee">
        <loopCardinality>3</loopCardinality>
      </multiInstanceLoopCharacteristics>
    </userTask>
    <userTask id="领导审批" name="领导审批"></userTask>
    <sequenceFlow id="flow3" sourceRef="员工互评" targetRef="领导审批"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow4" sourceRef="领导审批" targetRef="endevent1"></sequenceFlow>
    <sequenceFlow id="flow5" sourceRef="startevent1" targetRef="员工互评"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_并行多实例">
    <bpmndi:BPMNPlane bpmnElement="并行多实例" id="BPMNPlane_并行多实例">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="200.0" y="390.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="员工互评" id="BPMNShape_员工互评">
        <omgdc:Bounds height="55.0" width="105.0" x="310.0" y="380.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="领导审批" id="BPMNShape_领导审批">
        <omgdc:Bounds height="55.0" width="105.0" x="460.0" y="380.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="610.0" y="390.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="415.0" y="407.0"></omgdi:waypoint>
        <omgdi:waypoint x="460.0" y="407.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="565.0" y="407.0"></omgdi:waypoint>
        <omgdi:waypoint x="610.0" y="407.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="235.0" y="407.0"></omgdi:waypoint>
        <omgdi:waypoint x="310.0" y="407.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

接下来我们启动流程,和之前串行的时候一样,也是把assigneeList作为流程变量,传递处理人信息

public void startProcessByIdWithVar() {
	RuntimeService runtimeService = pe.getRuntimeService();
	List<String> assigneeList = new ArrayList<String>();
	assigneeList.add("张三");
	assigneeList.add("李四");
	assigneeList.add("王五");
	Map<String, Object> vars = new HashMap<String, Object>();
	vars.put("assigneeList", assigneeList);
	ProcessInstance pi = runtimeService.startProcessInstanceById("并行多实例:1:4", vars);
	System.out.println("流程定义ID:" + pi.getProcessInstanceId());
	System.out.println("流程实例ID:" + pi.getId());
}

 

启动流程后,观察act_ru_execution表,看到当前有5条记录。其中第一条为父execution,第二条为标识分支的execution,3-5条为具体的并行execution,。其在执行表中的表现与并行网关很相似。

 

再看act_ru_task表,同时有三条task记录,即张三、李四、王五可并行提交各自的任务。

当我们把张三、李四的记录提交之后,再看act_ru_execution表:

与并行网关不同的是,提交后对应的execution仍在,只是IS_ACTIVE置0。

 

动态配置

上面两个例子中,我们已经通过设置Collection和Element variable来实现动态配置assignee了。还有一个疑问,loop cardinality如果是一个固定值,那么进行员工互评时,员工的数量不可能一直是一个固定值,而且每个部门进行互评时,部门的人数也各不相同,总不可能为每个部门配置一个流程,并且实时维护多实例循环次数。其实只要我们设置了Collection并且不设置loop cardinality的话,那么activiti就会把Collection的长度作为loop cardinality的值,实现了动态配置多实例任务循环次数。

 

条件配置

在上面例子中,还有一个遗漏的属性没讲,就是Completion condition。这个属性表示当其值为true时,多实例流程就完成了。例如部门里项目评估小组进行决策,小组中成员超过60%的人同意,那么该环节就通过。这样我们就要使用Completion condition。

 

我们现在就以项目专家评估小组为例。假设项目需要先由一个专家评估小组进行评估,超过60%的人提交了,那环节就通过。我们设计流程图,在Completion condition中设置${nrOfCompletedInstances/nrOfInstances>=0.6},即当任务完成数大于等于任务总数的60%时,多实例任务完成。

 

下来我们启动流程,这次我们在assigneeList中加入五个人。那么按照条件,当有三人提交后,流程就会走到下一个节点。

public void startProcessByIdWithVar() {
	RuntimeService runtimeService = pe.getRuntimeService();
	List<String> assigneeList = new ArrayList<String>();
	assigneeList.add("张三");
	assigneeList.add("李四");
	assigneeList.add("王五");
	assigneeList.add("赵六");
	assigneeList.add("孙七");
	Map<String, Object> vars = new HashMap<String, Object>();
	vars.put("assigneeList", assigneeList);
	ProcessInstance pi = runtimeService.startProcessInstanceById("条件配置多实例:1:4", vars);
	System.out.println("流程定义ID:" + pi.getProcessInstanceId());
	System.out.println("流程实例ID:" + pi.getId());
}

 

看下执行表act_ru_execution表

 

任务表act_ru_task表

根据设置的 Completion condition,我们只要完成3个task,该节点就会通过。我们尝试提交张三、李四、王五这三人的task看看结果。

当提交了三人的任务后,看看此时act_ru_execution表,已经到了项目实施环节:

 

而act_ru_task表,之前赵六、孙七未提交的任务,会被删除。

可以看看历史任务表act_hi_task,在DELETE_REASON一栏,赵六、孙七是以“deleted”原因而删除的,张三、李四、王五则是以 “completed”原因删除的。

 

上面的例子只是条件配置比较简单的用法。实际中可以配合不同的流程变量进行判断,再配合流程分支和网关,实现不同的走向,例如超过30%的人提交反对意见,则流程退回到上一个节点。

 

小结

本文主要讲解了流程的会签功能如何通过activiti的多实例任务进行实现,以及多实例任务的基本用法。实际项目中,还常常会用到加签、减签等等,这个留到后面再研究。

在 Flowable 中,要实现会签实例任务的退回,可以通过以下步骤进行操作: 1. 首先,创建一个实现了 MultiInstanceActivityBehavior 接口的自定义活动行为类。这个类将负责处理会签实例任务的具体逻辑。 ```java public class MyMultiInstanceActivityBehavior implements MultiInstanceActivityBehavior { // 实现 MultiInstanceActivityBehavior 接口的方法 // ... } ``` 2. 在该类中,实现 `leave` 方法,该方法负责处理会签任务每个实例完成后的操作。在这个方法中,可以判断每个实例的完成情况,并根据需要进行退回操作。 ```java @Override public void leave(ActivityExecution execution) { // 判断当前实例是否需要退回 if (shouldReject(execution)) { // 获取会签任务的执行实例 ExecutionEntity multiInstanceExecution = (ExecutionEntity) execution.getParent(); // 将当前实例退回到上一步 CommandContext commandContext = Context.getCommandContext(); commandContext.getExecutionEntityManager().deleteChildExecutions(execution.getParent()); commandContext.getAgenda().planContinueMultiInstanceOperation(multiInstanceExecution); } else { // 正常离开会签任务 super.leave(execution); } } ``` 3. 将自定义的活动行为类应用到会签实例任务的流程定义中。在 BPMN 文件中,使用 `class` 属性指定自定义类的全限定名。 ```xml <userTask id="multiInstanceTask" name="Multi-Instance Task" flowable:activiti:class="com.example.MyMultiInstanceActivityBehavior" /> ``` 通过以上步骤,就可以在 Flowable 中实现会签实例任务的退回功能。自定义的活动行为类中的 `leave` 方法可以根据业务逻辑判断是否需要退回,并执行相应的操作。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值