JBPM节点分支之Group节点分析
JBPM的众多节点类型中,唯独Group节点比较有个性;Group节点作为众多的节点中的一员,其并不能完成业务功能,但其作为一种节点的容器,可以对现有的节点类型进行“混搭”,从而构造出可以完成新的业务功能的节点类型!今天我们来简单的学习一下Group节点,后续我们会进行深入分析JBPM的并发设计时候再次提到Group节点。
Group节点功能分析
作为父容器,可以将复杂的业务功能进行封装为新的节点类型,便于业务复用和维护;
可以将各种复杂的业务过程嵌套到Group中,简化复杂的业务流程;
可以作为流程并发的边界容器;
可以作为子流程的承载容器,将子流程封装为单独的一种节点类型;
Group的个性特点
作为容器节点,可以嵌套任意类型的节点,可以满足复杂的业务需求;
前一节点出弧可以直接跨越Group节点,与Group的子节点相连,子节点的出弧也可以直接Group的后一个节点;
嵌套多起点的时候可以进行并发;
下面我们看一下几个有Group节点的流程,来体味一下Group的功能和使用
简单的Group流程
在这个简单的公文流转(权且这样称谓吧)流程中,我们将这个简单的业务流程嵌套到Group节点中,封装了这个业务流程,当然从重用的角度来说,这并不是一个很好的例子,但是这同样足够体现了Group节点的功能!
流程定义JPDL
<start>
<transition to="evaluate document" />
</start>
<group name="evaluate document">
<start>
<transition to="distribute document" />
</start>
<state name="distribute document">
<transition to="collect feedback" />
</state>
<state name="collect feedback">
<transition name="approved" to="done" />
<transition name="rejected" to="update document" />
</state>
<state name="update document">
<transition to="distribute document" />
</state>
<end name="done" />
<transition to="publish document" />
</group>
<state name="publish document" />
</process>
流程测试用例
.startProcessInstanceByKey("GroupSimple");
String pid = processInstance.getId();
assertEquals("distribute document", processInstance.getActivityName());
processInstance = executionService.signalExecutionById(pid);
assertEquals("collect feedback", processInstance.getActivityName());
processInstance = executionService.signalExecutionById(pid, "rejected");
assertEquals("update document", processInstance.getActivityName());
processInstance = executionService.signalExecutionById(pid);
assertEquals("distribute document", processInstance.getActivityName());
processInstance = executionService.signalExecutionById(pid);
assertEquals("collect feedback", processInstance.getActivityName());
processInstance = executionService.signalExecutionById(pid, "approved");
assertEquals("publish document", processInstance.getActivityName());
带有定时器的Group流程
Group作为可等待外部唤醒的节点(此唤醒不同task、state的人工外部唤醒,此处唤醒是由流程引擎进行唤醒继续执行的),其出弧是可以设置定时器的,对于定时器我们后续会进行学习。在这个流程中,在Group开始执行后的2个业务小时后就会执行定时器所在的分支。
流程定义JPDL
<start>
<transition to="evaluate document" />
</start>
<group name="evaluate document">
<start>
<transition to="approve" />
</start>
<state name="approve">
<transition to="done" />
</state>
<end name="done" />
<transition to="publish document" />
<transition name="timeout" to="escalate">
<timer duedate="2 business hours" />
</transition>
</group>
<state name="escalate" />
<state name="publish document" />
</process>
流程测试用例
.startProcessInstanceByKey("GroupTimer");
Execution approveExecution = processInstance
.findActiveExecutionIn("approve");
assertNotNull(approveExecution);
List<Job> jobs = managementService
.createJobQuery()
.processInstanceId(processInstance.getId())
.list();
assertEquals(1, jobs.size());
Timer timer = (Timer) jobs.get(0);
managementService.executeJob(timer.getDbid());
processInstance = executionService
.findProcessInstanceById(processInstance.getId());
assertNotNull(processInstance.findActiveExecutionIn("escalate") );
具有多起点的Group流程(非并发流程)
在这个流程中,判断节点连接到Group中的不同分支上,通过运行时变量承载的弧名称来选择执行不同的分支,这种情况下只能有一个分支执行。
流程定义JPDL
<start>
<transition to="choose strategy" />
</start>
<decision name="choose strategy" expr="#{time}">
<transition name="plenty" to="play" />
<transition name="running out" to="plan" />
</decision>
<group name="evaluate project">
<start name="play">
<transition to="distribute document" />
</start>
<state name="distribute document" />
<start name="plan">
<transition to="make planning" />
</start>
<state name="make planning" />
</group>
</process>
流程测试用例代码
variables.put("time", "plenty");
ProcessInstance pi = executionService
.startProcessInstanceByKey("GroupMultipleEntries", variables);
assertNotNull(pi.findActiveExecutionIn("distribute document"));
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("time", "running out");
ProcessInstance pi = executionService
.startProcessInstanceByKey("GroupMultipleEntries", variables);
assertNotNull(pi.findActiveExecutionIn("make planning"));
带有Group的并发流程
带有Group的并发流程与WF4中的Parallel活动类似,在下面的流程中,由于Group的子节点中由两个没有入弧的节点(不一定得是开始节点),所以运行时会产生两个并发分支。
流程定义JPDL
<start>
<transition to="evaluate project" />
</start>
<group name="evaluate project">
<start>
<transition to="distribute document" />
</start>
<state name="distribute document">
<transition to="collect feedback" />
</state>
<state name="collect feedback">
<transition to="document finished" />
</state>
<end name="document finished" />
<start>
<transition to="make planning" />
</start>
<state name="make planning">
<transition to="estimate budget" />
</state>
<state name="estimate budget">
<transition to="planning finished" />
</state>
<end name="planning finished" />
<transition to="public project announcement" />
</group>
<state name="public project announcement" />
</process>
流程测试用例
.startProcessInstanceByKey("GroupConcurrency");
String documentExecutionId = pi
.findActiveExecutionIn("distribute document").getId();
String planningExecutionId = pi
.findActiveExecutionIn("make planning").getId();
pi = executionService.signalExecutionById(documentExecutionId);
assertNotNull(pi.findActiveExecutionIn("collect feedback"));
assertNotNull(pi.findActiveExecutionIn("make planning"));
pi = executionService.signalExecutionById(planningExecutionId);
assertNotNull(pi.findActiveExecutionIn("collect feedback"));
assertNotNull(pi.findActiveExecutionIn("estimate budget"));
pi = executionService.signalExecutionById(planningExecutionId);
assertNotNull(pi.findActiveExecutionIn("collect feedback"));
pi = executionService.signalExecutionById(documentExecutionId);
assertNotNull(pi.findActiveExecutionIn("public project announcement"));