日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

activiti监听器使用

發布時間:2025/3/21 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 activiti监听器使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

分享牛原創(尊重原創 轉載對的時候第一行請注明,轉載出處來自分享牛http://blog.csdn.net/qq_30739519)

activiti使用的時候,通常需要跟業務緊密的結合在一起,有些業務非常的復雜,通常有如下一些場景:

1.activiti人員動態的分配。

2.當前任務節點完成的時候,指定需要指定下一個節點的處理人(比如,一個請假流程,a員工請假,需要指定下一步需要處理請假流程的領導。)

3.任務節點完成的時候,需要一些復雜業務,(比如當前節點完成的時候,需要調用我們的jms消息系統發送消息)

4.任務流轉到當前的節點的時候,需要監控當前任務節點的一些信息或者其他的業務信息。

5.當前的任務節點分配處理人的時候,需要觸發自定義的一些業務。

6.流程開始結束的時候,需要處理業務信息。

7.經過任務節點的出線,也就是連線的時候,需要觸發自定義的業務。

那我們怎么實現以上的這些需求呢?這個時候,我們就需要使用activiti監聽器,activiti提供的監聽器怎么實現,以及如何觸發,這些都需要我們一步步了解。下面就詳細的介紹activiti監聽器。

從?activiti監聽器的使用范圍來看,大致分為三種:

1.全局的監聽器。

2.連線的監聽器。

3.節點的監聽器。

下面我們一個個的來看如何使用這些監聽器。

1.1.1.?全局的監聽器

全局監聽器主要使用的場景就是監控這個流程的啟動和結束。流程開始的時候可以監控,流程結束的時候可以監控,這里說的是流程實例啟動結束的監控,并非是流程引擎的啟動結束監控。流程引擎的啟動結束監控可以參考http://blog.csdn.net/qq_30739519/article/details/51217614 不要混淆了這些概念。

下面我們先定義一個簡單的bpmn xml文件看一下如何使用全局的監聽器。

全局監聽器實現的接口是org.activiti.engine.delegate.ExecutionListener,org.activiti.engine.impl.pvm.delegate.ExecutionListener這個接口新版本已經廢棄。

1.1.1.1.?ExecutionListener定義

ExecutionListener定義如下:流程實例startendtake的時候調用。take是監控連線的時候使用的。

public interface ExecutionListener extends Serializable {String EVENTNAME_START = "start";String EVENTNAME_END = "end";String EVENTNAME_TAKE = "take";void notify(DelegateExecution execution) throws Exception; }

下面定義一個類監控流程實例的啟動監控,看如何使用。

1.1.1.2.?流程圖

?

1.1.1.3.?流程定義xml

流程的xml如下所示:

<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="daling"><process id="daling" name="name_daling" isExecutable="true" activiti:candidateStarterUsers="a,b,c,d"><extensionElements><activiti:executionListener event="start" class="com.daling.ch1.listener.MyExecutionListener"></activiti:executionListener><activiti:executionListener event="end" class="com.daling.ch1.listener.MyExecutionListener"></activiti:executionListener></extensionElements><startEvent id="startevent1" name="Start"></startEvent><endEvent id="endevent1" name="End"></endEvent><userTask id="usertask2" name="User Task"></userTask><sequenceFlow id="flow3" sourceRef="startevent1" targetRef="usertask2"></sequenceFlow><sequenceFlow id="flow4" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_daling"><bpmndi:BPMNPlane bpmnElement="daling" id="BPMNPlane_daling"><bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"><omgdc:Bounds height="35.0" width="35.0" x="120.0" y="110.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"><omgdc:Bounds height="35.0" width="35.0" x="640.0" y="90.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2"><omgdc:Bounds height="55.0" width="105.0" x="300.0" y="90.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"><omgdi:waypoint x="155.0" y="127.0"></omgdi:waypoint><omgdi:waypoint x="300.0" y="117.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"><omgdi:waypoint x="405.0" y="117.0"></omgdi:waypoint><omgdi:waypoint x="640.0" y="107.0"></omgdi:waypoint></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram> </definitions>

1.1.1.4.?監聽類

上面我們定義了兩個監聽器分別指向同一個類com.daling.ch1.listener.MyExecutionListener我們在類中監控事件。

com.daling.ch1.listener.MyExecutionListener類的實現如下所示:

package com.daling.ch1.listener; import org.activiti.engine.delegate.DelegateExecution; import org.activiti.engine.delegate.ExecutionListener; public class MyExecutionListener implements ExecutionListener { public void notify(DelegateExecution execution) throws Exception { String eventName = execution.getEventName(); //start if ("start".equals(eventName)) { System.out.println("start========="); }else if ("end".equals(eventName)) { System.out.println("end========="); } } }

1.1.1.5.?開始監控

下面我們部署啟動流程看一下效果,部署流程可參考之前的章節,啟動流程如下所示:

ProcessInstance processInstance?= runtimeService

.startProcessInstanceByKey(key,vars);

我們看一下程序的部分輸出如下:

08:34:07.050 [main] DEBUG o.a.e.i.h.DefaultHistoryManager - Current history level: AUDIT, level required: ACTIVITY

08:34:07.056 [main] DEBUG o.a.e.i.h.DefaultHistoryManager - Current history level: AUDIT, level required: ACTIVITY

08:34:07.057 [main] DEBUG o.a.e.i.h.DefaultHistoryManager - Current history level: AUDIT, level required: FULL

start=========

08:34:07.064 [main] DEBUG o.a.e.i.p.r.AtomicOperationActivityExecute - ProcessInstance[107509] executes Activity(startevent1): org.activiti.engine.impl.bpmn.behavior.NoneStartEventActivityBehavior

08:34:07.064 [main] DEBUG o.a.e.i.p.e.J.selectJobsByExecutionId - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@eeb514]

08:34:07.064 [main] DEBUG o.a.e.i.p.e.J.selectJobsByExecutionId - ==> ?Preparing: select * from ACT_RU_JOB J where J.EXECUTION_ID_ = ?

可以看出流程實例啟動的時候,觸發了我們自定義的類中的函數。

1.1.1.6.?結束監控

下面我們把usertask的任務結束,然后流程實例結束,看看觸發了我們自定義的類中的end沒有。

String taskId="107515";

demo.getTaskService().complete(taskId);

我們看一下程序的部分輸出如下:

08:39:22.016 [main] DEBUG o.a.e.i.p.r.AtomicOperationActivityExecute - ProcessInstance[107509] executes Activity(endevent1): org.activiti.engine.impl.bpmn.behavior.NoneEndEventActivityBehavior

08:39:22.016 [main] DEBUG o.a.e.i.h.DefaultHistoryManager - Current history level: AUDIT, level required: ACTIVITY

end=========

1.1.1.7.?DelegateExecution類

在上面的全局監聽器中我們可以拿到DelegateExecution對象,這個對象可以讓我們操作activiti引擎中的一些東西,下面看一下DelegateExecution類中的定義。

DelegateExecution類圖如下:

?

啟動最常用的方法如下:

/** execution Id */String getId();/** 流程實例id*/String getProcessInstanceId();/** 這個比較有用 主要就是start、end、take */String getEventName();/*** 業務id已經廢棄*/String getBusinessKey();/*** 業務id */String getProcessBusinessKey();/*** 流程定義id*/String getProcessDefinitionId();/*** 獲取父id,并發的時候有用*/String getParentId();/*** 獲取當前的.Activityid*/String getCurrentActivityId();/*** 獲取當前的.Activity name*/String getCurrentActivityName();/*** 獲取TenantId 當有多個TenantId 有用*/String getTenantId();/*** 這個非常有用吧。當拿到EngineServices 對象所有的xxxService都可以拿到。*/EngineServices getEngineServices();

?

1.1.2.?連線監聽器

當節點結束的時候,經過連線的時候,我們可以在線上定義類,實現自己的業務邏輯。連線監聽器怎么實現呢?

這里為了方便測試,我們還是采用上面全局監聽器中的流程圖,下面我們自定義一個連線監聽器類,看看如何使用,可以拿到什么對象。

1.1.2.1.?流程圖

?

1.1.2.2.?流程定義xml

<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="daling"><process id="daling" name="name_daling" isExecutable="true" activiti:candidateStarterUsers="a,b,c,d"><extensionElements><activiti:executionListener event="start" class="com.daling.ch1.listener.MyExecutionListener"></activiti:executionListener><activiti:executionListener event="end" class="com.daling.ch1.listener.MyExecutionListener"></activiti:executionListener></extensionElements><startEvent id="startevent1" name="Start"></startEvent><endEvent id="endevent1" name="End"></endEvent><userTask id="usertask2" name="User Task"></userTask><sequenceFlow id="flow3" name="flow3" sourceRef="startevent1" targetRef="usertask2"><extensionElements><activiti:executionListener event="take" class="com.daling.ch1.listener.MyExecutionListener"></activiti:executionListener></extensionElements></sequenceFlow><sequenceFlow id="flow4" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_daling"><bpmndi:BPMNPlane bpmnElement="daling" id="BPMNPlane_daling"><bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"><omgdc:Bounds height="35.0" width="35.0" x="120.0" y="110.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"><omgdc:Bounds height="35.0" width="35.0" x="640.0" y="90.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2"><omgdc:Bounds height="55.0" width="105.0" x="300.0" y="90.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"><omgdi:waypoint x="155.0" y="127.0"></omgdi:waypoint><omgdi:waypoint x="300.0" y="117.0"></omgdi:waypoint><bpmndi:BPMNLabel><omgdc:Bounds height="14.0" width="100.0" x="155.0" y="127.0"></omgdc:Bounds></bpmndi:BPMNLabel></bpmndi:BPMNEdge>ss<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"><omgdi:waypoint x="405.0" y="117.0"></omgdi:waypoint><omgdi:waypoint x="640.0" y="107.0"></omgdi:waypoint></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram> </definitions>

1.1.2.3.?監聽類

上面我們定義了一個監聽器com.daling.ch1.listener.MyExecutionListener類中監控事件。

public class MyExecutionListener implements ExecutionListener { /****/ private static final long serialVersionUID = 7960387497099642910L; //ExecutionListener類的實現 public void notify(DelegateExecution execution) throws Exception { String eventName = execution.getEventName(); //start if ("start".equals(eventName)) { System.out.println("start========="); }else if ("end".equals(eventName)) { System.out.println("end========="); } else if ("take".equals(eventName)) { System.out.println("take========="); } } }

因為我們在flow3中定義的監聽類,所以流程經過flow3的時候觸發定義的take中的自定義事件。

?

1.1.3.?節點監聽器

1.1.3.1.?節點監聽器定義

在實際項目開發中,任務節點是經常用到的,所以我們必須要會使用節點監聽器。

節點監聽器的定義接口org.activiti.engine.delegate.TaskListener,org.activiti.engine.impl.pvm.delegate.TaskListener已經廢棄不用。

TaskListener類的定義如下所示:

String EVENTNAME_CREATE = "create";String EVENTNAME_ASSIGNMENT = "assignment";String EVENTNAME_COMPLETE = "complete";String EVENTNAME_DELETE = "delete";/*** Not an actual event, used as a marker-value for {@link TaskListener}s that should be called for all events,* including {@link #EVENTNAME_CREATE}, {@link #EVENTNAME_ASSIGNMENT} and {@link #EVENTNAME_COMPLETE} and {@link #EVENTNAME_DELETE}.*/String EVENTNAME_ALL_EVENTS = "all";void notify(DelegateTask delegateTask);

1.1.3.2.?節點監聽器類圖

節點監聽器的子類如下圖所示:

?

?

下面開始我們詳細的使用吧。首先還是定義一個xmlxml流程還是上面所示的,只是多了一些任務節點的監聽。

1.1.3.3.?流程圖

?

?

1.1.3.4.?流程定義xml

流程的xml如下所示:

<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="daling"><process id="daling" name="name_daling" isExecutable="true" activiti:candidateStarterUsers="a,b,c,d"><extensionElements><activiti:executionListener event="start" class="com.daling.ch1.listener.MyExecutionListener"></activiti:executionListener><activiti:executionListener event="end" class="com.daling.ch1.listener.MyExecutionListener"></activiti:executionListener></extensionElements><startEvent id="startevent1" name="Start"></startEvent><endEvent id="endevent1" name="End"></endEvent><userTask id="usertask2" name="User Task" activiti:assignee="c"><extensionElements><activiti:taskListener event="all" class="com.daling.ch1.listener.MyExecutionListener"></activiti:taskListener></extensionElements></userTask><sequenceFlow id="flow3" name="flow3" sourceRef="startevent1" targetRef="usertask2"><extensionElements><activiti:executionListener event="take" class="com.daling.ch1.listener.MyExecutionListener"></activiti:executionListener></extensionElements></sequenceFlow><sequenceFlow id="flow4" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_daling"><bpmndi:BPMNPlane bpmnElement="daling" id="BPMNPlane_daling"><bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"><omgdc:Bounds height="35.0" width="35.0" x="120.0" y="110.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"><omgdc:Bounds height="35.0" width="35.0" x="640.0" y="90.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2"><omgdc:Bounds height="55.0" width="105.0" x="300.0" y="90.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"><omgdi:waypoint x="155.0" y="127.0"></omgdi:waypoint><omgdi:waypoint x="300.0" y="117.0"></omgdi:waypoint><bpmndi:BPMNLabel><omgdc:Bounds height="14.0" width="100.0" x="155.0" y="127.0"></omgdc:Bounds></bpmndi:BPMNLabel></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"><omgdi:waypoint x="405.0" y="117.0"></omgdi:waypoint><omgdi:waypoint x="640.0" y="107.0"></omgdi:waypoint></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram> </definitions>

1.1.3.5.?監聽類

任務節點的監聽只要實現TaskListener接口即可。下面是任務監聽的實現類,如下所示:

public class MyExecutionListener implements ExecutionListener,TaskListener { /****/ private static final long serialVersionUID = 7960387497099642910L; //ExecutionListener類的實現 public void notify(DelegateExecution execution) throws Exception { String eventName = execution.getEventName(); //start if ("start".equals(eventName)) { System.out.println("start========="); }else if ("end".equals(eventName)) { System.out.println("end========="); } else if ("take".equals(eventName)) { System.out.println("take========="); } } //實現TaskListener中的方法 public void notify(DelegateTask delegateTask) { String eventName = delegateTask.getEventName(); if ("create".endsWith(eventName)) { System.out.println("create========="); }else if ("assignment".endsWith(eventName)) { System.out.println("assignment========"); }else if ("complete".endsWith(eventName)) { System.out.println("complete==========="); }else if ("delete".endsWith(eventName)) { System.out.println("delete============="); } } }

當流程運轉到usertask2我們看一下程序的輸出:

assignment========

create=========

因為usertask2節點配置了處理人所以觸發assignment事件監聽,當任務運轉到usertask2的時候觸發了create事件。

這里我們也可以得出一個結論:assignment事件比create先執行。

使用代碼結束任務,代碼如下:

String taskId="127515";

demo.getTaskService().complete(taskId);

當我們結束usertask2我們看一下程序的輸出:

complete===========

delete=============

在這里我們可以得出結論:

任務完成的時候,觸發complete事件,因為任務完成之后,要從ACT_RU_TASK中刪除這條記錄,所以觸發delete事件。

1.1.3.6.?DelegateTask類

在上面的任務監聽器中我們可以拿到DelegateTask對象,這個對象可以讓我們操作activiti引擎中的一些東西,下面看一下DelegateTask類中的定義主要訪法。

public interface DelegateTask extends VariableScope {/** 數據庫中的taskId主鍵*/String getId();/** 任務名稱 */String getName();/** 修改任務名稱 */void setName(String name);/** 獲取任務的描述信息 */String getDescription();/** 修改任務的描述信息 */void setDescription(String description);/*** lower priority: [0..19] lowest, [20..39] low, [40..59] normal, [60..79] high* [80..100] highest 任務處理的優先級范圍是0-100 */int getPriority();/** 修改優先級*/void setPriority(int priority);/** 獲取流程實例id */String getProcessInstanceId();/**獲取執行id*/String getExecutionId();/** 獲取流程定義id*/String getProcessDefinitionId();/** Adds the given user as a candidate user to this task. */void addCandidateUser(String userId);/** 添加候選人 */void addCandidateUsers(Collection<String> candidateUsers);/** 添加候選組 */void addCandidateGroup(String groupId); }

上面我們總結了,全局監聽器、連線、任務節點的使用,下面我們看一下共性的東西再做一次總結。

?

1.1.4.?其他的使用方式

1.1.4.1.?spring整合

上面我們在定義類的的時候,我們直接使用的類定義的方式,當然我們還可以跟sring整合使用,因為在實際項目中類的管理可能交給spring容器。使用也很簡單如下圖所示:

?

需要填寫的表達書,只需要使用spring方式注入即可,${}里面的值為需要使用的beanspring中的id.可以xml定義或者在注解中定義。注解中定義需要開啟spring包掃描機制。比如我的@Service("myExecutionListener")。

?

1.1.5.?小結

1.activiti監聽方式分為三大類,節點監聽、全局監聽、連線監聽。

2.activiti監聽主要實現的類是兩個 節點監聽、全局監聽實現org.activiti.engine.delegate.ExecutionListener

節點的監聽實現org.activiti.engine.delegate.TaskListener接口即可。

3.監聽器其實就是一個觀察者模式。(關于觀察者模式,我們后續章節講解,限于篇幅有限)

4.監聽器獲取spring對象,如何獲取spring中的bean呢?

1.1.6.?遺留點

5.監聽器關于觀察者模式,我們后續章節講解。

6.監聽器獲取spring對象,如何獲取spring中的bean呢?留到下一個章節講解吧。

總結

以上是生活随笔為你收集整理的activiti监听器使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。