Task task = taskService.createTaskQuery() // 创建任务查询
.processInstanceId(自己的ProcessInstanceId) // 根据流程实例id查询
.singleResult();
//获取流程定义
Process process = repositoryService.getBpmnModel(task.getProcessDefinitionId()).getMainProcess();
//获取目标节点定义
FlowNode targetNode = (FlowNode) process.getFlowElement("要跳转的节点名");
//删除当前运行任务
String executionEntityId = managementService.executeCommand(new DeleteTaskCmd(task.getId()));
//流程执行到来源节点
managementService.executeCommand(new SetFLowNodeAndGoCmd(targetNode, executionEntityId));
//删除当前运行时任务命令,并返回当前任务的执行对象id
//这里继承了NeedsActiveTaskCmd,主要时很多跳转业务场景下,要求不能时挂起任务。可以直接继承Command即可
public class DeleteTaskCmd extends NeedsActiveTaskCmd<String> {
public DeleteTaskCmd(String taskId) {
super(taskId);
}
@Override
public String execute(CommandContext commandContext, TaskEntity currentTask) {
//获取所需服务
TaskEntityManagerImpl taskEntityManager = (TaskEntityManagerImpl) commandContext.getTaskEntityManager();
//获取当前任务的来源任务及来源节点信息
ExecutionEntity executionEntity = currentTask.getExecution();
//删除当前任务,来源任务
taskEntityManager.deleteTask(currentTask, "jumpReason", false, false);
return executionEntity.getId();
}
@Override
public String getSuspendedTaskException() {
return "挂起的任务不能跳转";
}
}
//根据提供节点和执行对象id,进行跳转命令
public class SetFLowNodeAndGoCmd implements Command<Void> {
private FlowNode flowElement;
private String executionId;
public SetFLowNodeAndGoCmd(FlowNode flowElement, String executionId) {
this.flowElement = flowElement;
this.executionId = executionId;
}
@Override
public Void execute(CommandContext commandContext) {
//获取目标节点的来源连线
List<SequenceFlow> flows = flowElement.getIncomingFlows();
if (flows == null || flows.size() < 1) {
throw new ActivitiException("回退错误,目标节点没有来源连线");
}
//随便选一条连线来执行,时当前执行计划为,从连线流转到目标节点,实现跳转
ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findById(executionId);
executionEntity.setCurrentFlowElement(flows.get(0));
commandContext.getAgenda().planTakeOutgoingSequenceFlowsOperation(executionEntity, true);
return null;
}
}
activiti 6.0自由跳转
最新推荐文章于 2022-10-28 11:33:36 发布