在前一章《Activiti Spring集成Demo》的基础上进行测试
1.创建流程图
注意上图中Id为serviceTaskProcess,后边的流程实例要根据这个Id获取,才能执行流程。
在上图中点击Service Task1,见下图
将Service Task1 的处理逻辑Class设置为com.lei.demo.activiti.spring.service.task.ServiceTask1,
其他的Service Task2,3,4类似,xml如下
2.编写处理逻辑的Class
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.");
}
}
3.测试类
测试类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);
}
}