flowable实现多实例节点的自由跳转

最近弃用activiti,改用flowable,发现在实现多节点实例自由跳转时,有很大区别。
自由跳转整理如下:


import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.Process;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.FlowableEngineAgenda;
import org.flowable.engine.impl.cmd.NeedsActiveTaskCmd;
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 java.util.List;
import java.util.Map;


/**
 * @description: 自由跳转流程
 * @author: starmark
 * @create: 2018-10-13 09:22
 **/
public class ActJumpTaskCmd extends NeedsActiveTaskCmd<Boolean> {

    protected String processId;//执行实例id
    protected String targetNodeId;//目标节点
    protected Map<String, Object> formData;//变量
    protected String operationCode;
    public ActJumpTaskCmd(String taskId,   String processId, String targetNodeId, Map<String, Object> formData,String operationCode) {
        super(taskId);
        this.processId = processId;
        this.targetNodeId = targetNodeId;
        this.formData = formData;
        this.operationCode=operationCode;
    }


    @Override
    protected Boolean execute(CommandContext commandContext, TaskEntity task) {
        ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager();
        ExecutionEntity rootExecution=  executionEntityManager.findChildExecutionsByParentExecutionId(processId).get(0);


        CommandContextUtil.getTaskService().deleteTask(task, true);

        List<ExecutionEntity> executionEntityList= executionEntityManager.findChildExecutionsByParentExecutionId(rootExecution.getId());
        for(ExecutionEntity executionEntity:executionEntityList){
            List<ExecutionEntity> executionEntityList2= executionEntityManager.findChildExecutionsByParentExecutionId(executionEntity.getId());

            for(ExecutionEntity executionEntity2:executionEntityList2){
                CommandContextUtil.getTaskService().deleteTasksByExecutionId(executionEntity2.getId());
                executionEntityManager.deleteChildExecutions(executionEntity2,"delete",true);
                executionEntityManager.delete(executionEntity2);
                CommandContextUtil.getVariableService().deleteVariablesByExecutionId(executionEntity2.getId());
            }
            CommandContextUtil.getTaskService().deleteTasksByExecutionId(executionEntity.getId());
            executionEntityManager.deleteChildExecutions(executionEntity,"delete",true);
            executionEntityManager.delete(executionEntity);
            CommandContextUtil.getVariableService().deleteVariablesByExecutionId(executionEntity.getId());
        }




        Process process = ProcessDefinitionUtil.getProcess(rootExecution.getProcessDefinitionId());
        FlowElement targetFlowElement = process.getFlowElement(targetNodeId);
        rootExecution.setCurrentFlowElement(targetFlowElement);
        FlowableEngineAgenda agenda = CommandContextUtil.getAgenda();
        agenda.planContinueProcessInCompensation(rootExecution);

        return true;
    }


}

看完代码,我们再说说过程,流程实例的驱动主要是靠表act_ru_execution来驱动的。前后跳转主要操作以下步骤:

  • 清除相关任务(act_ru_task)
  • 清除局部变量(act_ru_variable),注意是局部变量
  • 清除轨迹(act_ru_execution)
  • 保留act_ru_execution到只剩下两条记录再往下驱动流程.
    注意:activti自由跳转也是同样的道理.
    有朋友私信我,说我的代码不支持并行分支的驳回,这个确实是这样。
    但并行分支的驳回有两种。
  1. 分支内的驳回,即驳回前有多少条分支,驳回后还是有多少条分支,研究一下act_ru_execution,看清哪些数据再处理
  2. 分支外的驳回,即原来有5条分支,可能驳回后只有一条。这样的话,还是删除掉act_ru_execution到只剩下两条记录即可.
    后续有空,我再考虑这两种驳回吧!
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,653评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,480评论 2 59
  • flowable是一个用Java写的轻量级商业流程引擎,用它可以部署BPMN2.0(在工业界被广泛接受的XML标准...
    Acamy丶阅读 28,717评论 9 42
  • 2018年10月10日 今天又过了充实的一天,人为什么一定要传播正能量,物理学告诉我们,自然界是有磁场的。...
    微笑一生01阅读 1,540评论 2 2
  • 其实到底哪个环节出了问题呢,零散地记录一点过往吧。从小,很多事要是自己能处理,就不会告诉父母知道了,我爸爸...
    蘅芷清秋阅读 1,384评论 0 0