一个复杂的程序可以分成几个细微的模块开发,同样一个复杂的业务流程也可以分成几个及其微小的子流程去执行、管理。
我们先来看看父流程,如图:
其中Subprocess1是一个子流程,在别的流程定义中单独有定义,
子流程如图:
父流程的xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<process name="ProcessFather" xmlns="http://jbpm.org/4.4/jpdl">
<start name="start1" g="75,52,48,48">
<transition name="to Subprocess1" to="Subprocess1" g="304,75:-96,-22" />
</start>
<state name="wait" g="61,241,92,52" />
<sub-process name="Subprocess1" g="243,143,117,52"
sub-process-key="ProcessSub">
<parameter-in var="fatherVar" subvar="subVar" />
<parameter-out var="fatherRes" subvar="subRes" />
<transition name="to wait" to="wait" g="302,268:-44,-22" />
</sub-process>
</process>
测试程序如下
/**
* 开始父流程 传入参数
*/
public void test03StartFather() {
Map<String, Object> in = new HashMap<String, Object>();
in.put("fatherVar", "你爸是李刚");
// 开始process流程
ProcessInstance processInstance = executionService
.startProcessInstanceByKey("ProcessFather", in);
// 流程实例的ID
String pid = processInstance.getId();
System.out.println(pid);// ProcessFather.20001
}
/**
* 看子流程
*/
public void test04SubSeeTask() {
List<Task> list = taskService.findPersonalTasks("liuyan");
for (Task task : list) {
String taskId = task.getId();
String subValue = (String) taskService
.getVariable(taskId, "subVar");
System.out.println(subValue);
}
}
/**
* 结束子流程的任务,向父流程传输参数
*/
public void test05completeTask() {
List<Task> list = taskService.findPersonalTasks("liuyan");
for (Task task : list) {
String taskId = task.getId();
// 传给父流程的参数
Map<String, Object> res = new HashMap<String, Object>();
res.put("subRes", "你是小霸王");
taskService.setVariables(taskId, res);
taskService.completeTask(taskId);
}
}
/**
* 查找父流程的变量
*/
public void test06findFatherRes() {
// 20001
ProcessInstance processInstance = executionService
.findProcessInstanceById("ProcessFather.20001");
String processInstanceId = processInstance.getId();
String result = (String) executionService.getVariable(
processInstanceId, "fatherRes");
System.out.println(result);
}