activiti6.0源码剖析之节点任意跳转
2019-04-19 本文已影响2人
我有一只喵喵
随着各种业务需求的出现,activiti的一些api或许不满足与我们的需要,那么接下来将说明如何进行任意节点之间的跳转,先来说下何为任意节点的跳转。
![](https://img.haomeiwen.com/i6922386/23a1e7e3be63751d.png)
比如现在流程已经运转到userTask2节点,那么如果我现在想要流程回到userTask1节点怎么做呢?
思路
首先从思路上应该是处理掉当前的节点(也就是删除正在运行的该任务,更新维护历史数据),然后将流程的当前节点设置到userTask1节点。那么如何做呢?在这篇文章中(ativiti6.0源码剖析之流程运转原理)已经介绍流程运转的原理。所以大概已经很清除应该如何做了把。
1、实现我们自己的命令类,实现execute方法
2、在实现execute逻辑中找出要跳转任务节点所在的执行实例,以及跳往的目标节点
3、通知当前的活动节点结束,并且删除,然后设置执行实例的当前节点为目标节点。
4、往operations栈中压入ContinueProcessInCompensation操作类,并且传入的执行实例的当前节点为跳转的目标节点。
实战
在部署启动流程之后,完成第一个用户任务,使得流程运转到第二个用户任务节点,此时数据库情况如下
![](https://img.haomeiwen.com/i6922386/ddc522811e012551.png)
![](https://img.haomeiwen.com/i6922386/0330bf73f1dcb40b.png)
![](https://img.haomeiwen.com/i6922386/2f9be0ca606bbe13.png)
![](https://img.haomeiwen.com/i6922386/45fe7adff74954a3.png)
- 自定义跳转命令类
public class JumpAnyWhereCmd implements Command {
private String taskId;
private String targetNodeId;
public JumpAnyWhereCmd(String taskId, String targetNodeId) {
this.taskId = taskId;
this.targetNodeId = targetNodeId;
}
public Object execute(CommandContext commandContext) {
//获取任务实例管理类
TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
//获取当前任务实例
TaskEntity currentTask = taskEntityManager.findById(taskId);
//获取当前节点的执行实例
ExecutionEntity execution = currentTask.getExecution();
String executionId = execution.getId();
//获取流程定义id
String processDefinitionId = execution.getProcessDefinitionId();
//获取目标节点
Process process = ProcessDefinitionUtil.getProcess(processDefinitionId);
FlowElement flowElement = process.getFlowElement(targetNodeId);
//获取历史管理
HistoryManager historyManager = commandContext.getHistoryManager();
//通知当前活动结束(更新act_hi_actinst)
historyManager.recordActivityEnd(execution,"jump to userTask1");
//通知任务节点结束(更新act_hi_taskinst)
historyManager.recordTaskEnd(taskId,"jump to userTask1");
//删除正在执行的当前任务
taskEntityManager.delete(taskId);
//此时设置执行实例的当前活动节点为目标节点
execution.setCurrentFlowElement(flowElement);
//向operations中压入继续流程的操作类
commandContext.getAgenda().planContinueProcessOperation(execution);
return null;
}
}
调用命令类
@Test
public void testInvokeCommand(){
processEngine.getManagementService().executeCommand(new JumpAnyWhereCmd("5002","usertask1"));
}
此时数据库:
![](https://img.haomeiwen.com/i6922386/0576944fd91c50fc.png)
![](https://img.haomeiwen.com/i6922386/5e450f406006094a.png)
![](https://img.haomeiwen.com/i6922386/b7b13b844c41b015.png)
![](https://img.haomeiwen.com/i6922386/e2207231e9f3e7fd.png)