最近在研究flowable,發(fā)現(xiàn)這個東東雖說是activiti的升級版,但感覺還是沒有a5的好用。
項目中需要實現(xiàn)一個全局事件,實現(xiàn)如下:
- 實現(xiàn)flowable的配置
@Bean
public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> customIdGeneratorConfigurer() {
return engineConfiguration -> {engineConfiguration.setIdGenerator( customIdGenerator());
engineConfiguration.setActivityBehaviorFactory(activityBehaviorFactory());
engineConfiguration.setTransactionManager(transactionManager);
//設(shè)置全局事件監(jiān)聽
engineConfiguration.setTypedEventListeners(this.getGlobalFlowableEventListener());
};
}
/**
* 設(shè)置系統(tǒng)級別監(jiān)聽器
*
* @return
*/
private Map<String, List<FlowableEventListener>> getGlobalFlowableEventListener() {
Map<String, List<FlowableEventListener>> typedListeners = new HashMap<String, List<FlowableEventListener>>();
List<FlowableEventListener> processCompleteList = new ArrayList<FlowableEventListener>();
processCompleteList.add(new ProcessEndListener());
typedListeners.put("PROCESS_COMPLETED", processCompleteList);
return typedListeners;
}
上述方法中,實現(xiàn)了流程結(jié)束時苗缩,自動觸發(fā)全局事件ProcessEndListener
- 繼承監(jiān)聽器FlowableEventListener,ProcessEndListener源碼如下:
/**
* 流程結(jié)束修改狀態(tài)
*/
public class ProcessEndListener implements FlowableEventListener {
private static final long serialVersionUID = 1L;
@Override
public void onEvent(FlowableEvent event) {
FlowableEngineEventImpl engineEvent=(FlowableEngineEventImpl)event;
SpringContextUtils.getApplicationContext().publishEvent(new ProcessEndEvent(engineEvent,engineEvent.getProcessInstanceId()));
}
@Override
public boolean isFailOnException() {
return false;
}
@Override
public boolean isFireOnTransactionLifecycleEvent() {
return false;
}
@Override
public String getOnTransaction() {
return null;
}
}
我上述代碼只是廣播了一下spring 事件声诸,讓代碼實現(xiàn)與流程引擎的解耦酱讶。
PS:flowable實現(xiàn)監(jiān)聽器我個人認(rèn)為比A5差了,他的參數(shù)只有一個FlowableEvent,這個接口沒法取到相關(guān)參數(shù)双絮,必須要強(qiáng)轉(zhuǎn)型為FlowableEngineEventImpl才可以浴麻。
而Activiti5沒有這種操作,感覺使用上比a5差了一點!
至此,全局監(jiān)聽事件完成囤攀!