Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3334439
  • 博文数量: 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 16:18:50

1.概述
    手工触发执行是指,执行到流程中某个个结点后流程暂时停止运行,直到收到外部发送的信号以后,才会继续向前推进,这样情况可以更加精细地控制流程。

2.ReceiveTask
    ReceiveTask只是一个简单的使流程进入等待状态的元素。直到引擎接收到明确的触发信息继续执行。

定义一个流程文件

点击(此处)折叠或打开

上述流程定义中,对应的两个处理类,代码分别如下所示:

点击(此处)折叠或打开

  1. package com.manexcute;

  2. import java.util.HashMap;
  3. import java.util.logging.Logger;
  4.   
  5. import org.activiti.engine.delegate.DelegateExecution;
  6. import org.activiti.engine.delegate.JavaDelegate;
  7.   
  8. public class CheckBankReceiveTask implements JavaDelegate {
  9.   
  10.     private final Logger log = Logger.getLogger(CheckBankReceiveTask.class.getName());
  11.   
  12.     @SuppressWarnings("unchecked")
  13.     @Override
  14.     public void execute(DelegateExecution execution) throws Exception {
  15.         log.info("i am CheckBankReceiveTask.");
  16.         System.out.println("in : " + execution.getVariables());
  17.         ((HashMap<String, Object>)execution.getVariables().get("in")).put("next", "CheckBankTask");
  18.         ((HashMap<String, Object>)execution.getVariables().get("out")).put("reponse", "subprocess:CheckBankReceiveTask->CheckMerchantReceiveTask");
  19.     }
  20. }

点击(此处)折叠或打开

  1. package com.manexcute;

  2. import java.util.HashMap;
  3. import java.util.logging.Logger;
  4.   
  5. import org.activiti.engine.delegate.DelegateExecution;
  6. import org.activiti.engine.delegate.JavaDelegate;
  7.   
  8. public class CheckMerchantReceiveTask implements JavaDelegate {
  9.   
  10.     private final Logger log = Logger.getLogger(CheckMerchantReceiveTask.class.getName());
  11.       
  12.     @SuppressWarnings("unchecked")
  13.     @Override
  14.     public void execute(DelegateExecution execution) throws Exception {
  15.         log.info("i am CheckMerchantReceiveTask.");
  16.         System.out.println("in : " + execution.getVariables());
  17.         ((HashMap<String, Object>)execution.getVariables().get("in")).put("previous", "CheckMerchantReceiveTask");
  18.     }
  19. }
测试类中还乃至一个Merchant类,该类必须支持序列化,如下所示:

点击(此处)折叠或打开

  1. package com.manexcute;

  2. import java.io.Serializable;

  3. public class Merchant implements Serializable {
  4.     private static final long serialVersionUID = 1L;
  5.     
  6.     public Merchant(String merchantId, int priority, short serviceType, short status) {
  7.         super();
  8.         this.merchantId = merchantId;
  9.         this.priority = priority;
  10.         this.serviceType = serviceType;
  11.         this.status = status;
  12.     }
  13.     
  14.     public Merchant(String merchantId) {
  15.         this(merchantId, -1, (short)0, (short)0);
  16.     }
  17.     
  18.     private String merchantId;
  19.     private int priority = -1;
  20.     private short serviceType = 0;
  21.     private short status = 0;
  22.     
  23.     public String getMerchantId() {
  24.         return merchantId;
  25.     }
  26.     public void setMerchantId(String merchantId) {
  27.         this.merchantId = merchantId;
  28.     }
  29.     public int getPriority() {
  30.         return priority;
  31.     }
  32.     public void setPriority(int priority) {
  33.         this.priority = priority;
  34.     }
  35.     public short getServiceType() {
  36.         return serviceType;
  37.     }
  38.     public void setServiceType(short serviceType) {
  39.         this.serviceType = serviceType;
  40.     }
  41.     public short getStatus() {
  42.         return status;
  43.     }
  44.     
  45.     public void setStatus(short status) {
  46.         this.status = status;
  47.     }
  48.     
  49.     @Override
  50.     public String toString() {
  51.         return "Merchant[" + merchantId + "]";
  52.     }
  53. }
