第二十篇Flowable中的任务回退
Posted 波波烤鸭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第二十篇Flowable中的任务回退相关的知识,希望对你有一定的参考价值。
Flowable中的任务回退
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="用户任务1" activiti:assignee="user1"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<userTask id="usertask2" name="用户任务2" activiti:assignee="user2"></userTask>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
<userTask id="usertask3" name="用户任务3" activiti:assignee="user3"></userTask>
<sequenceFlow id="flow3" sourceRef="usertask2" targetRef="usertask3"></sequenceFlow>
<userTask id="usertask4" name="用户任务4" activiti:assignee="user4"></userTask>
<sequenceFlow id="flow4" sourceRef="usertask3" targetRef="usertask4"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow5" sourceRef="usertask4" 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="390.0" y="260.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="470.0" y="250.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="620.0" y="250.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
<omgdc:Bounds height="55.0" width="105.0" x="770.0" y="250.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask4" id="BPMNShape_usertask4">
<omgdc:Bounds height="55.0" width="105.0" x="920.0" y="250.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="1070.0" y="260.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="425.0" y="277.0"></omgdi:waypoint>
<omgdi:waypoint x="470.0" y="277.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="575.0" y="277.0"></omgdi:waypoint>
<omgdi:waypoint x="620.0" y="277.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="725.0" y="277.0"></omgdi:waypoint>
<omgdi:waypoint x="770.0" y="277.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="875.0" y="277.0"></omgdi:waypoint>
<omgdi:waypoint x="920.0" y="277.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
<omgdi:waypoint x="1025.0" y="277.0"></omgdi:waypoint>
<omgdi:waypoint x="1070.0" y="277.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
上面的流程就是一个非常简单的串行任务,定义了4个用户任务,指派的处理人分别是user1
,user2
,user3
,user4
.在流程的执行过程中我们可以通过回退来演示具体的效果。首先来部署流程
/**
* 部署流程
*/
@Test
void testDeploy() throws Exception
Deployment deploy = repositoryService.createDeployment()
.addClasspathResource("任务回退01.bpmn20.xml")
.name("任务回退01")
.deploy();
System.out.println("deploy.getId() = " + deploy.getId());
System.out.println("deploy.getName() = " + deploy.getName());
System.out.println("部署开始的时间:" + new Date());
//TimeUnit.MINUTES.sleep(3);
然后我们启动一个流程实例。
/**
* 启动流程实例
*/
@Test
void startProcess()
ProcessInstance processInstance = runtimeService
.startProcessInstanceById("myProcess:1:4");
System.out.println("processInstance.getId() = " + processInstance.getId());
然后我们通过user1完成任务,直到user3完成任务,到user4来处理任务。
/**
* 完成任务
*/
@Test
void completeTask()
Task task = taskService.createTaskQuery()
.processDefinitionId("myProcess:1:4")
.taskAssignee("user1")
.singleResult();
taskService.complete(task.getId());
通过上面的多个Task完成操作,现在已经到了user4来处理的节点了
我们先看下从用户任务4
回退到用户任务3
的操作。
/**
* 回退操作
*/
@Test
void rollbackTask()
// 当前的Task对应的用户任务的Id
List<String> currentActivityIds = new ArrayList<>();
currentActivityIds.add("usertask4");
// 需要回退的目标节点的用户任务Id
String newActivityId = "usertask3";
// 回退操作
runtimeService.createChangeActivityStateBuilder()
.processInstanceId("2501")
.moveActivityIdsToSingleActivityId(currentActivityIds,newActivityId)
.changeState();
操作后我们可以在对应的历史表中看到相关的信息
然后我们通过user3来完成任务继续到user4处理,然后我们可以测试回退到user1处。
/**
* 回退操作
*/
@Test
void rollbackTask()
// 当前的Task对应的用户任务的Id
List<String> currentActivityIds = new ArrayList<>();
currentActivityIds.add("usertask4");
// 需要回退的目标节点的用户任务Id
String newActivityId = "usertask1";
// 回退操作
runtimeService.createChangeActivityStateBuilder()
.processInstanceId("2501")
.moveActivityIdsToSingleActivityId(currentActivityIds,newActivityId)
.changeState();
可以看到任务又回到了user1处。也就是在串行的流程中,我们可以回退到任意的用户节点,当然这个串行也包括多人会签和排他网关节点。当然在回退的时候我们还可以使用moveActivityIdTo(String currentActivityId,String newActivityId)
这个方法来处理。
2.并行的回退
接下来我们在并行的场景中来看看各种回退的场景。具体案例流程如下:
详细的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="用户审批01" activiti:assignee="user1"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="u以上是关于第二十篇Flowable中的任务回退的主要内容,如果未能解决你的问题,请参考以下文章