新項目中存在一個這樣的業務邏輯,工作流節點任務的任意跳轉。比如說一條流程A-B-C,現在A任務執行完了,按道理來說到B任務了,可是我不想經過B了,直接跳C節點任務執行。自己對工作流這塊也不是很熟悉,在網上查閱了一些資料,修改了一下,也就實現了。現在記錄分享一下。
先附上參考鏈接吧:https://www.jianshu.com/p/3e3087b2213b (還有一篇參考鏈接,恕我實在忘記了)
/** * 跳轉至指定活動節點 * @param processId 流程Id * @param targetTaskDefinitionKey 目標節點任務的id */ public void jump(String processId,String targetTaskDefinitionKey){ //獲取當前節點任務 TaskEntity currentTask = (TaskEntity) taskService.createTaskQuery() .processInstanceId(processId).singleResult(); jump(processId,currentTask,targetTaskDefinitionKey); } /** * @param currentTaskEntity 當前任務節點 * @param targetTaskDefinitionKey 目標任務節點(在模型定義里面的節點任務Id) */ private void jump(String processInstanceId,final TaskEntity currentTaskEntity, String targetTaskDefinitionKey){ ProcessInstance processInstance=runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult(); TaskEntity taskEntity=(TaskEntity) taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult(); //獲取流程定義 ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(processInstance.getProcessDefinitionId()); //當前正在執行任務的節點 ActivityImpl currentActivity = (ActivityImpl)processDefinitionEntity.findActivity(processInstance.getActivityId()); //跳轉的目標節點 ActivityImpl targetActivity = (ActivityImpl) processDefinitionEntity.findActivity(targetTaskDefinitionKey); ((RuntimeServiceImpl)runtimeService).getCommandExecutor() .execute(new Command<java.lang.Void>() { public Void execute(CommandContext commandContext) { ExecutionEntity executionEntity = (ExecutionEntity) runtimeService .createExecutionQuery().executionId(currentTaskEntity.getExecutionId()).singleResult(); executionEntity.setActivity(currentActivity); executionEntity.setEventSource(currentActivity); //根據executionId 獲取Task Iterator<TaskEntity> localIterator = Context.getCommandContext().getTaskEntityManager().findTasksByExecutionId(taskEntity.getExecutionId()).iterator(); while (localIterator.hasNext()) { TaskEntity taskEntity = (TaskEntity) localIterator.next(); //刪除任務 Context.getCommandContext().getTaskEntityManager().deleteTask(taskEntity, "跳轉節點", true); } executionEntity.executeActivity(targetActivity); //設置任務 執行對象 return null; } }); }
說明: (1)閱讀了網上的資料,在processEngine初始化的時候,也會初始化CommandExcutor對象,execute方法中執行了真正的命令,將流程的當前節點任務,替換成目標節點任務對象。
(2) Context.getCommandContext().getTaskEntityManager().deleteTask(taskEntity, "跳轉節點", true);這里如果傳值是false的話,那么就會保留原有的歷史任務信息,比如說A-B-C,A完成之后,正常走是到B,倘若我們A走完,直接跳到C的話,那么C做完之后,查詢該流程的全過程節點任務就是A,B,C三條數據,如果寫true,那么查詢出來的數據就是A,B。所以這里根據場景決定傳值吧。
最后說下自己的想法吧,感覺工作流還是蠻強大的,幫助解決了不少問題,如果對bpmn理解到位了,也能快速使用。自己也還在探索階段,一起加油吧。