活動(dòng)標(biāo)簽
-控制流程的活動(dòng)
-原子活動(dòng)
控制流程的活動(dòng)
Start 開始節(jié)點(diǎn),一個(gè)(主)流程只能有一個(gè)開始節(jié)點(diǎn)
End 結(jié)束節(jié)點(diǎn),一個(gè)流程可以有多個(gè)結(jié)束節(jié)點(diǎn)
Decision 條件判斷節(jié)點(diǎn),當(dāng)一個(gè)流程出現(xiàn)多個(gè)分支(情況),而分支排他時(shí)使用。
Fork 分支節(jié)點(diǎn),當(dāng)一個(gè)流程出現(xiàn)多個(gè)分支,而分支并列執(zhí)行時(shí)使用。
Join 聚合/聯(lián)合節(jié)點(diǎn),通常與fork節(jié)點(diǎn)一起使用。
Sub-process 子流程(本人未曾使用過)
State 狀態(tài)節(jié)點(diǎn) 一個(gè)暫停節(jié)點(diǎn),當(dāng)需要對(duì)流程的執(zhí)行進(jìn)行控制時(shí)使用。
Task 任務(wù)節(jié)點(diǎn),通常與form表單關(guān)聯(lián),主要是在流程實(shí)例經(jīng)過活動(dòng)時(shí)為某一人或
組指派任務(wù)
原子活動(dòng)
Java、Script、Sql、Hql、Email
下面介紹三個(gè)最常用的活動(dòng)
state
一個(gè)強(qiáng)制流程暫停的節(jié)點(diǎn),當(dāng)需要對(duì)流程的執(zhí)行進(jìn)行控制時(shí)使用。該節(jié)點(diǎn)可能什么都
不需要做,也可能執(zhí)行一些的操作。比如,路過銀行,你拍怕褲兜,看看錢夠還是不
夠。如果錢還可以花一段時(shí)間,你就不會(huì)去取錢。如果錢不多了,你就會(huì)去銀行取些
現(xiàn)金。或是錢本來就夠花,但外面在下著雨,你想取避一避雨也不是不可以。State
要做的相當(dāng)于帶你路過銀行,至于你取不取錢,取多少,還是為了別的什么,不是它
說了算,而是你自己的決定。
第一種方式:無分支的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>
測(cè)試代碼如下:
Java代碼
ProcessInstance processInstance =
executionService.startProcessInstanceByKey("simpleState");
System.out.println("流程實(shí)例Id:"+
processInstance.getId());
System.out.println("流程定義Id:"+
processInstance.getProcessDefinitionId());
System.out.println("是否在 state節(jié)點(diǎn):"+
processInstance.isActive("state"));//true
System.out.println("判斷流程是否結(jié)束:"+
processInstance.isEnded());//false
processInstance =
executionService.signalExecutionById(processInstance.getId());
System.out.println("是否在 state節(jié)點(diǎn):"+
processInstance.isActive("state"));//true
System.out.println("判斷流程是否結(jié)束:"+
processInstance.isEnded());//false
執(zhí)行結(jié)果如下:
Consult代碼
流程實(shí)例Id:simpleState.7
流程定義Id:simpleState-1
是否在 state節(jié)點(diǎn):true
判斷流程是否結(jié)束:false
**************
是否在 state節(jié)點(diǎn):false
判斷流程是否結(jié)束: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>
測(cè)試代碼如下:
Java代碼
ProcessInstance processInstance =
executionService.startProcessInstanceByKey("compState");
System.out.println("是否在 proot節(jié)點(diǎn):"+
processInstance.isActive("proot"));//true
System.out.println("是否在 boy節(jié)點(diǎn):"+
processInstance.isActive("boy"));//false
System.out.println("是否在 girl節(jié)點(diǎn):"+
processInstance.isActive("girl"));//false
System.out.println("判斷流程是否結(jié)束:"+
processInstance.isEnded());//false
processInstance =
executionService.signalExecutionById(processInstance.getId
(),"to boy");//因?yàn)閜root往下有多個(gè)分支,如果不指定流程轉(zhuǎn)向,流程不會(huì)繼續(xù)往
下執(zhí)行
System.out.println("是否在 proot節(jié)點(diǎn):"+
processInstance.isActive("proot"));//false
System.out.println("是否在 boy節(jié)點(diǎn):"+
processInstance.isActive("boy"));//true
System.out.println("是否在 girl節(jié)點(diǎn):"+
processInstance.isActive("girl"));//false
System.out.println("判斷流程是否結(jié)束:"+
processInstance.isEnded());//false
executionService.signalExecutionById(processInstance.getId(),"to boy");
這句如果改為:
executionService.signalExecutionById(processInstance.getId());
則流程不會(huì)往下執(zhí)行,流程繼續(xù)停留在proot節(jié)點(diǎn)。
decision
條件判斷節(jié)點(diǎn),當(dāng)一個(gè)流程出現(xiàn)多個(gè)中情況而各種情況都排他時(shí)使用,相當(dāng)于switch
case.
第一種方式:decision內(nèi)置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>
測(cè)試代碼如下:
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"));//進(jìn)入state 200,暫停
System.out.println("processInstance isEnd:"+
processInstance.isEnded()); //流程未結(jié)束,返回false
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
//該方法返回processInstance如果不接收,processInstance還是原來的對(duì)
象
//如果不接收返回值,也不重新查詢,則processInstance還是方法調(diào)前的
狀態(tài)
processInstance=
executionService.signalExecutionById(processInstance.getId());
System.out.println("processInstance isEnd:"+
processInstance.isEnded()); //流程結(jié)束,返回true
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
控制臺(tái)輸出結(jié)果如下:
Consult代碼
200 isActive:true
processInstance isEnd:false
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
********
processInstance isEnd:true
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
第二種方式:為decision活動(dòng)設(shè)置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>
測(cè)試代碼如下:
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()); //流程未結(jié)束,返回false
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
//該方法返回processInstance如果不接收,processInstance還是原來的對(duì)
象
//如果不接收返回值,也不重新查詢,則processInstance還是方法調(diào)前的
狀態(tài)
processInstance=
executionService.signalExecutionById(processInstance.getId());
System.out.println("processInstance isEnd:"+
processInstance.isEnded()); //流程結(jié)束,返回true
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
控制臺(tái)輸出結(jié)果如下:
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");
}
}
測(cè)試代碼如下:
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()); //流程未結(jié)束,返回false
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
//該方法返回processInstance如果不接收,processInstance還是原來的對(duì)
象
//如果不接收返回值,也不重新查詢,則processInstance還是方法調(diào)前的
狀態(tài)
processInstance=
executionService.signalExecutionById(processInstance.getId());
System.out.println("processInstance isEnd:"+
processInstance.isEnded()); //流程結(jié)束,返回true
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
控制臺(tái)輸出結(jié)果如下:
Consult代碼
200 isActive:false
404 isActive:false
500 isActive:true
processInstance isEnd:false
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
************
processInstance isEnd:true
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
以上三種方式差別都不是很大,第三中在程序中通過Handler進(jìn)行復(fù)雜的處理。個(gè)人
覺得,如果對(duì)于邏輯不是很復(fù)雜的操作,前兩種方式是比較可取的。但如果邏輯過于
復(fù)雜,或者還有其他的操作比如同步數(shù)據(jù)庫中的其他信息等操作時(shí),不妨選擇第三種
方式。
這些代碼代碼比較簡(jiǎn)單,這里就不做過多的解釋。
Task
任務(wù)節(jié)點(diǎn),通常與form表單關(guān)聯(lián),主要是在流程實(shí)例經(jīng)過活動(dòng)時(shí)為某一人或組指派任
務(wù).
Task的assignee屬性
第一, assignee用來指示用戶,負(fù)責(zé)完成任務(wù)的人。分配人是一個(gè)任務(wù)中的字符串
屬性,引用一個(gè)用戶。(直接指定一個(gè)字符串)
第二,這個(gè)屬性默認(rèn)會(huì)當(dāng)做表達(dá)式來執(zhí)行。(指定一個(gè)表達(dá)式,然后在代碼里為該表達(dá)
式賦值) 如:在這里任務(wù)被分配給#{order.owner}。這意味著首先使用order這個(gè)名字
查找一個(gè)對(duì)象。 其中一個(gè)查找對(duì)象的地方是這個(gè)任務(wù)對(duì)應(yīng)的流程變量。 然后
getOwner()方法會(huì)用來獲得用戶id, 引用的用戶負(fù)責(zé)完成這個(gè)任務(wù)。
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>
測(cè)試代碼如下:
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節(jié)點(diǎn):"+
processInstance.isActive("task"));//true
System.out.println("流程實(shí)例Id:"+
processInstance.getId());
}
List<Task> list=taskService.findPersonalTasks("lihua");
for (Task task : list) {
System.out.println("任務(wù)活動(dòng)名稱:"+
task.getActivityName());
System.out.println("流程實(shí)例Id:"+
task.getExecutionId());
System.out.println("任務(wù)活動(dòng)Id:"+
task.getId());
System.out.println("任務(wù)活動(dòng)創(chuàng)建時(shí)間:"+
task.getCreateTime());
System.out.println("任務(wù)活動(dòng)進(jìn)度:"+
task.getProgress());
System.out.println("任務(wù)活動(dòng)分配給:"+
task.getAssignee());
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
控制臺(tái)輸出結(jié)果如下:
Consult代碼
流程是否處于task節(jié)點(diǎn):true
流程實(shí)例Id:task.7
************
流程是否處于task節(jié)點(diǎn):true
流程實(shí)例Id:task.11
*****************
任務(wù)活動(dòng)名稱:task
流程實(shí)例Id:task.11
任務(wù)活動(dòng)Id:13
任務(wù)活動(dòng)創(chuàng)建時(shí)間:2012-02-16 15:48:50.406
任務(wù)活動(dòng)進(jìn)度:null
任務(wù)活動(dòng)分配給:lihua
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
任務(wù)活動(dòng)名稱:task
流程實(shí)例Id:task.7
任務(wù)活動(dòng)Id:9
任務(wù)活動(dòng)創(chuàng)建時(shí)間:2012-02-16 15:48:50.375
任務(wù)活動(dòng)進(jìn)度:null
任務(wù)活動(dòng)分配給:lihua
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Tips代碼
1、在流程設(shè)置時(shí)如果出現(xiàn)亂碼可在Eclipse.ini中添加如下配置:
-Dfile.encoding=UTF-8
2、在jbpm中表達(dá)式$(),#()均可以成功解析。