如在流程定义文件中:
- <process id="process1" name="process1" isExecutable="true">
- <startEvent id="startevent1" name="Start"></startEvent>
- <sequenceFlow id="flow1" name="" sourceRef="startevent1" targetRef="serviceTask1"></sequenceFlow>
- <serviceTask id="serviceTask1" name="serviceTask1" activiti:class="com.activiti.testcase.turn.ServiceTask1Listener"/>
- <sequenceFlow id="s2" sourceRef="serviceTask1" targetRef="userTask2" ></sequenceFlow>
- <sequenceFlow id="s3" sourceRef="serviceTask1" targetRef="manualTask1" ></sequenceFlow>
- <userTask id="userTask2" name="userTask2" activiti:assignee="gonzo"></userTask>
- <manualTask id="manualTask1" name="manualTask1"></manualTask>
- <sequenceFlow id="s4" targetRef="userTask2" sourceRef="endevent1"></sequenceFlow>
- <sequenceFlow id="s5" targetRef="manualTask1" sourceRef="endevent1"></sequenceFlow>
- <endEvent id="endevent1" name="End"></endEvent>
- </process>
其中serviceTask 的类中是实现流程转向的具体代码。
首先说明:要使用serviceTask调用外部的java类,则该java类必须实现JavaDelegate或ActivityBehavior接口,若需要控制流程的转向则需实现 ActivityBehavior接口,因为该接口功能更强大。
来看ServiceTask1Listener类,该类只有一个execute方法:
- public class ServiceTask1Listener implements ActivityBehavior {
- public void execute(ActivityExecution execution) throws Exception {
- //流程定义id
- String procDefId = execution.getProcessDefinitionId();
- RepositoryServiceImpl repositoryService = (RepositoryServiceImpl)execution.getEngineServices().getRepositoryService();
- //流程定义的实现
- ProcessDefinitionImpl processDefinitionImpl = (ProcessDefinitionImpl)repositoryService.getDeployedProcessDefinition(procDefId);
- //当前执行的活动节点
- ActivityImpl ac = (ActivityImpl)execution.getActivity();
- //创建输出的执行路径
- TransitionImpl transition = ac.createOutgoingTransition("flow1");
- //设置输出路径的目标节点
- ActivityImpl destination = new ActivityImpl("serviceTask1", processDefinitionImpl);
- destination.setActivityBehavior(this);
- //设置目标节点
- transition.setDestination(destination);
- List<PvmTransition> pvms = execution.getActivity().getOutgoingTransitions();
- //[ s2,s3,flow1]
- //选用刚刚创建的执行路径
- execution.take(pvms.get(2));
- }
- }