如果我們像給任務配置監聽,按照常規的做法是這樣的 一個個配置,比較麻煩。 現在利用ActivitiEventListener,監聽全局事件,並且可以判斷不同的事件類型,進而執行不同的業務邏輯。 1.定義事件處理handler介面 用介面來定義統一約定 2.實現不同事件 任務結束事件 任務創建事件 還 ...
如果我們像給任務配置監聽,按照常規的做法是這樣的
一個個配置,比較麻煩。
現在利用ActivitiEventListener,監聽全局事件,並且可以判斷不同的事件類型,進而執行不同的業務邏輯。
1.定義事件處理handler介面
用介面來定義統一約定
public interface EventHandler {
void handle(ActivitiEvent event);
}
2.實現不同事件
任務結束事件
*/
public class TaskCompleteListener implements EventHandler {
protected Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void handle(ActivitiEvent event) {
ActivitiEntityEventImpl eventImpl = (ActivitiEntityEventImpl) event;
TaskEntity taskEntity = (TaskEntity) eventImpl.getEntity();
}
}
任務創建事件
public class TaskCreateListener implements EventHandler {
protected Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void handle(ActivitiEvent event) {
ActivitiEntityEventImpl eventImpl = (ActivitiEntityEventImpl) event;
TaskEntity taskEntity = (TaskEntity) eventImpl.getEntity();
}
}
還有很多事件,比如TASK_ASSIGNED,PROCESS_STARTED,PROCESS_COMPLETED
3.實現ActivitiEventListener
public class GlobalEventListener implements ActivitiEventListener {
protected Logger logger = LoggerFactory.getLogger(getClass());
/**
* 各類 Event 的處理器
*/
private Map<ActivitiEventType, EventHandler> handlers = new HashMap<ActivitiEventType, EventHandler>();
@Override
public void onEvent(ActivitiEvent event) {
EventHandler eventHandler = handlers.get(event.getType());
if(eventHandler!=null){
eventHandler.handle(event);
}
}
@Override
public boolean isFailOnException() {
return false;
}
public Map<ActivitiEventType, EventHandler> getHandlers() {
return handlers;
}
public void setHandlers(Map<ActivitiEventType, EventHandler> handlers) {
this.handlers = handlers;
}
}
4.註冊事件監聽
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"/>
<property name="transactionManager" ref="transactionManager"/>
<property name="databaseSchemaUpdate" value="true"/>
<property name="jobExecutorActivate" value="false"/>
<property name="history" value="full"/>
<property name="processDefinitionCacheLimit" value="10"/>
<!-- 生成流程圖的字體 -->
<property name="activityFontName" value="宋體"/>
<property name="labelFontName" value="宋體"/>
<property name="annotationFontName" value="宋體"/>
<!-- 自動部署 -->
<property name="deploymentResources">
<list>
<value>classpath*:*</value>
</list>
</property>
<!-- <property name="idGenerator" ref="idGenerator"/>-->
<property name="eventListeners">
<list>
<ref bean="globalEventListener"/>
</list>
</property>
<property name="customFormTypes">
<bean class="org.activiti.engine.impl.form.DateFormType">
<constructor-arg value="yyyy-MM-dd HH:mm:ss"/>
</bean>
</property>
</bean>
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration"/>
</bean>
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
<bean id="formService" factory-bean="processEngine" factory-method="getFormService"/>
<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService"/>
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/>
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/>
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/>
<!--流程全局事件處理器-->
<bean id="globalEventListener" class="GlobalEventListener">
<property name="handlers">
<map>
<entry key="TASK_CREATED">
<bean class="TaskCreateListener"/>
</entry>
<entry key="TASK_COMPLETED">
<bean class="TaskCompleteListener"/>
</entry>
</map>
</property>
</bean>
優點
- 這樣代碼清晰,業務邏輯解耦,單一職責
- 省去在bpmn.xml中配置,方便。