1.概述
自动执行是指,在启动流程之前,准备流程所需要的控制流程进度的变量数据,启动流程之后,无需外部干预,就能够按照预定义的流程执行;
2.基于junit3的测试抽象类
该类用于测试覆用,在测试之前做一些初始化工作,主要包括流程引擎实例的构建,及其流程提供的基本服务。
-
package com.autoexcute;
-
-
import junit.framework.TestCase;
-
-
import org.activiti.engine.FormService;
-
import org.activiti.engine.HistoryService;
-
import org.activiti.engine.IdentityService;
-
import org.activiti.engine.ManagementService;
-
import org.activiti.engine.ProcessEngine;
-
import org.activiti.engine.ProcessEngines;
-
import org.activiti.engine.RepositoryService;
-
import org.activiti.engine.RuntimeService;
-
import org.activiti.engine.TaskService;
-
-
/**
-
* @author shirdrn
-
*/
-
public abstract class AbstractTest extends TestCase {
-
-
private ProcessEngine processEngine;
-
protected String deploymentId;
-
protected RepositoryService repositoryService;
-
protected RuntimeService runtimeService;
-
protected TaskService taskService;
-
protected FormService formService;
-
protected HistoryService historyService;
-
protected IdentityService identityService;
-
protected ManagementService managementService;
-
-
@Override
-
protected void setUp() throws Exception {
-
super.setUp();
-
if(processEngine==null) {
-
processEngine = ProcessEngines.getDefaultProcessEngine();
-
}
-
repositoryService = processEngine.getRepositoryService();
-
runtimeService = processEngine.getRuntimeService();
-
taskService = processEngine.getTaskService();
-
formService = processEngine.getFormService();
-
historyService = processEngine.getHistoryService();
-
identityService = processEngine.getIdentityService();
-
managementService = processEngine.getManagementService();
-
initialize();
-
}
-
-
@Override
-
protected void tearDown() throws Exception {
-
super.tearDown();
-
destroy();
-
}
-
-
protected abstract void initialize() throws Exception;
-
-
protected abstract void destroy() throws Exception;
-
}
3.bpmn中的自动执行元素serviceTask
语法如下所示:
activiti:class属性为该结点对应的处理类,该类要求实现org.activiti.engine.delegate.JavaDelegate接口,该接口定义如下所示:
-
package org.activiti.engine.delegate;
-
-
public interface JavaDelegate {
-
-
void execute(DelegateExecution execution) throws Exception;
-
}
execute方法的参数DelegateExecution execution可以在流程中各个结点之间传递流程变量。
4.bpmn文件
点击(此处)折叠或打开
5.自定义ServiceTask类
定义了4个类,这些类只代码基本相同,读取流程变量中的值,并向流程变量赋值
-
package com.autoexcute;
-
-
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.");
-
}
-
}
-
-
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.");
-
}
-
}
-
-
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.");
-
}
-
}
-
-
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.");
-
}
-
}
-
package com.autoexcute;
-
-
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.");
-
}
-
}
-
-
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.");
-
}
-
}
-
-
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.");
-
}
-
}
-
-
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.");
-
}
-
}
6.测试类
点击(此处)折叠或打开
-
package com.autoexcute;
-
-
import org.activiti.engine.runtime.ProcessInstance;
-
import org.activiti.engine.test.Deployment;
-
import com.autoexcute.AbstractTest;
-
-
-
public class AutomaticParallelGatewayTest extends AbstractTest {
-
-
private String deploymentId;
-
-
@Override
-
protected void initialize() throws Exception {
-
deploymentId = repositoryService.createDeployment()
-
.addClasspathResource("com/autoexcute/AutomaticForkJoin.bpmn")
-
.deploy().getId();
-
}
-
-
@Override
-
protected void destroy() throws Exception {
-
repositoryService.deleteDeployment(deploymentId, true);
-
}
-
-
@Deployment
-
public void testForkJoin() {
-
ProcessInstance pi = runtimeService.startProcessInstanceByKey("AutomaticParalellBasedForkJoin");
-
assertEquals(true, pi.isEnded());
-
}
-
}
7.执行结果
-
八月 01, 2013 2:42:26 下午 com.autoexcute.ServiceTask1 execute
-
信息: variavles={}
-
八月 01, 2013 2:42:26 下午 com.autoexcute.ServiceTask1 execute
-
信息: I am task 1.
-
八月 01, 2013 2:42:36 下午 com.autoexcute.ServiceTask2 execute
-
信息: variavles={task1=I am task 1}
-
八月 01, 2013 2:42:36 下午 com.autoexcute.ServiceTask2 execute
-
信息: I am task 2.
-
八月 01, 2013 2:42:46 下午 com.autoexcute.ServiceTask4 execute
-
信息: variavles={task1=I am task 1, task2=I am task 2}
-
八月 01, 2013 2:42:46 下午 com.autoexcute.ServiceTask4 execute
-
信息: I am task 4.
-
八月 01, 2013 2:42:56 下午 com.autoexcute.ServiceTask3 execute
-
信息: variavles={task1=I am task 1, task2=I am task 2, task4=I am task 4}
-
八月 01, 2013 2:42:56 下午 com.autoexcute.ServiceTask3 execute
-
信息: I am task 3.
参考文献
1.Activiti 5.3:流程活动自动与手工触发执行. http://blog.csdn.net/shirdrn/article/details/6270506
阅读(14488) | 评论(0) | 转发(0) |