此实例是一个最简单的在运行时人为动态改变流程运转的实例,意在为任意流、驳回等功能抛砖引玉.
定义一个最简单的流程,它只有一个“开始事件”、一个“ServiceTask”、一个“结束事件”.
我们现在的要实现的是直接跳过ServiceTask。
ServiceTask的实现类只是简单的在控制台输出一条语句.
public class Log implements JavaDelegate { public void execute(DelegateExecution execution) throws Exception { System.err.println("如果我出现了,就说明我没被忽略"); } }
System.out.println("直接跳过ServiceTask的流程开始.................."); RepositoryService repositoryService = activitiRule.getRepositoryService(); ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId("1").singleResult(); ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService) .getDeployedProcessDefinition(processDefinition.getId()); List<ActivityImpl> activities = processDefinitionEntity.getActivities(); ActivityImpl start = null; ActivityImpl task = null; ActivityImpl end = null; for (ActivityImpl activity : activities) { if (activity.getId().equals("startevent1")) { start = activity; } if (activity.getId().equals("usertask1")) { task = activity; } if (activity.getId().equals("endevent1")) { end = activity; } } //清除原先的路径 List<PvmTransition> cacheTran = start.getOutgoingTransitions(); start.getOutgoingTransitions().clear(); //创建新路径 TransitionImpl tran = start.createOutgoingTransition(); tran.setDestination(end); RuntimeService runtimeService = activitiRule.getRuntimeService(); runtimeService.startProcessInstanceById(processDefinitionEntity.getProcessDefinition().getId()); //恢复原来路径 start.getOutgoingTransitions().clear(); for (PvmTransition transition : cacheTran) { start.createOutgoingTransition().setDestination((ActivityImpl) transition.getDestination()); }这样ServiceTask将被跳过不会自动执行
当注释掉
start.getOutgoingTransitions().clear();
serviceTask将被执行.
这种方法只会影响一个流程实例的流程定义的内存镜像,其他流程不会受到影响.