1.概述
手工触发执行是指,执行到流程中某个个结点后流程暂时停止运行,直到收到外部发送的信号以后,才会继续向前推进,这样情况可以更加精细地控制流程。
2.ReceiveTask
ReceiveTask只是一个简单的使流程进入等待状态的元素。直到引擎接收到明确的触发信息继续执行。
定义一个流程文件
上述流程定义中,对应的两个处理类,代码分别如下所示:
-
package com.manexcute;
-
-
import java.util.HashMap;
-
import java.util.logging.Logger;
-
-
import org.activiti.engine.delegate.DelegateExecution;
-
import org.activiti.engine.delegate.JavaDelegate;
-
-
public class CheckBankReceiveTask implements JavaDelegate {
-
-
private final Logger log = Logger.getLogger(CheckBankReceiveTask.class.getName());
-
-
@SuppressWarnings("unchecked")
-
@Override
-
public void execute(DelegateExecution execution) throws Exception {
-
log.info("i am CheckBankReceiveTask.");
-
System.out.println("in : " + execution.getVariables());
-
((HashMap<String, Object>)execution.getVariables().get("in")).put("next", "CheckBankTask");
-
((HashMap<String, Object>)execution.getVariables().get("out")).put("reponse", "subprocess:CheckBankReceiveTask->CheckMerchantReceiveTask");
-
}
-
}
-
package com.manexcute;
-
-
import java.util.HashMap;
-
import java.util.logging.Logger;
-
-
import org.activiti.engine.delegate.DelegateExecution;
-
import org.activiti.engine.delegate.JavaDelegate;
-
-
public class CheckMerchantReceiveTask implements JavaDelegate {
-
-
private final Logger log = Logger.getLogger(CheckMerchantReceiveTask.class.getName());
-
-
@SuppressWarnings("unchecked")
-
@Override
-
public void execute(DelegateExecution execution) throws Exception {
-
log.info("i am CheckMerchantReceiveTask.");
-
System.out.println("in : " + execution.getVariables());
-
((HashMap<String, Object>)execution.getVariables().get("in")).put("previous", "CheckMerchantReceiveTask");
-
}
-
}
测试类中还乃至一个Merchant类,该类必须支持序列化,如下所示:
-
package com.manexcute;
-
-
import java.io.Serializable;
-
-
public class Merchant implements Serializable {
-
private static final long serialVersionUID = 1L;
-
-
public Merchant(String merchantId, int priority, short serviceType, short status) {
-
super();
-
this.merchantId = merchantId;
-
this.priority = priority;
-
this.serviceType = serviceType;
-
this.status = status;
-
}
-
-
public Merchant(String merchantId) {
-
this(merchantId, -1, (short)0, (short)0);
-
}
-
-
private String merchantId;
-
private int priority = -1;
-
private short serviceType = 0;
-
private short status = 0;
-
-
public String getMerchantId() {
-
return merchantId;
-
}
-
public void setMerchantId(String merchantId) {
-
this.merchantId = merchantId;
-
}
-
public int getPriority() {
-
return priority;
-
}
-
public void setPriority(int priority) {
-
this.priority = priority;
-
}
-
public short getServiceType() {
-
return serviceType;
-
}
-
public void setServiceType(short serviceType) {
-
this.serviceType = serviceType;
-
}
-
public short getStatus() {
-
return status;
-
}
-
-
public void setStatus(short status) {
-
this.status = status;
-
}
-
-
@Override
-
public String toString() {
-
return "Merchant[" + merchantId + "]";
-
}
-
}
测试代码
-
package com.manexcute;
-
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
-
import org.activiti.engine.repository.Deployment;
-
import org.activiti.engine.runtime.Execution;
-
import org.activiti.engine.runtime.ProcessInstance;
-
-
import com.autoexcute.AbstractTest;
-
-
public class ManexcuteTest extends AbstractTest {
-
-
@Override
-
protected void initialize() throws Exception {
-
Deployment deployment = repositoryService
-
.createDeployment()
-
.addClasspathResource("com/manexcute/ReceiveTask.bpmn")
-
.deploy();
-
deploymentId = deployment.getId();
-
}
-
-
@Override
-
protected void destroy() throws Exception {
-
repositoryService.deleteDeployment(deploymentId, true);
-
}
-
-
public void testSubProcess() {
-
// prepare data packet
-
Map<String, Object> variables = new HashMap<String, Object>();
-
Map<String, Object> subVariables = new HashMap<String, Object>();
-
variables.put("maxTransCount", 1000000);
-
variables.put("merchant", new Merchant("ICBC"));
-
variables.put("protocol", "UM32");
-
variables.put("repository", "10.10.38.99:/home/shirdrn/repository");
-
variables.put("in", subVariables);
-
variables.put("out", new HashMap<String, Object>());
-
-
// start process instance
-
ProcessInstance pi = runtimeService.startProcessInstanceByKey("MyReceiveTask", variables);
-
-
/*第一种写法
-
Execution execution=runtimeService.createExecutionQuery()
-
.processInstanceId(pi.getId())
-
.activityId("receivetask1")
-
.singleResult();
-
assertNotNull(execution);
-
runtimeService.signal(execution.getId());
-
-
execution=runtimeService.createExecutionQuery()
-
.processInstanceId(pi.getId())
-
.activityId("receivetask2")
-
.singleResult();
-
assertNotNull(execution);
-
runtimeService.signal(execution.getId());
-
*/
-
-
List<Execution> executions = runtimeService.createExecutionQuery().list();
-
assertEquals(1, executions.size());
-
-
//第二种写法
-
Execution execution = runtimeService.createExecutionQuery().singleResult();
-
runtimeService.setVariable(execution.getId(), "type", "receiveTask");
-
runtimeService.signal(execution.getId()); //向
-
assertEquals(1, executions.size());
-
-
//第三种写法
-
execution = runtimeService.createExecutionQuery().list().get(0);
-
assertNotNull(execution);
-
runtimeService.setVariable(execution.getId(), "oper", "shirdrn");
-
runtimeService.signal(execution.getId());
-
-
}
-
-
}
运行结果
-
信息: i am CheckBankReceiveTask.
-
in : {protocol=UM32, repository=10.10.38.99:/home/shirdrn/repository, merchant=Merchant[ICBC], maxTransCount=1000000, in={}, out={}}
-
八月 01, 2013 4:14:16 下午 com.manexcute.CheckMerchantReceiveTask execute
-
信息: i am CheckMerchantReceiveTask.
-
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) |