Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3351858
  • 博文数量: 530
  • 博客积分: 13360
  • 博客等级: 上将
  • 技术积分: 5473
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-13 13:32
文章分类

全部博文(530)

文章存档

2017年(1)

2015年(2)

2013年(24)

2012年(20)

2011年(97)

2010年(240)

2009年(117)

2008年(12)

2007年(8)

2006年(9)

分类: Java

2013-08-01 15:13:20

1.概述
    自动执行是指,在启动流程之前,准备流程所需要的控制流程进度的变量数据,启动流程之后,无需外部干预,就能够按照预定义的流程执行;

2.基于junit3的测试抽象类
    该类用于测试覆用,在测试之前做一些初始化工作,主要包括流程引擎实例的构建,及其流程提供的基本服务。

点击(此处)折叠或打开

  1. package com.autoexcute;

  2. import junit.framework.TestCase;

  3. import org.activiti.engine.FormService;
  4. import org.activiti.engine.HistoryService;
  5. import org.activiti.engine.IdentityService;
  6. import org.activiti.engine.ManagementService;
  7. import org.activiti.engine.ProcessEngine;
  8. import org.activiti.engine.ProcessEngines;
  9. import org.activiti.engine.RepositoryService;
  10. import org.activiti.engine.RuntimeService;
  11. import org.activiti.engine.TaskService;
  12.   
  13. /**
  14.  * @author shirdrn
  15.  */
  16. public abstract class AbstractTest extends TestCase {
  17.   
  18.     private ProcessEngine processEngine;
  19.     protected String deploymentId;
  20.     protected RepositoryService repositoryService;
  21.     protected RuntimeService runtimeService;
  22.     protected TaskService taskService;
  23.     protected FormService formService;
  24.     protected HistoryService historyService;
  25.     protected IdentityService identityService;
  26.     protected ManagementService managementService;
  27.       
  28.     @Override
  29.     protected void setUp() throws Exception {
  30.         super.setUp();
  31.         if(processEngine==null) {
  32.             processEngine = ProcessEngines.getDefaultProcessEngine();
  33.         }
  34.         repositoryService = processEngine.getRepositoryService();
  35.         runtimeService = processEngine.getRuntimeService();
  36.         taskService = processEngine.getTaskService();
  37.         formService = processEngine.getFormService();
  38.         historyService = processEngine.getHistoryService();
  39.         identityService = processEngine.getIdentityService();
  40.         managementService = processEngine.getManagementService();
  41.         initialize();
  42.     }
  43.       
  44.     @Override
  45.     protected void tearDown() throws Exception {
  46.         super.tearDown();
  47.         destroy();
  48.     }
  49.       
  50.     protected abstract void initialize() throws Exception;
  51.       
  52.     protected abstract void destroy() throws Exception;
  53. }

3.bpmn中的自动执行元素serviceTask
语法如下所示:
   


activiti:class属性为该结点对应的处理类,该类要求实现org.activiti.engine.delegate.JavaDelegate接口,该接口定义如下所示:
  1. package org.activiti.engine.delegate;
  2.   
  3. public interface JavaDelegate {
  4.     
  5.   void execute(DelegateExecution execution) throws Exception;
  6. }
execute方法的参数DelegateExecution execution可以在流程中各个结点之间传递流程变量。


4.bpmn文件
点击(此处)折叠或打开
5.自定义ServiceTask类
    定义了4个类,这些类只代码基本相同,读取流程变量中的值,并向流程变量赋值

点击(此处)折叠或打开

  1. package com.autoexcute;

  2. import java.util.logging.Logger;

  3. import org.activiti.engine.delegate.DelegateExecution;
  4. import org.activiti.engine.delegate.JavaDelegate;
  5.   
  6. public class ServiceTask1 implements JavaDelegate {
  7.   
  8.     private final Logger log = Logger.getLogger(ServiceTask1.class.getName());
  9.   
  10.     @Override
  11.     public void execute(DelegateExecution execution) throws Exception {
  12.         Thread.sleep(10000);
  13.         log.info("variavles=" + execution.getVariables());
  14.         execution.setVariable("task1", "I am task 1");
  15.         log.info("I am task 1.");
  16.     }
  17. }

  18. public class ServiceTask2 implements JavaDelegate {
  19.   
  20.     private final Logger log = Logger.getLogger(ServiceTask2.class.getName());
  21.   
  22.     @Override
  23.     public void execute(DelegateExecution execution) throws Exception {
  24.         Thread.sleep(10000);
  25.         log.info("variavles=" + execution.getVariables());
  26.         execution.setVariable("task2", "I am task 2");
  27.         log.info("I am task 2.");
  28.     }
  29. }

  30. public class ServiceTask3 implements JavaDelegate {
  31.     
  32.     private final Logger log = Logger.getLogger(ServiceTask3.class.getName());
  33.   
  34.     @Override
  35.     public void execute(DelegateExecution execution) throws Exception {
  36.         Thread.sleep(10000);
  37.         log.info("variavles=" + execution.getVariables());
  38.         execution.setVariable("task3", "I am task 3");
  39.         log.info("I am task 3.");
  40.     }
  41. }

  42. public class ServiceTask4 implements JavaDelegate {
  43.   
  44.     private final Logger log = Logger.getLogger(ServiceTask4.class.getName());
  45.   
  46.     @Override
  47.     public void execute(DelegateExecution execution) throws Exception {
  48.         Thread.sleep(10000);
  49.         log.info("variavles=" + execution.getVariables());
  50.         execution.setVariable("task4", "I am task 4");
  51.         log.info("I am task 4.");
  52.     }
  53. }


