flowable实现流程全局事件

最近在研究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差了一点!
至此,全局监听事件完成!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 8,862评论 1 11
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,216评论 25 709
  • 问题 假设我开了一个创业公司,现在邀请一批老伙伴加入公司,一起吃饼。这时候我收到了这些回复: ok, i will...
    转角遇见一直熊阅读 3,290评论 0 0
  • 情川骑着小黑,不过五十息的时间便回到了情家。 情川一会到情家,青源明的神念立刻就感应到了,一个瞬息便出现在了情川面...
    总有宫女想非礼朕阅读 2,206评论 3 7
  • 早上7:00提床洗漱早餐 8:30分送妙妙上学 9:00-12:00在办公室和工作人员交流。看书1个半小时《问题背...
    王翼wy阅读 754评论 0 0