kxbin
成功留给有准备的人
posts - 10,  comments - 35,  trackbacks - 0
活动标签
-控制流程的活动
-原子活动
 
控制流程的活动
Start   开始节点,一个(主)流程只能有一个开始节点
End     结束节点,一个流程可以有多个结束节点
Decision 条件判断节点,当一个流程出现多个分支(情况),而分支排他时使用。
Fork    分支节点,当一个流程出现多个分支,而分支并列执行时使用。
Join    聚合/联合节点,通常与fork节点一起使用。
Sub-process 子流程(本人未曾使用过)
State   状态节点    一个暂停节点,当需要对流程的执行进行控制时使用。
Task    任务节点,通常与form表单关联,主要是在流程实例经过活动时为某一人或
组指派任务
 
原子活动
Java、Script、Sql、Hql、Email
 
 
下面介绍三个最常用的活动
state
一个强制流程暂停的节点,当需要对流程的执行进行控制时使用。该节点可能什么都
不需要做,也可能执行一些的操作。比如,路过银行,你拍怕裤兜,看看钱够还是不
够。如果钱还可以花一段时间,你就不会去取钱。如果钱不多了,你就会去银行取些
现金。或是钱本来就够花,但外面在下着雨,你想取避一避雨也不是不可以。State
要做的相当于带你路过银行,至于你取不取钱,取多少,还是为了别的什么,不是它
说了算,而是你自己的决定。
 
第一种方式:无分支的State
Xml代码  
<?xml version="1.0" encoding="UTF-8"?>  
  
<process name="simpleState" xmlns="http://jbpm.org/4.3/jpdl">  
   <start name="start1" g="368,117,48,48">  
      <transition name="to state" to="state" g="-59,-17"/>  
   </start>  
   <end name="end" g="372,396,48,48"/>  
   <state name="state" g="348,250,92,52">  
      <transition name="to end" to="end" g="-47,-17"/>  
   </state>  
</process>  
 
测试代码如下:
Java代码  
ProcessInstance processInstance =   
            executionService.startProcessInstanceByKey("simpleState");  
        System.out.println("流程实例Id:"+  
                processInstance.getId());  
        System.out.println("流程定义Id:"+  
                processInstance.getProcessDefinitionId());            
        System.out.println("是否在 state节点:"+  
                processInstance.isActive("state"));//true  
        System.out.println("判断流程是否结束:"+  
                processInstance.isEnded());//false  
          
        processInstance =   
            executionService.signalExecutionById(processInstance.getId()); 
 
        System.out.println("是否在 state节点:"+  
                processInstance.isActive("state"));//true  
        System.out.println("判断流程是否结束:"+  
                processInstance.isEnded());//false  
 
执行结果如下:
Consult代码  
流程实例Id:simpleState.7  
流程定义Id:simpleState-1  
是否在 state节点:true  
判断流程是否结束:false  
**************  
是否在 state节点:false  
判断流程是否结束:true  
 
第二种方式:有分支的state
Xml代码  
<?xml version="1.0" encoding="UTF-8"?>  
  
<process name="compState" xmlns="http://jbpm.org/4.3/jpdl">  
   <start name="start1" g="300,106,48,48">  
      <transition name="to proot" to="proot" g="-59,-17"/>  
   </start>  
   <end name="end" g="315,448,48,48"/>  
   <state name="proot" g="281,223,92,52">  
      <transition name="to boy" to="boy" g="-59,-17"/>  
      <transition name="to girl" to="girl" g="-59,-17"/>  
   </state>  
   <state name="boy" g="187,346,92,52">  
      <transition name="to end" to="end" g="-47,-17"/>  
   </state>  
   <state name="girl" g="383,342,92,52">  
      <transition name="to end" to="end" g="-47,-17"/>  
   </state>  
</process>  
 
