在前一章《Activiti Spring集成Demo》的基础上进行测试
注意上图中Id为serviceTaskProcess,后边的流程实例要根据这个Id获取,才能执行流程。
在上图中点击Service Task1,见下图
将Service Task1 的处理逻辑Class设置为com.lei.demo.activiti.spring.service.task.ServiceTask1,
其他的Service Task2,3,4类似,xml如下
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test"> <process id="serviceTaskProcess" name="My process" isExecutable="true"> <startEvent id="startevent1" name="Start"></startEvent> <parallelGateway id="parallelgateway1" name="Parallel Gateway1"></parallelGateway> <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="parallelgateway1"></sequenceFlow> <serviceTask id="servicetask1" name="Service Task1" activiti:class="com.lei.demo.activiti.spring.service.task.ServiceTask1"></serviceTask> <serviceTask id="servicetask2" name="Service Task2" activiti:class="com.lei.demo.activiti.spring.service.task.ServiceTask2"></serviceTask> <serviceTask id="servicetask3" name="Service Task3" activiti:class="com.lei.demo.activiti.spring.service.task.ServiceTask3"></serviceTask> <sequenceFlow id="flow2" sourceRef="parallelgateway1" targetRef="servicetask1"></sequenceFlow> <sequenceFlow id="flow3" sourceRef="parallelgateway1" targetRef="servicetask2"></sequenceFlow> <sequenceFlow id="flow4" sourceRef="parallelgateway1" targetRef="servicetask3"></sequenceFlow> <parallelGateway id="parallelgateway2" name="Parallel Gateway2"></parallelGateway> <sequenceFlow id="flow5" sourceRef="servicetask2" targetRef="parallelgateway2"></sequenceFlow> <sequenceFlow id="flow6" sourceRef="servicetask1" targetRef="parallelgateway2"></sequenceFlow> <serviceTask id="servicetask4" name="Service Task4" activiti:class="com.lei.demo.activiti.spring.service.task.ServiceTask4"></serviceTask> <sequenceFlow id="flow7" sourceRef="parallelgateway2" targetRef="servicetask4"></sequenceFlow> <parallelGateway id="parallelgateway3" name="Parallel Gateway3"></parallelGateway> <sequenceFlow id="flow8" sourceRef="servicetask4" targetRef="parallelgateway3"></sequenceFlow> <sequenceFlow id="flow9" sourceRef="servicetask3" targetRef="parallelgateway3"></sequenceFlow> <endEvent id="endevent1" name="End"></endEvent> <sequenceFlow id="flow10" sourceRef="parallelgateway3" targetRef="endevent1"></sequenceFlow> </process> </definitions>
ServiceTask1.java如下,必须实现org.activiti.engine.delegate.JavaDelegate接口
package com.lei.demo.activiti.spring.service.task; import java.util.logging.Logger; import org.activiti.engine.delegate.DelegateExecution; import org.activiti.engine.delegate.JavaDelegate; public class ServiceTask1 implements JavaDelegate { private final Logger log = Logger.getLogger(ServiceTask1.class.getName()); @Override public void execute(DelegateExecution execution) throws Exception { Thread.sleep(10000); log.info("variavles=" + execution.getVariables()); execution.setVariable("task1", "I am task 1"); log.info("I am task 1."); } }其他的ServiceTask相似
ServiceTask2.java
package com.lei.demo.activiti.spring.service.task; import java.util.logging.Logger; import org.activiti.engine.delegate.DelegateExecution; import org.activiti.engine.delegate.JavaDelegate; public class ServiceTask2 implements JavaDelegate { private final Logger log = Logger.getLogger(ServiceTask2.class.getName()); @Override public void execute(DelegateExecution execution) throws Exception { Thread.sleep(10000); log.info("variavles=" + execution.getVariables()); execution.setVariable("task2", "I am task 2"); log.info("I am task 2."); } }ServiceTask3.java
package com.lei.demo.activiti.spring.service.task; import java.util.logging.Logger; import org.activiti.engine.delegate.DelegateExecution; import org.activiti.engine.delegate.JavaDelegate; public class ServiceTask3 implements JavaDelegate { private final Logger log = Logger.getLogger(ServiceTask3.class.getName()); @Override public void execute(DelegateExecution execution) throws Exception { Thread.sleep(10000); log.info("variavles=" + execution.getVariables()); execution.setVariable("task3", "I am task 3"); log.info("I am task 3."); } }ServiceTask4.java
package com.lei.demo.activiti.spring.service.task; import java.util.logging.Logger; import org.activiti.engine.delegate.DelegateExecution; import org.activiti.engine.delegate.JavaDelegate; public class ServiceTask4 implements JavaDelegate { private final Logger log = Logger.getLogger(ServiceTask4.class.getName()); @Override public void execute(DelegateExecution execution) throws Exception { Thread.sleep(10000); log.info("variavles=" + execution.getVariables()); execution.setVariable("task4", "I am task 4"); log.info("I am task 4."); } }
测试类AutomaticTaskTest.java
package com.lei.demo.activiti.spring.service.task; import org.activiti.engine.ProcessEngine; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.runtime.ProcessInstance; import org.junit.Assert; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AutomaticTaskTest { private static ApplicationContext context; private static ProcessEngine processEngine; private static RepositoryService repositoryService; private static RuntimeService runtimeService; private static String deploymentId; public static void main(String[] args) throws Exception { System.out.println( "start" ); AutomaticTaskTest.initialize(); AutomaticTaskTest.test(); System.out.println( "end" ); } public static void test() { ProcessInstance pi = runtimeService.startProcessInstanceByKey("serviceTaskProcess"); System.out.println("ProcessInstance Id:"+pi.getId()); System.out.println("ProcessInstance ActivitiId:"+pi.getActivityId()); Assert.assertEquals(true, pi.isEnded()); } protected static void initialize() throws Exception { context = new ClassPathXmlApplicationContext("spring-activiti.xml"); System.out.println("创建Activiti流程引擎,Activiti主接口"); processEngine = (ProcessEngine) context.getBean("processEngine"); System.out.println("Activiti主接口创建完毕,processEngine Name:"+processEngine.getName()); System.out.println("获取Activiti的资源操作接口--开始"); repositoryService = processEngine.getRepositoryService(); System.out.println("获取Activiti的资源操作接口--完毕"); System.out.println("获得运行时操作接口--开始"); runtimeService = processEngine.getRuntimeService(); System.out.println("获得运行时操作接口--完毕"); System.out.println("部署流程,创建数据库表--开始"); deploymentId = repositoryService.createDeployment() .addClasspathResource("diagrams/ServiceTaskProcess.bpmn") .deploy().getId(); System.out.println("部署流程,创建数据库表--完毕"); } protected static void destroy() throws Exception { repositoryService.deleteDeployment(deploymentId, true); } }