点击(此处)折叠或打开

  1. package com.autoexcute;

  2. import java.util.logging.Logger;

  3. import org.activiti.engine.delegate.DelegateExecution;
  4. import org.activiti.engine.delegate.JavaDelegate;
  5.   
  6. public class ServiceTask1 implements JavaDelegate {
  7.   
  8.     private final Logger log = Logger.getLogger(ServiceTask1.class.getName());
  9.   
  10.     @Override
  11.     public void execute(DelegateExecution execution) throws Exception {
  12.         Thread.sleep(10000);
  13.         log.info("variavles=" + execution.getVariables());
  14.         execution.setVariable("task1", "I am task 1");
  15.         log.info("I am task 1.");
  16.     }
  17. }

  18. public class ServiceTask2 implements JavaDelegate {
  19.   
  20.     private final Logger log = Logger.getLogger(ServiceTask2.class.getName());
  21.   
  22.     @Override
  23.     public void execute(DelegateExecution execution) throws Exception {
  24.         Thread.sleep(10000);
  25.         log.info("variavles=" + execution.getVariables());
  26.         execution.setVariable("task2", "I am task 2");
  27.         log.info("I am task 2.");
  28.     }
  29. }

  30. public class ServiceTask3 implements JavaDelegate {
  31.     
  32.     private final Logger log = Logger.getLogger(ServiceTask3.class.getName());
  33.   
  34.     @Override
  35.     public void execute(DelegateExecution execution) throws Exception {
  36.         Thread.sleep(10000);
  37.         log.info("variavles=" + execution.getVariables());
  38.         execution.setVariable("task3", "I am task 3");
  39.         log.info("I am task 3.");
  40.     }
  41. }

  42. public class ServiceTask4 implements JavaDelegate {
  43.   
  44.     private final Logger log = Logger.getLogger(ServiceTask4.class.getName());
  45.   
  46.     @Override
  47.     public void execute(DelegateExecution execution) throws Exception {
  48.         Thread.sleep(10000);
  49.         log.info("variavles=" + execution.getVariables());
  50.         execution.setVariable("task4", "I am task 4");
  51.         log.info("I am task 4.");
  52.     }
  53. }
6.测试类
点击(此处)折叠或打开
  1. package com.autoexcute;

  2. import org.activiti.engine.runtime.ProcessInstance;
  3. import org.activiti.engine.test.Deployment;
  4. import com.autoexcute.AbstractTest;
  5.   
  6.   
  7. public class AutomaticParallelGatewayTest extends AbstractTest {
  8.   
  9.     private String deploymentId;
  10.       
  11.     @Override
  12.     protected void initialize() throws Exception {
  13.         deploymentId = repositoryService.createDeployment()
  14.             .addClasspathResource("com/autoexcute/AutomaticForkJoin.bpmn")
  15.             .deploy().getId();
  16.     }
  17.   
  18.     @Override
  19.     protected void destroy() throws Exception {
  20.         repositoryService.deleteDeployment(deploymentId, true);
  21.     }
  22.       
  23.     @Deployment
  24.     public void testForkJoin() {
  25.         ProcessInstance pi = runtimeService.startProcessInstanceByKey("AutomaticParalellBasedForkJoin");
  26.         assertEquals(true, pi.isEnded());
  27.     }
  28. }

7.执行结果

点击(此处)折叠或打开

  1. 八月 01, 2013 2:42:26 下午 com.autoexcute.ServiceTask1 execute
  2. 信息: variavles={}
  3. 八月 01, 2013 2:42:26 下午 com.autoexcute.ServiceTask1 execute
  4. 信息: I am task 1.
  5. 八月 01, 2013 2:42:36 下午 com.autoexcute.ServiceTask2 execute
  6. 信息: variavles={task1=I am task 1}
  7. 八月 01, 2013 2:42:36 下午 com.autoexcute.ServiceTask2 execute
  8. 信息: I am task 2.
  9. 八月 01, 2013 2:42:46 下午 com.autoexcute.ServiceTask4 execute
  10. 信息: variavles={task1=I am task 1, task2=I am task 2}
  11. 八月 01, 2013 2:42:46 下午 com.autoexcute.ServiceTask4 execute
  12. 信息: I am task 4.
  13. 八月 01, 2013 2:42:56 下午 com.autoexcute.ServiceTask3 execute
  14. 信息: variavles={task1=I am task 1, task2=I am task 2, task4=I am task 4}
  15. 八月 01, 2013 2:42:56 下午 com.autoexcute.ServiceTask3 execute
  16. 信息: I am task 3.

参考文献
1.Activiti 5.3:流程活动自动与手工触发执行. http://blog.csdn.net/shirdrn/article/details/6270506

阅读(14488) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~