测试代码如下:
Java代码  
ProcessInstance processInstance =   
            executionService.startProcessInstanceByKey("compState");  
          
        System.out.println("是否在 proot节点:"+  
                processInstance.isActive("proot"));//true  
        System.out.println("是否在 boy节点:"+  
                processInstance.isActive("boy"));//false  
          
        System.out.println("是否在 girl节点:"+  
                processInstance.isActive("girl"));//false  
          
        System.out.println("判断流程是否结束:"+  
                processInstance.isEnded());//false  
          
        processInstance =   
            executionService.signalExecutionById(processInstance.getId
(),"to boy");//因为proot往下有多个分支,如果不指定流程转向,流程不会继续往
下执行  
        System.out.println("是否在 proot节点:"+  
                processInstance.isActive("proot"));//false  
        System.out.println("是否在 boy节点:"+  
                processInstance.isActive("boy"));//true  
        System.out.println("是否在 girl节点:"+  
                processInstance.isActive("girl"));//false  
        System.out.println("判断流程是否结束:"+  
                processInstance.isEnded());//false    
 
executionService.signalExecutionById(processInstance.getId(),"to boy");
这句如果改为:
executionService.signalExecutionById(processInstance.getId());
则流程不会往下执行,流程继续停留在proot节点。
 
decision  
条件判断节点,当一个流程出现多个中情况而各种情况都排他时使用,相当于switch 
case.
第一种方式:decision内置condition
 
Xml代码  
<?xml version="1.0" encoding="UTF-8"?>  
<process key="deci" name="deci" xmlns="http://jbpm.org/4.3/jpdl">  
   <start g="358,77,48,48" name="start1">  
      <transition g="-83,-17" name="to exclusive1" to="exclusive1"/>  
   </start>  
   <end g="374,510,48,48" name="end"/>  
   <decision g="358,219,48,48" name="exclusive1">  
      <transition g="-59,-17" name="to 200" to="200">  
        <condition expr="#{errorcode == 200}"/>  
      </transition>  
      <transition g="-59,-17" name="to 404" to="404">  
        <condition expr="#{errorcode == 404}"/>  
      </transition>  
      <transition g="-59,-17" name="to 500" to="500">        
        <condition expr="#{errorcode == 500}"/>  
      </transition>  
   </decision>  
   <state g="194,351,92,52" name="200">  
      <transition g="-47,-17" name="to end" to="end"/>  
   </state>  
   <state g="340,349,92,52" name="404">  
      <transition g="-47,-17" name="to end" to="end"/>  
   </state>  
   <state g="476,349,92,52" name="500">  
      <transition g="-47,-17" name="to end" to="end"/>  
   </state>  
</process>  
 
 
测试代码如下:
Java代码  
Map<String, String> variables = new HashMap<String, String>();  
        variables.put("errorcode", "200");  
        ProcessInstance processInstance =   
            executionService.startProcessInstanceByKey("deci", variables); 
 
          
        System.out.println("200 isActive:"+  
                processInstance.isActive("200"));//进入state 200,暂停  
        System.out.println("processInstance isEnd:"+  
                processInstance.isEnded()); //流程未结束,返回false  
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");  
          
        //该方法返回processInstance如果不接收,processInstance还是原来的对
象  
        //如果不接收返回值,也不重新查询,则processInstance还是方法调前的
状态  
        processInstance=  
            executionService.signalExecutionById(processInstance.getId()); 
 
          
        System.out.println("processInstance isEnd:"+  
                processInstance.isEnded()); //流程结束,返回true  
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");  
 
控制台输出结果如下:
Consult代码  
200 isActive:true  
processInstance isEnd:false  
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  
********  
processInstance isEnd:true  
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  
 
第二种方式:为decision活动设置expr
Xml代码  
<?xml version="1.0" encoding="UTF-8"?>  
<process name="deciBaseExpr" xmlns="http://jbpm.org/4.3/jpdl">  
   <start name="start1" g="407,65,48,48">  
      <transition name="to exclusive1" to="exclusive1" g="-83,-17"/>  
   </start>  
   <end name="end1" g="418,497,48,48"/>  
   <decision name="exclusive1" g="409,207,48,48" expr="#{whatcode}">  
      <transition name="to 200" to="200" g="-59,-17"/>  
      <transition name="to 404" to="404" g="-59,-17"/>  
      <transition name="to 500" to="500" g="-59,-17"/>  
   </decision>  
   <state name="200" g="251,344,92,52">  
      <transition name="to end1" to="end1" g="-47,-17"/>  
   </state>  
   <state name="404" g="389,342,92,52">  
      <transition name="to end1" to="end1" g="-47,-17"/>  
   </state>  
   <state name="500" g="516,341,92,52">  
      <transition name="to end1" to="end1" g="-47,-17"/>  
   </state>  
