JBPM具体应用之decision节点的使用
JBPM工作流引擎为我们提供了许多的节点应用,每一个节点都有其不同的作用,其中有四个比较常用的节点,他们分别decision,fork,state和task。在本文中我们先介绍decision节点,余下的节点在下文介绍。decision节点,顾名思义是一个判断节点,通过判断条件来决定执行哪项任务,decision节点的使用方式又分为三种,他们依次是:
(1)handler方式:使用handler标签将具体的执行方向交给java类来决定,jpdl文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <process key="decision2" name="decision" xmlns="http://jbpm.org/4.4/jpdl">
- <start name="start1" g="43,136,48,48">
- <transition name="提交" to="exclusive1" g="-14,-25"/>
- </start>
- <decision name="exclusive1" g="153,137,48,48">
- <handler class="com.tgb.node.decision.MyDecision"/>
- <transition name="到组长" to="组长" g="-52,-20">
- </transition>
- <transition name="到科长" to="科长" g="-52,-20">
- </transition>
- <transition name="到主任" to="主任" g="-52,-20">
- </transition>
- </decision>
- <task assignee="zuzhang" name="组长" g="323,40,92,52">
- <transition name="组长批准" to="归档" g="-52,-20"/>
- </task>
- <task assignee="kezhang" name="科长" g="333,135,92,52">
- <transition name="科长批准" to="归档" g="-52,-20"/>
- </task>
- <task assignee="zhuren" name="主任" g="336,233,92,52">
- <transition name="主任批准" to="归档" g="-52,-20"/>
- </task>
- <task assignee="caiwu" name="归档" g="579,133,92,52">
- <transition name="通过" to="end1" g="-29,-23"/>
- </task>
- <end name="end1" g="749,141,48,48"/>
- </process>
在上面的jpdl文件中,我们可以看到当流程启动的时候具体执行那个task是由decision决定的,而decision又将判断权交给了MyDecision类,下面来看Decision类代码:
- public class MyDecision implements DecisionHandler {
- private static final long serialVersionUID = 1L;
- @Override
- public String decide(OpenExecution execution) {
- String to = execution.getVariable("to").toString();
- if(to.equals("组长")){
- return "到组长";
- }else if(to.equals("科长")){
- return "到科长";
- }else{
- return "到主任";
- }
- }
- }
这个Decision类是在什么时候做出的判断呢,非常简单的就可以想到,既然当创建流程实例的时候会自动进入到第一个任务节点,那么Decision类起判断作用的时刻毫无疑问应该是在创建流程实例的时候,下面我们就来看一下具体的创建过程:
- public class TestDecision extends JbpmTestCase implements JbpmUtil {
- @Override
- public void deploy() {
- super.startUp();
- repositoryService.createDeployment().addResourceFromClasspath("com/tgb/node/decision/decision2.jpdl.xml").deploy();
- }
- @Override
- public void createInstance() {
- super.startUp();
- Map<String,Object> map = new HashMap<String,Object>();
- map.put("to", "组长");
- ProcessInstance processInstance = executionService.startProcessInstanceByKey("decision2", map);
- print("流程实例ID",processInstance.getId());
- }
- @Override
- public void getCurrentActivity() {
- super.startUp();
- String name = executionService.createProcessInstanceQuery().processInstanceId("decision.20001").uniqueResult().findActiveActivityNames().toString();
- print("到达节点名词",name);
- }
- }
在上面的代码中我们可以看到我们将判断条件放到了map中,在启动实例的时候将map中的条件取出放入到Decision类中进行判断。
(2)EL表达式进行判断。首先来看jpdl文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <process key="decision" name="decision" xmlns="http://jbpm.org/4.4/jpdl">
- <start name="start1" g="43,136,48,48">
- <transition name="提交" to="exclusive1" g="-14,-25"/>
- </start>
- <decision name="exclusive1" expr="${node}" g="153,137,48,48">
- <transition name="小于2天" to="组长" g="-52,-20"/>
- <transition name="大于2天,小于10" to="科长" g="-52,-20"/>
- <transition name="10天以上" to="主任" g="-52,-20"/>
- </decision>
- <task assignee="zuzhang" name="组长" g="323,40,92,52">
- <transition name="组长批准" to="归档" g="-52,-20"/>
- </task>
- <task assignee="kezhang" name="科长" g="333,135,92,52">
- <transition name="科长批准" to="归档" g="-52,-20"/>
- </task>
- <task assignee="zhuren" name="主任" g="336,233,92,52">
- <transition name="主任批准" to="归档" g="-52,-20"/>
- </task>
- <task assignee="caiwu" name="归档" g="579,133,92,52">
- <transition name="通过" to="end1" g="-29,-23"/>
- </task>
- <end name="end1" g="749,141,48,48"/>
- </process>
使用EL表达式省去了第三方类进行判断的麻烦,我们直接将判断条件写在java代码中进行判断,后台代码如下:
- public class TestDecision extends JbpmTestCase implements JbpmUtil {
- @Override
- public void deploy() {
- super.startUp();
- repositoryService.createDeployment().addResourceFromClasspath("com/tgb/node/decision/decision.jpdl.xml").deploy();
- }
- @Override
- public void createInstance() {
- super.startUp();
- Map<String,Object> map = new HashMap<String,Object>();
- map.put("node", "小于2天");
- ProcessInstance processInstance = executionService.startProcessInstanceByKey("decision", map);
- print("流程实例ID",processInstance.getId());
- }
- @Override
- public void getCurrentActivity() {
- super.startUp();
- String name = executionService.createProcessInstanceQuery().processInstanceId("decision.20001").uniqueResult().findActiveActivityNames().toString();
- print("到达节点名词",name);
- }
- }
(3)使用Condition标签,将判断条件写在EL表达式中进行判断。仍然先看jpdl文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <process key="decision1" name="decision" xmlns="http://jbpm.org/4.4/jpdl">
- <start name="start1" g="43,136,48,48">
- <transition name="提交" to="exclusive1" g="-14,-25"/>
- </start>
- <decision name="exclusive1" g="153,137,48,48">
- <transition name="小于2天" to="组长" g="-52,-20">
- <condition expr="${days lt 2}"/>
- </transition>
- <transition name="大于2天,小于10" to="科长" g="-52,-20">
- <condition expr="${days ge 2 and days le 10}"/>
- </transition>
- <transition name="10天以上" to="主任" g="-52,-20">
- <condition expr="${days gt 10}"/>
- </transition>
- </decision>
- <task assignee="zuzhang" name="组长" g="323,40,92,52">
- <transition name="组长批准" to="归档" g="-52,-20"/>
- </task>
- <task assignee="kezhang" name="科长" g="333,135,92,52">
- <transition name="科长批准" to="归档" g="-52,-20"/>
- </task>
- <task assignee="zhuren" name="主任" g="336,233,92,52">
- <transition name="主任批准" to="归档" g="-52,-20"/>
- </task>
- <task assignee="caiwu" name="归档" g="579,133,92,52">
- <transition name="通过" to="end1" g="-29,-23"/>
- </task>
- <end name="end1" g="749,141,48,48"/>
- </process>
由于EL表达式具有的灵活性,我们可以在后台代码中仅传参数,而将具体的判断交给el表达式来完成,后台代码如下:
- public class TestDecision extends JbpmTestCase implements JbpmUtil {
- @Override
- public void deploy() {
- super.startUp();
- repositoryService.createDeployment().addResourceFromClasspath("com/tgb/node/decision/decision1.jpdl.xml").deploy();
- }
- @Override
- public void createInstance() {
- super.startUp();
- Map<String,Object> map = new HashMap<String,Object>();
- map.put("days", 1);
- ProcessInstance processInstance = executionService.startProcessInstanceByKey("decision1", map);
- print("流程实例ID",processInstance.getId());
- }
- @Override
- public void getCurrentActivity() {
- super.startUp();
- String name = executionService.createProcessInstanceQuery().processInstanceId("decision.20001").uniqueResult().findActiveActivityNames().toString();
- print("到达节点名词",name);
- }
- }
上面介绍的decision节点的三种使用方法其实只是形式上的区别,归结到一点都是在创建流程实例的时候根据条件确定到达哪个任务节点,只不过是形式不一样而已,在具体的使用过程中可以根据自己的需要选择使用哪种方式。