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));