activiti高并发情况下回退


public class JDJumpTaskCmd implements Command<Void> {
    protected String executionId;
    protected List<ActivityImpl> desActivities;
    protected Map<String, Object> paramvar;
    protected ActivityImpl currentActivity;
    protected String instanceId;
    protected TaskService taskService;
    protected RedisCache redisCache;

    public Void execute(CommandContext commandContext) {
        ExecutionEntityManager executionEntityManager = Context
                .getCommandContext().getExecutionEntityManager();
        // 获取当前流程的executionId,因为在并发的情况下executionId是唯一的。
        ExecutionEntity executionEntity = executionEntityManager
                .findExecutionById(executionId);
        executionEntity.setVariables(paramvar);
        executionEntity.setEventSource(this.currentActivity);
        executionEntity.setActivity(this.currentActivity);
        // 根据executionId 获取Task
        Iterator<TaskEntity> localIterator = Context.getCommandContext()
                .getTaskEntityManager()
                .findTasksByProcessInstanceId(instanceId).iterator();

        while (localIterator.hasNext()) {
            TaskEntity taskEntity = (TaskEntity) localIterator.next();
            if(taskEntity.getExecutionId().equals(executionId)){
                taskEntity.removeVariablesLocal();
                // 触发任务监听
                taskEntity.fireEvent("complete");
            }

            // 删除任务的原因
            Context.getCommandContext().getTaskEntityManager()
                    .deleteTask(taskEntity, "completed", false);
        }
        //如果并行,有多个activiti
        for(ActivityImpl activity:desActivities){
            executionEntity.executeActivity(activity);
        }
        return null;
    }

    /**
     * 构造参数 可以根据自己的业务需要添加更多的字段
     * @param task
     * @param nodeIds
     * @param paramvar
     * @param currentActivity
     */
    public JDJumpTaskCmd(Task task,List<String> nodeIds,Map<String, Object> paramvar,
                         ActivityImpl currentActivity, String instanceId,TaskService taskService) {

        this.executionId = task.getExecutionId();
        this.paramvar = paramvar;
        this.currentActivity = currentActivity;
        this.instanceId = instanceId;
        this.desActivities = KmcActivitiUtil.getnextActivities(task.getProcessDefinitionId(),nodeIds);
        this.taskService = taskService;

        if(redisCache == null){
            redisCache = SpringContextHolder.getBean("redisCache");
            redisCache.add(WorkFlowConstant.REDIS_BACK_KEY_PRE+task.getProcessInstanceId()+task.getTaskDefinitionKey(), (Serializable) nodeIds,5*60);
        }
    }
}

 

测试代码:

            ActivityImpl currentActivity = KmcActivitiUtil.getActivitiImp(task.getProcessDefinitionId(),task.getTaskDefinitionKey());
            List<String> nodeIds = KmcActivitiUtil.getBrotherNodes(nodeId,task);

            TaskServiceImpl taskServiceImpl=(TaskServiceImpl)taskService;
            Map<String, Object> vars = new HashMap<String, Object>();
            vars.put("msg",backToBeforeTaskReq.getMsg());
            vars.put("userId",backToBeforeTaskReq.getUserId());
            CommandExecutor commandExecutor = taskServiceImpl.getCommandExecutor();

            //将变量数据删除,否则会签失效
            jdbcTemplate.execute("delete from ACT_RU_VARIABLE where PROC_INST_ID_ ="+task.getProcessInstanceId());

            commandExecutor.execute(new JDJumpTaskCmd(task,nodeIds, vars, currentActivity,task.getProcessInstanceId(),taskService));

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值