在Flowable开发中可能会遇到当前节点由于某种原因直接跳过 此时可以用到基于flowable命令类实现跳过当前流程,也可配合之前所写Flowable全局监听效果更好 直接上代码!
package com.tlsq.service.workflow.command;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.Process;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.FlowableEngineAgenda;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityManager;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.engine.impl.util.ProcessDefinitionUtil;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.flowable.task.service.impl.persistence.entity.TaskEntityManager;
/**
* @explain: 自定义节点跳转命令
* @author: Lu Yang
* @data: 2019-04-23 14:35
*/
public class TaskJumpCmd implements Command<Void> {
// 任务ID
private String taskId;
// 目标节点ID
private String targetNodeId;
public TaskJumpCmd(String taskId, String targetNodeId) {
this.taskId = taskId;
this.targetNodeId = targetNodeId;
}
@Override
public Void execute(CommandContext commandContext) {
// 获取命令执行实体管理器
ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();
// 获取任务对象管理器
TaskEntityManager taskEntityManager = CommandContextUtil.getTaskServiceConfiguration().getTaskEntityManager();
// 根据任务ID获取任务对象
TaskEntity taskEntity = taskEntityManager.findById(taskId);
// 根据任务对象属性excutionId获取流程实例对象
ExecutionEntity executionEntity = executionEntityManager.findById(taskEntity.getExecutionId());
// 根据流程执行实例中的流程模板定义ID属性获取流程对象
Process process = ProcessDefinitionUtil.getProcess(executionEntity.getProcessDefinitionId());
// 根据目标节点ID获取对应节点
FlowElement targetFlowElement = process.getFlowElement(targetNodeId);
// 在上一流程实例对象中改变当前节点
executionEntity.setCurrentFlowElement(targetFlowElement);
// 获取流程计划 注: 翻译可能不对
FlowableEngineAgenda agenda = CommandContextUtil.getAgenda();
// 更新流程实例对象 注: 翻译可能不对
agenda.planContinueProcessInCompensation(executionEntity);
// 根据在任务对象中删除之前任务
taskEntityManager.delete(taskId);
return null;
}
}
// 改方法的调用
managementService.executeCommand(new TaskJumpCmd(taskId, nextNodeId));
网友评论