1.通过代码实现,即获取到当前节点,然后退回到已走过的指定节点。代码如下:
@RequestMapping("/returnNode") public String returnNode(String taskId) { // 取得当前任务.当前任务节点 HistoricTaskInstance currTask = historyService.createHistoricTaskInstanceQuery().taskId(taskId).singleResult(); // 取得所有历史任务按时间降序排序 List<HistoricTaskInstance> hisInstances = historyService.createHistoricTaskInstanceQuery().processInstanceId(currTask.getProcessInstanceId()).orderByTaskCreateTime().desc().list(); if(ObjectUtils.isEmpty(hisInstances)||hisInstances.size()<2){ return "fail"; } //目的节点 HistoricTaskInstance lastTask = null; //所有目的节点的历史记录 List<HistoricTaskInstance> commitList = historyService.createHistoricTaskInstanceQuery().processInstanceId(currTask.getProcessInstanceId()).taskName("one").orderByTaskCreateTime().asc().list(); lastTask=commitList.get(0); if (null==lastTask){ return "fail"; } // 目的节点的taskId String lastTaskId = lastTask.getId(); // 目的节点的executionId String lastExecutionId = lastTask.getExecutionId(); //目的节点对应的流程定义ID String processDefinitionId = lastTask.getProcessDefinitionId(); //对应的流程图文件 BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId); String lastActivityId = null; //获取所有和目的节点任务名一样的已完成的历史记录 List<HistoricActivityInstance> finishedList = historyService.createHistoricActivityInstanceQuery().executionId(lastExecutionId).finished().list(); for (HistoricActivityInstance f: finishedList){ if(lastTaskId.equals(f.getTaskId())){ lastActivityId=f.getActivityId(); break; } } FlowNode lastFlowNode = (FlowNode)bpmnModel.getMainProcess().getFlowElement(lastActivityId); // 取得当前节点的信息 // 当前节点的executionId String curExecutionId = currTask.getExecutionId(); Execution execution = runtimeService.createExecutionQuery().executionId(curExecutionId).singleResult(); String curActivityId = execution.getActivityId(); FlowNode curFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(curActivityId); //记录当前节点的原活动方向 List<SequenceFlow> oriSequenceFlows = new ArrayList<>(); oriSequenceFlows.addAll(curFlowNode.getOutgoingFlows()); //清理活动方向 curFlowNode.getOutgoingFlows().clear(); //建立新方向 List<SequenceFlow> newSequenceFlowList = new ArrayList<>(); SequenceFlow newSequenceFlow = new SequenceFlow(); newSequenceFlow.setId("newSequenceFlowId"); newSequenceFlow.setSourceFlowElement(curFlowNode); newSequenceFlow.setTargetFlowElement(lastFlowNode); newSequenceFlowList.add(newSequenceFlow); curFlowNode.setOutgoingFlows(newSequenceFlowList); // 完成任务 taskService.complete(taskId); //恢复原方向 curFlowNode.setOutgoingFlows(oriSequenceFlows); return "成功"; } |
对应的流程图如下:
上述代码可以实现从two节点退回到one节点
2.通过排他网关实现,流程图如下:
通过设置two节点完成时的参数确定流程图是退回到one节点还是结束。
注意,方法一不会自动删除流程中的参数,需要手动删除,如果通过网关退回,可以实现退回后之前的流程变量自动被删除。