前面關於eventType的屬性值的配置簡單的說了一下,activiti支持的值如下表所示:這是我摘抄的activiti官網的
Event 的名字 | 描述 | Event的類名 |
---|---|---|
ENGINE_CREATED |
The process-engine this listener is attached to, has been created and is ready for API-calls. |
|
ENGINE_CLOSED |
The process-engine this listener is attached to, has been closed. API-calls to the engine are no longer possible. |
|
ENTITY_CREATED |
A new entity is created. The new entity is contained in the event. |
|
ENTITY_INITIALIZED |
A new entity has been created and is fully initialized. If any children are created as part of the creation of an entity, this event will be fired AFTER the create/initialisation of the child entities as opposed to the |
|
ENTITY_UPDATED |
An existing is updated. The updated entity is contained in the event. |
|
ENTITY_DELETED |
An existing entity is deleted. The deleted entity is contained in the event. |
|
ENTITY_SUSPENDED |
An existing entity is suspended. The suspended entity is contained in the event. Will be dispatched for ProcessDefinitions, ProcessInstances and Tasks. |
|
ENTITY_ACTIVATED |
An existing entity is activated. The activated entity is contained in the event. Will be dispatched for ProcessDefinitions, ProcessInstances and Tasks. |
|
JOB_EXECUTION_SUCCESS |
A job has been executed successfully. The event contains the job that was executed. |
|
JOB_EXECUTION_FAILURE |
The execution of a job has failed. The event contains the job that was executed and the exception. |
|
JOB_RETRIES_DECREMENTED |
The number of job retries have been decremented due to a failed job. The event contains the job that was updated. |
|
TIMER_FIRED |
A timer has been fired. The event contains the job that was executed? |
|
JOB_CANCELED |
A job has been canceled. The event contains the job that was canceled. Job can be canceled by API call, task was completed and associated boundary timer was canceled, on the new process definition deployment. |
|
ACTIVITY_STARTED |
An activity is starting to execute |
|
ACTIVITY_COMPLETED |
An activity is completed successfully |
|
ACTIVITY_CANCELLED |
An activity is going to be cancelled. There can be three reasons for activity cancellation (MessageEventSubscriptionEntity, SignalEventSubscriptionEntity, TimerEntity). |
|
ACTIVITY_SIGNALED |
An activity received a signal |
|
ACTIVITY_MESSAGE_RECEIVED |
An activity received a message. Dispatched before the activity receives the message. When received, a |
|
ACTIVITY_ERROR_RECEIVED |
An activity has received an error event. Dispatched before the actual error has been handled by the activity. The event’s |
|
UNCAUGHT_BPMN_ERROR |
An uncaught BPMN error has been thrown. The process did not have any handlers for that specific error. The event’s |
|
ACTIVITY_COMPENSATE |
An activity is about to be compensated. The event contains the id of the activity that is will be executed for compensation. |
|
VARIABLE_CREATED |
A variable has been created. The event contains the variable name, value and related execution and task (if any). |
|
VARIABLE_UPDATED |
An existing variable has been updated. The event contains the variable name, updated value and related execution and task (if any). |
|
VARIABLE_DELETED |
An existing variable has been deleted. The event contains the variable name, last known value and related execution and task (if any). |
|
TASK_ASSIGNED |
A task has been assigned to a user. The event contains the task |
|
TASK_CREATED |
A task has been created. This is dispatched after the |
|
TASK_COMPLETED |
A task has been completed. This is dispatched before the |
|
PROCESS_COMPLETED |
A process has been completed. Dispatched after the last activity |
|
PROCESS_CANCELLED |
A process has been cancelled. Dispatched before the process instance is deleted from runtime. Process instance is cancelled by API call |
|
MEMBERSHIP_CREATED |
A user has been added to a group. The event contains the ids of the user and group involved. |
|
MEMBERSHIP_DELETED |
A user has been removed from a group. The event contains the ids of the user and group involved. |
|
MEMBERSHIPS_DELETED |
All members will be removed from a group. The event is thrown before the members are removed, so they are still accessible. No individual |
|
簡單的說明:
所有的ENTITY_\*事件關聯流程引擎的entity-event事件,具體的entity-event監聽的范圍有這些:
-
ENTITY_CREATED, ENTITY_INITIALIZED, ENTITY_DELETED
: Attachment, Comment, Deployment, Execution, Group, IdentityLink, Job, Model, ProcessDefinition, ProcessInstance, Task, User. -
ENTITY_UPDATED
: Attachment, Deployment, Execution, Group, IdentityLink, Job, Model, ProcessDefinition, ProcessInstance, Task, User. -
ENTITY_SUSPENDED, ENTITY_ACTIVATED
: ProcessDefinition, ProcessInstance/Execution, Task.
補充說明:
事件監聽器僅僅會監聽流程引擎里面的事件發生,所以如果你有其他的引擎在運行同一個數據庫里面,僅僅是在同一個流程引擎中注冊的事件監聽器會被調用,在其他流程引擎中注冊的,即使在同一個JVM運行也不會生效。即使是這樣,你需要謹慎和更新/操作實體參與派遣的事件。
三、工作流核心--流程引擎
Activiti的流程引擎穿插在整個工作流中,從ProcessEngine中你可以獲取不同的service, ProcessEngine和Service對象都是線程安全的,所以在整個服務器中你只需要存在這些單例對象就可以了。
ProcessEngine和Service對象的創建方式如下:
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); RepositoryService repositoryService = processEngine.getRepositoryService(); TaskService taskService = processEngine.getTaskService(); ManagementService managementService = processEngine.getManagementService(); IdentityService identityService = processEngine.getIdentityService(); HistoryService historyService = processEngine.getHistoryService(); FormService formService = processEngine.getFormService();
ProcessEngines.getDefaultProcessEngine(),合理的關閉和初始化所有的ProcessEngine的方法是:ProcessEngines.destroy()和ProcessEngines.init() 。ProcessEngines對象將會掃描所有不同目錄的activiti.cfg.xml和activiti-context.xml文件,對於所有的activiti.cfg.xml文件,內部都會調用Activiti底層的方法,ProcessEngineConfiguration.createProcessEngineConfigurationFromInputStream(inputStream).buildProcessEngine(),對於所有的activiti-context.xml,ProcessEngine將會被以Spring的方式創建,在Spring的上下文創建后,ProcessEngine將會被加進Spring的上下文中。
所有的Service是無狀態的,這意味這你可以在具有多個節點的集群上面,每個相同的數據庫上面進行調用這些Service,而不用關心是那一台機器調用的,任何服務的調用都是冪等的,無論它何處運行。
注:冪等:是一個數學與計算機學概念,常見於抽象代數中。 在編程中.一個冪等操作的特點是其任意多次執行所產生的影響均與一次執行的影響相同。
一般的,在Activiti的開發中都會先去獲取RepositoryService,RepositoryService提供了管理和操作deployments和process definitions,這里就不用細說更多細節的東西了,一個流程定義的java對象其實就是BPMN 2.0文件的副本,流程定義對象也表現了每一步流程步驟的結構和行為。deployment是在工作流引擎里面打包的單位,也就是說它囊括了單個的bpmn文件到流程的文件夾和相關資源,在一個deployment中應該包含多少配置文件取決於開發者。RepositoryService允許發布文件夾,發布一個deployment意味着將會上傳到引擎中,在存進數據庫之前需要檢查和解析,從那時起,deployment中所有的流程就可以啟動了.
RepositoryService也提供了一些功能, 比如:
- 提供在流程引擎中流程定義和deployments的查詢。
- 阻塞或者激活deployment中全部或者部分流程定義。阻塞的意思是指以后這些流程定義將不會執行,直到被激活為止。
- 獲取各種資源文件,例如在deployment中的file,流程圖等。
- 獲取流程定義對象的版本信息。
RepositoryService保存了靜態信息,而RuntimeService則相反, RuntimeService處理流程定義實例。綜上所述,process definition對象是描述流程的結構和具體步驟的表現,而流程實例對象是這個流程定義對象的執行對象。同一時間每一個流程定義對象對應了多個流程實例在運行,RuntimeService同樣可以獲取存儲process variables(流程變量),在流程實例中獲取流程變量,在流程中作用於各種execution(比如排他網關通常使用流程變量來判斷流程路線的走向),Runtimeservice也允許查詢流程實例和執行實例,Executions是取用BPMN 2.0的概念,Execution(也就是執行實例)可以說一個指針指出當前的流程實例的位置。RuntimeService讓流程實例的深入執行,一個流程實例有幾種wait states,並且RunTimeService提供的不同的方式發信號(外部的觸發器觸發)給流程實例,流程實例向下執行。
Tasks(任務)在Activiti中需要被實際的人員去完成,關於所有的Task的事情都交給了TaskService處理,例如:
- 查詢任務被分配的組或人員
- 額外創建任務,這些任務是和流程實例沒有關系的。
- 任務的分配和相關人員的參與
- 領取任務和完成任務,領取任務就是說當前任務被人獲得,並且該任務將由他來完成。完成任務就是執行任務工作,常見的就是填寫表單。
IdentityService是很簡單的,它就是對users和group的增刪改查,重要提示:activiti在運行的時候並不會檢測用戶,比如現在一個任務可以分配給任何人做,但activiti並不會去檢查這個人是否存在於系統里,要知道Activiti是被用來提供LDAP,Active Directory等服務的。
FormService是一個可選用的Service,意味着activiti沒有它不會受到影響,不會因此少了功能。這個Service用來提供start form和task form的概念。start form指的是流程實例開始之前展示用戶的表單,而task form在用戶需要完成任務是展示的表單。Activiti允許用戶將這些表單插入流程定義中,但是沒必要內嵌進入activiti。
HistoryService提供所有關於工作流引擎的歷史數據,在流程執行的時候,一大筆數據會被引擎保存起來,例如流程實例開始的時候,記錄了誰在做任務,做了多久,流程實例的走向等等,HistoryService提供查詢方法支持這些數據的獲取。
ManagementService也是一個在工作流中可選的service,它允許從數據庫表和元數據中獲取數據,他也提供了對作業(job)的查詢和管理,Job被用來處理這些事情:定時器,異步延遲等。3.1 異常的處理策略
activiti里面最常用的是org.activiti.engine.ActivitiException,他是一個運行時異常,在整個工作流API中都可以拋出,但最好還是根據api在相應的地方使用對應提供的異常類。比如:在RuntimeService中的一個方法
/** * Called when the task is successfully executed. * @param taskId the id of the task to complete, cannot be null. * @throws ActivitiObjectNotFoundException when no task exists with the given id. */ void complete(String taskId);
明確的說明的id不能為空,否則拋出ActivitiIllegalArgumentException.
盡管我們想避免拋出很寬泛的異常,但是在activiti的運行執行當中,仍然有超出我們預料的異常出現,不符合下面異常的都會拋出ActivitiException:
ActivitiWrongDbException:在工作流引擎發現數據庫中工作流版本和引擎本身的版本不一致的時候拋出。
ActivitiOptimisticLockingException:在並發讀寫相同數據,樂觀鎖發生版本不一致拋出異常。
ActivitiClassLoadingException:類加載的時候沒有找到或者加載它的時候發生錯誤會拋出異常。
ActivitiObjectNotFoundException:請求對象或者動作不存在的時候拋出。
ActivitiIllegalArgumentException:在調用含參API的時候,參數值非法。
ActivitiTaskAlreadyClaimedException:在任務已經被認領的時候。