activiti退回到任意节点实例,不支持并行网关

简介:

activiti在设计的时候没有回退相关的操作,回退是中国特有的特色。这里写一个比较简单的回退。不支持回退到并行网关前面节点,虽然回退到前面节点不会报错
但会导致任务无法结束。使用没有并行网关的回退。
1.测试类
package com.test.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.TaskServiceImpl;
import org.activiti.engine.task.Task;
import org.junit.Test;

import com.ncq.core.JumpTaskCmd;

public class TestA {
	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	
	/**
	 * 驳回任务到某个节点,不适用并行节点
	 * 虽然并行节点回退不会报错,但楼主经过测试发现,最后会导致任务无法结束
	 */
	@Test
	public void backTask(){
		String taskId= "172502";
		TaskService taskService = processEngine.getTaskService();
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		
		String executionId=task.getExecutionId();//得到流程实例ID
		String activityId="usertask1";//要驳回到哪个任务节点
		TaskServiceImpl taskServiceImpl=(TaskServiceImpl)taskService;
		taskServiceImpl.getCommandExecutor().execute(new JumpTaskCmd(executionId, activityId));
	}
}

2.相关类
package com.ncq.core;

import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;
import org.activiti.engine.task.Comment;
/**
 * 驳回到任务的某个节点,不适用并行网关
 * @author Administrator
 *
 */
public class JumpTaskCmd implements Command{

	protected String executionId;
	protected String activityId;
	
	
	public JumpTaskCmd(String executionId, String activityId) {
		this.executionId = executionId;
		this.activityId = activityId;
	}
	
	@Override
	public Comment execute(CommandContext arg0) {
		for (TaskEntity taskEntity : Context.getCommandContext().getTaskEntityManager().findTasksByExecutionId(executionId)) {
	    	Context.getCommandContext().getTaskEntityManager().deleteTask(taskEntity, "jump", false);
	    }
	    ExecutionEntity executionEntity = Context.getCommandContext().getExecutionEntityManager().findExecutionById(executionId);
	    ProcessDefinitionImpl processDefinition = executionEntity.getProcessDefinition();
	    ActivityImpl activity = processDefinition.findActivity(activityId);
	    executionEntity.executeActivity(activity);
		return null;
	}

}
3.activit.cfg.xml


  	
  	
  		
  		
  		
  		
  		
  		
  		
  	




你可能感兴趣的:(activiti)