</process>  
 
测试代码如下:
Java代码  
Map<String, String> variables = new HashMap<String, String>();  
        variables.put("whatcode", "to 404");  
        ProcessInstance processInstance =   
            executionService.startProcessInstanceByKey("deciBaseExpr", 
variables);  
          
        System.out.println("200 isActive:"+  
                processInstance.isActive("200"));//返回false  
        System.out.println("404 isActive:"+  
                processInstance.isActive("404"));//返回true  
        System.out.println("500 isActive:"+  
                processInstance.isActive("500"));//返回false  
        System.out.println("processInstance isEnd:"+  
                processInstance.isEnded()); //流程未结束,返回false  
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");  
          
        //该方法返回processInstance如果不接收,processInstance还是原来的对
象  
        //如果不接收返回值,也不重新查询,则processInstance还是方法调前的
状态  
        processInstance=  
            executionService.signalExecutionById(processInstance.getId()); 
 
          
        System.out.println("processInstance isEnd:"+  
                processInstance.isEnded()); //流程结束,返回true  
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");  
 
控制台输出结果如下:
Consult代码  
200 isActive:false  
404 isActive:true  
500 isActive:false  
processInstance isEnd:false  
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  
***************  
processInstance isEnd:true  
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  
 
第三种方式:为decision配置handler
Xml代码  
<?xml version="1.0" encoding="UTF-8"?>  
<process name="deciByHandler" key="deciByHandler" 
xmlns="http://jbpm.org/4.3/jpdl">  
   <start name="start1" g="341,55,48,48">  
      <transition name="to exclusive1" to="exclusive1" g="-83,-17"/>  
   </start>  
   <end name="end1" g="346,435,48,48"/>  
   <decision name="exclusive1" g="340,164,48,48">  
      <handler class="com.lihua.HandlerDecision"></handler>  
      <transition name="to 200" to="200" g="-59,-17"/>  
      <transition name="to 404" to="404" g="-59,-17"/>  
      <transition name="to 500" to="500" g="-59,-17"/>  
   </decision>  
   <state name="200" g="178,315,92,52">  
      <transition name="to end1" to="end1" g="-47,-17"/>  
   </state>  
   <state name="404" g="322,309,92,52">  
      <transition name="to end1" to="end1" g="-47,-17"/>  
   </state>  
   <state name="500" g="461,309,92,52">  
      <transition name="to end1" to="end1" g="-47,-17"/>  
   </state>  
</process>  
 
Handler类:
 
Java代码  
package com.lihua;  
  
import org.jbpm.api.jpdl.DecisionHandler;  
import org.jbpm.api.model.OpenExecution;  
  
public class HandlerDecision implements DecisionHandler {  
  
    private static final long serialVersionUID = -1639139174140348966L;  
  
    @Override  
    public String decide(OpenExecution execution) {  
        return (String) execution.getVariable("towhere");      
    }  
  
}  
 
测试代码如下:
Java代码  
Map<String, String> variables =   
            new HashMap<String, String>();  
        variables.put("towhere", "to 500");  
        ProcessInstance processInstance =   
            executionService.  
            startProcessInstanceByKey("deciByHandler", variables);  
          
        System.out.println("200 isActive:"+  
                processInstance.isActive("200"));//返回 false  
        System.out.println("404 isActive:"+  
                processInstance.isActive("404"));//返回 false  
        System.out.println("500 isActive:"+  
                processInstance.isActive("500"));//返回 true  
        System.out.println("processInstance isEnd:"+  
                processInstance.isEnded()); //流程未结束,返回false  
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");  
          
        //该方法返回processInstance如果不接收,processInstance还是原来的对
象  
        //如果不接收返回值,也不重新查询,则processInstance还是方法调前的
状态  
        processInstance=  
            executionService.signalExecutionById(processInstance.getId()); 
 
          
        System.out.println("processInstance isEnd:"+  
                processInstance.isEnded()); //流程结束,返回true  
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");  
 
控制台输出结果如下:
Consult代码  
200 isActive:false  
404 isActive:false  
500 isActive:true  
processInstance isEnd:false  
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  
************  
processInstance isEnd:true  
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  
 
