隨著各種業(yè)務(wù)需求的出現(xiàn)筛圆,activiti的一些api或許不滿足與我們的需要卡骂,那么接下來將說明如何進行任意節(jié)點之間的跳轉(zhuǎn)融击,先來說下何為任意節(jié)點的跳轉(zhuǎn)役听。
比如現(xiàn)在流程已經(jīng)運轉(zhuǎn)到userTask2節(jié)點疆液,那么如果我現(xiàn)在想要流程回到userTask1節(jié)點怎么做呢一铅?
思路
首先從思路上應(yīng)該是處理掉當(dāng)前的節(jié)點(也就是刪除正在運行的該任務(wù),更新維護歷史數(shù)據(jù))堕油,然后將流程的當(dāng)前節(jié)點設(shè)置到userTask1節(jié)點潘飘。那么如何做呢肮之?在這篇文章中(ativiti6.0源碼剖析之流程運轉(zhuǎn)原理)已經(jīng)介紹流程運轉(zhuǎn)的原理。所以大概已經(jīng)很清除應(yīng)該如何做了把卜录。
1戈擒、實現(xiàn)我們自己的命令類,實現(xiàn)execute方法
2艰毒、在實現(xiàn)execute邏輯中找出要跳轉(zhuǎn)任務(wù)節(jié)點所在的執(zhí)行實例筐高,以及跳往的目標節(jié)點
3、通知當(dāng)前的活動節(jié)點結(jié)束丑瞧,并且刪除柑土,然后設(shè)置執(zhí)行實例的當(dāng)前節(jié)點為目標節(jié)點。
4绊汹、往operations棧中壓入ContinueProcessInCompensation操作類稽屏,并且傳入的執(zhí)行實例的當(dāng)前節(jié)點為跳轉(zhuǎn)的目標節(jié)點。
實戰(zhàn)
在部署啟動流程之后西乖,完成第一個用戶任務(wù)狐榔,使得流程運轉(zhuǎn)到第二個用戶任務(wù)節(jié)點,此時數(shù)據(jù)庫情況如下
- 自定義跳轉(zhuǎn)命令類
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) {
//獲取任務(wù)實例管理類
TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
//獲取當(dāng)前任務(wù)實例
TaskEntity currentTask = taskEntityManager.findById(taskId);
//獲取當(dāng)前節(jié)點的執(zhí)行實例
ExecutionEntity execution = currentTask.getExecution();
String executionId = execution.getId();
//獲取流程定義id
String processDefinitionId = execution.getProcessDefinitionId();
//獲取目標節(jié)點
Process process = ProcessDefinitionUtil.getProcess(processDefinitionId);
FlowElement flowElement = process.getFlowElement(targetNodeId);
//獲取歷史管理
HistoryManager historyManager = commandContext.getHistoryManager();
//通知當(dāng)前活動結(jié)束(更新act_hi_actinst)
historyManager.recordActivityEnd(execution,"jump to userTask1");
//通知任務(wù)節(jié)點結(jié)束(更新act_hi_taskinst)
historyManager.recordTaskEnd(taskId,"jump to userTask1");
//刪除正在執(zhí)行的當(dāng)前任務(wù)
taskEntityManager.delete(taskId);
//此時設(shè)置執(zhí)行實例的當(dāng)前活動節(jié)點為目標節(jié)點
execution.setCurrentFlowElement(flowElement);
//向operations中壓入繼續(xù)流程的操作類
commandContext.getAgenda().planContinueProcessOperation(execution);
return null;
}
}
調(diào)用命令類
@Test
public void testInvokeCommand(){
processEngine.getManagementService().executeCommand(new JumpAnyWhereCmd("5002","usertask1"));
}
此時數(shù)據(jù)庫: