Activiti 5.6提供了子流程的实现,包括两种基于子流程的实现:
一种是内嵌子流程:子流程元素<subProcess>内嵌在主流程元素<process>之内,只能在该流程中使用该子流程,外部是无法访问到的。这种子流程一般作为局部通用逻辑处理,或者因为特定业务需要,使得比较复杂的单个主流程设计清晰直观;
另一种是调用子流程:首先实现一个流程,在另一个流程中可以调用该流程,通常可以定义一些通用的流程作为这种调用子流程,供其他多个流程定义复用。这种子流程使用<callActivity>元素来进行调用,间接地嵌入到主流程中,用起来比较方便。
内嵌子流程
实现的基于内嵌子流程的流程,示例如图所示:
对应的流程定义文件SubProcessTest.MySubprocess.bpmn20.xml,内容如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<definitions id="definitions"
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn"
targetNamespace="Examples">
<process id="MySubprocess" name="My Subprocess">
<startEvent id="theStart" />
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="subProcess" />
<subProcess id="subProcess">
<startEvent id="subProcessStart" />
<sequenceFlow id="flow2" sourceRef="subProcessStart" targetRef="subProcessFork" />
<parallelGateway id="subProcessFork" />
<sequenceFlow id="flow3" sourceRef="subProcessFork" targetRef="task1" />
<sequenceFlow id="flow4" sourceRef="subProcessFork" targetRef="task2" />
<userTask id="task1" name="Check bank" activiti:candidateGroups="engineering">
<extensionElements>
<activiti:taskListener event="complete"
class="org.shirdrn.workflow.activiti.subprocess.CheckBankTask" />
</extensionElements>
</userTask>
<sequenceFlow id="flow5" sourceRef="task1" targetRef="subProcessJoin" />
<userTask id="task2" name="Investigate software" activiti:candidateGroups="engineering" >
<extensionElements>
<activiti:taskListener event="complete"
class="org.shirdrn.workflow.activiti.subprocess.CheckMerchantTask" />
</extensionElements>
</userTask>
<sequenceFlow id="flow6" sourceRef="task2" targetRef="subProcessJoin" />
<parallelGateway id="subProcessJoin" />
<sequenceFlow id="flow7" sourceRef="subProcessJoin" targetRef="subProcessEnd" />
<endEvent id="subProcessEnd" />
</subProcess>
<sequenceFlow id="flow10" sourceRef="subProcess" targetRef="taskAfterSubProcess" />
<userTask id="taskAfterSubProcess" name="Collect message" activiti:candidateGroups="engineering" >
<extensionElements>
<activiti:taskListener event="complete"
class="org.shirdrn.workflow.activiti.subprocess.CollectMessageTask" />
</extensionElements>
</userTask>
<sequenceFlow id="flow11" sourceRef="taskAfterSubProcess" targetRef="theEnd" />
<endEvent id="theEnd" />
</process>
</definitions>
流程定义中,实现了3个TaskListener,对应的代码分别如下所示:
package org.shirdrn.workflow.activiti.subprocess;
import java.util.HashMap;
import java.util.logging.Logger;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.impl.pvm.delegate.TaskListener;
public class CheckBankTask implements TaskListener {
private final Logger log = Logger.getLogger(CheckBankTask.class.getName());
@SuppressWarnings("unchecked")
public void notify(DelegateTask delegateTask) {
log.info("i am CheckBankTask.");
System.out.println("in : " + delegateTask.getVariables());
((HashMap<String, Object>)delegateTask.getVariables().get("in")).put("next", "CheckBankTask");
((HashMap<String, Object>)delegateTask.getVariables().get("out")).put("reponse", "subprocess:CheckBankTask->CheckMerchantTask");
}
}
package org.shirdrn.workflow.activiti.subprocess;
import java.util.HashMap;
public class CheckMerchantTask implements TaskListener {
private final Logger log = Logger.getLogger(CheckMerchantTask.class.getName());
@SuppressWarnings("unchecked")
public void notify(DelegateTask delegateTask) {
log.info("i am CheckMerchantTask.");
System.out.println("in : " + delegateTask.getVariables());
((HashMap<String, Object>)delegateTask.getVariables().get("in")).put("previous", "CheckMerchantTask");
}
}
package org.shirdrn.workflow.activiti.subprocess;
import java.util.Map;
public class CollectMessageTask implements TaskListener {
private final Logger log = Logger.getLogger(CollectMessageTask.class.getName());
@SuppressWarnings("unchecked")
public void notify(DelegateTask delegateTask) {
log.info("i am CollectMessageTask.");
System.out.println("out : " + (Map<String, Object>)delegateTask.getVariables().get("out"));
System.out.println("all : " + delegateTask.getVariables());
}
}
测试用例,代码如下所示:
package org.shirdrn.workflow.activiti.subprocess;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.shirdrn.workflow.activiti.AbstractTest;
public class MySubProcessTest extends AbstractTest {
@Override
protected void initialize() throws Exception {
Deployment deployment = repositoryService
.createDeployment()
.addClasspathResource(
"diagrams/SubProcessTest.MySubprocess.bpmn20.xml")
.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("MySubprocess", variables);
// enter subprocess
List<Task> tasks = taskService.createTaskQuery().processInstanceId(pi.getId()).orderByTaskName().asc().list();
assertEquals(2, tasks.size());
for(Task task : tasks) {
taskService.complete(task.getId());
}
// leave subprocess
Task collectTask = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
assertEquals("Collect message", collectTask.getName());
Map<String, Object> taskVariables = new HashMap<String, Object>();
taskVariables.put("att", "anything you need");
taskService.setVariable(collectTask.getId(), "oper", "shirdrn");
taskService.complete(collectTask.getId(), taskVariables);
}
}
运行结果信息,如下所示:
2011-3-24 17:36:36 org.shirdrn.workflow.activiti.subprocess.CheckBankTask notify
信息: i am CheckBankTask.
in : {protocol=UM32, repository=10.10.38.99:/home/shirdrn/repository, merchant=Merchant[ICBC], maxTransCount=1000000, in={}, out={}}
2011-3-24 17:36:36 org.shirdrn.workflow.activiti.subprocess.CheckMerchantTask notify
信息: i am CheckMerchantTask.
in : {protocol=UM32, repository=10.10.38.99:/home/shirdrn/repository, merchant=Merchant[ICBC], maxTransCount=1000000, in={next=CheckBankTask}, out={reponse=subprocess:CheckBankTask->CheckMerchantTask}}
2011-3-24 17:36:36 org.shirdrn.workflow.activiti.subprocess.CollectMessageTask notify
信息: i am CollectMessageTask.
out : {reponse=subprocess:CheckBankTask->CheckMerchantTask}
all : {protocol=UM32, repository=10.10.38.99:/home/shirdrn/repository, merchant=Merchant[ICBC], oper=shirdrn, att=anything you need, maxTransCount=1000000, in={previous=CheckMerchantTask, next=CheckBankTask}, out={reponse=subprocess:CheckBankTask->CheckMerchantTask}}