以上三种方式差别都不是很大,第三中在程序中通过Handler进行复杂的处理。个人
觉得,如果对于逻辑不是很复杂的操作,前两种方式是比较可取的。但如果逻辑过于
复杂,或者还有其他的操作比如同步数据库中的其他信息等操作时,不妨选择第三种
方式。
这些代码代码比较简单,这里就不做过多的解释。
 
Task
任务节点,通常与form表单关联,主要是在流程实例经过活动时为某一人或组指派任
务.
Task的assignee属性
 
第一, assignee用来指示用户,负责完成任务的人。分配人是一个任务中的字符串
属性,引用一个用户。(直接指定一个字符串)
第二,这个属性默认会当做表达式来执行。(指定一个表达式,然后在代码里为该表达
式赋值) 如:在这里任务被分配给#{order.owner}。这意味着首先使用order这个名字
查找一个对象。 其中一个查找对象的地方是这个任务对应的流程变量。 然后
getOwner()方法会用来获得用户id, 引用的用户负责完成这个任务。
 
Xml代码  
<?xml version="1.0" encoding="UTF-8"?>  
  
<process name="task" xmlns="http://jbpm.org/4.3/jpdl">  
   <start name="start1" g="390,97,48,48">  
      <transition name="to task" to="task" g="-53,-17"/>  
   </start>  
   <end name="end1" g="391,362,48,48"/>  
   <task name="task" g="368,239,92,52" assignee="${taskAssignee}">  
      <transition name="to end1" to="end1" g="-47,-17"/>  
   </task>  
</process>  
 
 
测试代码如下:
Java代码  
Map<String,String> map=  
            new HashMap<String, String>();  
        map.put("taskAssignee", "lihua");  
        ProcessInstance processInstance=null;  
        for (int i = 0; i < 2; i++) {  
            processInstance=executionService.  
            startProcessInstanceByKey("task",map);            
            System.out.println("流程是否处于task节点:"+  
                    processInstance.isActive("task"));//true  
            System.out.println("流程实例Id:"+  
                    processInstance.getId());  
        }  
        List<Task> list=taskService.findPersonalTasks("lihua");  
        for (Task task : list) {  
            System.out.println("任务活动名称:"+  
                    task.getActivityName());  
            System.out.println("流程实例Id:"+  
                    task.getExecutionId());  
            System.out.println("任务活动Id:"+  
                    task.getId());  
            System.out.println("任务活动创建时间:"+  
                    task.getCreateTime());  
            System.out.println("任务活动进度:"+  
                    task.getProgress());  
            System.out.println("任务活动分配给:"+  
                    task.getAssignee());  
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");  
        }  
 
控制台输出结果如下:
Consult代码  
流程是否处于task节点:true  
流程实例Id:task.7  
************  
流程是否处于task节点:true  
流程实例Id:task.11  
*****************  
任务活动名称:task  
流程实例Id:task.11  
任务活动Id:13  
任务活动创建时间:2012-02-16 15:48:50.406  
任务活动进度:null  
任务活动分配给:lihua  
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  
任务活动名称:task  
流程实例Id:task.7  
任务活动Id:9  
任务活动创建时间:2012-02-16 15:48:50.375  
任务活动进度:null  
任务活动分配给:lihua  
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  
 
 
Tips代码  
1、在流程设置时如果出现乱码可在Eclipse.ini中添加如下配置:  
-Dfile.encoding=UTF-8  
2、在jbpm中表达式$(),#()均可以成功解析。  
posted on 2012-05-18 00:09 kxbin 阅读(1064) 评论(0)  编辑  收藏 所属分类: 流程引擎

只有注册用户登录后才能发表评论。


网站导航:
 
你恨一个人是因为你爱他;你喜欢一个人,是因为他身上有你没有的;你讨厌一个人是因为他身上有你有的东西;你经常在别人面前批评某人,其实潜意识中是想接近他。

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(5)

随笔档案

文章分类

文章档案

相册

收藏夹

J2EE

java技术网站

Linux

平时常去的网站

数据库

电影网站

网站设计

搜索

  •  

最新评论

阅读排行榜

评论排行榜