测试代码

点击(此处)折叠或打开

  1. package com.manexcute;

  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5.   
  6. import org.activiti.engine.repository.Deployment;
  7. import org.activiti.engine.runtime.Execution;
  8. import org.activiti.engine.runtime.ProcessInstance;

  9. import com.autoexcute.AbstractTest;

  10. public class ManexcuteTest extends AbstractTest {
  11.   
  12.     @Override
  13.     protected void initialize() throws Exception {
  14.         Deployment deployment = repositoryService
  15.         .createDeployment()
  16.         .addClasspathResource("com/manexcute/ReceiveTask.bpmn")
  17.         .deploy();
  18.         deploymentId = deployment.getId();
  19.     }
  20.   
  21.     @Override
  22.     protected void destroy() throws Exception {
  23.         repositoryService.deleteDeployment(deploymentId, true);
  24.     }
  25.       
  26.     public void testSubProcess() {
  27.         // prepare data packet
  28.         Map<String, Object> variables = new HashMap<String, Object>();
  29.         Map<String, Object> subVariables = new HashMap<String, Object>();
  30.         variables.put("maxTransCount", 1000000);
  31.         variables.put("merchant", new Merchant("ICBC"));
  32.         variables.put("protocol", "UM32");
  33.         variables.put("repository", "10.10.38.99:/home/shirdrn/repository");
  34.         variables.put("in", subVariables);
  35.         variables.put("out", new HashMap<String, Object>());
  36.           
  37.         // start process instance
  38.         ProcessInstance pi = runtimeService.startProcessInstanceByKey("MyReceiveTask", variables);
  39.         
  40.         /*第一种写法
  41.         Execution execution=runtimeService.createExecutionQuery()
  42.                 .processInstanceId(pi.getId())
  43.                 .activityId("receivetask1")
  44.                 .singleResult();
  45.         assertNotNull(execution);
  46.         runtimeService.signal(execution.getId());
  47.         
  48.         execution=runtimeService.createExecutionQuery()
  49.                 .processInstanceId(pi.getId())
  50.                 .activityId("receivetask2")
  51.                 .singleResult();
  52.         assertNotNull(execution);
  53.         runtimeService.signal(execution.getId());
  54.         */

  55.         List<Execution> executions = runtimeService.createExecutionQuery().list();
  56.         assertEquals(1, executions.size());
  57.          
  58.         //第二种写法
  59.         Execution execution = runtimeService.createExecutionQuery().singleResult();
  60.         runtimeService.setVariable(execution.getId(), "type", "receiveTask");
  61.         runtimeService.signal(execution.getId()); //向
  62.         assertEquals(1, executions.size());
  63.         
  64.         //第三种写法
  65.         execution = runtimeService.createExecutionQuery().list().get(0);
  66.         assertNotNull(execution);
  67.         runtimeService.setVariable(execution.getId(), "oper", "shirdrn");
  68.         runtimeService.signal(execution.getId());
  69.         
  70.     }

  71. }

运行结果
  1. 信息: i am CheckBankReceiveTask.
  2. in : {protocol=UM32, repository=10.10.38.99:/home/shirdrn/repository, merchant=Merchant[ICBC], maxTransCount=1000000, in={}, out={}}
  3. 八月 01, 2013 4:14:16 下午 com.manexcute.CheckMerchantReceiveTask execute
  4. 信息: i am CheckMerchantReceiveTask.
  5. in : {protocol=UM32, repository=10.10.38.99:/home/shirdrn/repository, merchant=Merchant[ICBC], maxTransCount=1000000, in={next=CheckBankTask}, out={reponse=subprocess:CheckBankReceiveTask->CheckMerchantReceiveTask}}

参考文献
1.http://blog.csdn.net/shirdrn/article/details/6270506







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