最近在研究flowable,发现这个东东虽说是activiti的升级版,但感觉还是没有a5的好用。
项目中需要实现一个全局事件,实现如下:
- 实现flowable的配置
@Bean
public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> customIdGeneratorConfigurer() {
return engineConfiguration -> {engineConfiguration.setIdGenerator( customIdGenerator());
engineConfiguration.setActivityBehaviorFactory(activityBehaviorFactory());
engineConfiguration.setTransactionManager(transactionManager);
//设置全局事件监听
engineConfiguration.setTypedEventListeners(this.getGlobalFlowableEventListener());
};
}
/**
* 设置系统级别监听器
*
* @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;
}
上述方法中,实现了流程结束时,自动触发全局事件ProcessEndListener
- 继承监听器FlowableEventListener,ProcessEndListener源码如下:
/**
* 流程结束修改状态
*/
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 事件,让代码实现与流程引擎的解耦。
PS:flowable实现监听器我个人认为比A5差了,他的参数只有一个FlowableEvent,这个接口没法取到相关参数,必须要强转型为FlowableEngineEventImpl才可以。
而Activiti5没有这种操作,感觉使用上比a5差了一点!
至此,全局监听事件完成!