JBPM4 流程回退实现方法

在JBPM4流程设计中,让让当前节点回退到该节点以前的任意节点,如图,

JBPM4 流程回退实现方法

如果想从task3回到task1,可以使用的方法有: 

①创建一个Transition对象,从task3到task1 

/**
	 * 动态创建连接当前任务节点至名称为destName的节点的Transition
	 * 
	 * @param taskId
	 *            任务节点ID
	 * @param sourceName
	 *            源节点名称
	 * @param desName
	 *            目标节点名称
	 */
	public void addOutTransition(ProcessDefinitionImpl pd, String sourceName, String desName) {
		ProcessEngine pe = Configuration.getProcessEngine();
		EnvironmentFactory ef = (EnvironmentFactory) pe;
		EnvironmentImpl evti = null;
		try {
			evti = ef.openEnvironment();

			ActivityImpl sourceActivity = pd.getActivity(sourceName);

			TransitionImpl tran = sourceActivity.createOutgoingTransition();
			ActivityImpl desActivity = pd.getActivity(desName);

			tran.setName("to" + desName);
			tran.setDestination(desActivity);

			sourceActivity.addOutgoingTransition(tran);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (evti != null)
				evti.close();
		}

	}

	/**
	 * 动态删除连接sourceName与destName的Transition
	 * 
	 * @param taskId
	 * @param sourceName
	 * @param desName
	 */
	public void removeOutTransition(ProcessDefinitionImpl pd, String sourceName, String destName) {
		EnvironmentFactory environmentFactory = (EnvironmentFactory) processEngine;
		EnvironmentImpl env = null;
		try {
			env = environmentFactory.openEnvironment();
			// 取得当前流程的活动定义
			ActivityImpl sourceActivity = pd.findActivity(sourceName);

			// 若存在这个连接,则需要把该连接删除
			List<Transition> trans = (List<Transition>) sourceActivity.getOutgoingTransitions();
			for (Transition tran : trans) {
				if (destName.equals(tran.getDestination().getName())) {// 删除该连接
					trans.remove(tran);
					break;
				}
			}
		} catch (Exception ex) {

		} finally {
			if (env != null)
				env.close();
		}
	}

这样再通过take(), completeTask()等方法就可以回退了。

②调用ProcessEngin.execute()方法 
③调用ExecutionImpl.moveTo()方法 
推荐使用第一种

你可能感兴趣的:(jbpm4)