flowable实现全局监听器的两种方式解决6.4.0版本FlowableEventDispatcher dispatcher = configuration.getEventDispatcher()

flowable实现全局监听器的两种方式解决6.4.0版本FlowableEventDispatcher dispatcher = configuration.getEventDispatcher()为空的问题

1.自定义监听器的业务实现

package com.flowable.demo.handle;

import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
import org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.Date;

@Component("globalProcistEndListener")
public class GlobalProcistEndListener extends AbstractFlowableEngineEventListener {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

		//任务创建时触发
    @Override
    protected void taskCreated(FlowableEngineEntityEvent event) {
        //监听器为同步的,要想获取当前线程的提交的数据,可以手动提交事务
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                //主线程的事务提交之后执行
            }
        });
        TaskEntity taskEntity= (TaskEntity)event.getEntity();
        //todo 处理业务逻辑
    }

    @Override
    protected void processCompleted(FlowableEngineEntityEvent event) {
       //流程结束时触发
       TaskEntity taskEntity= (TaskEntity)event.getEntity();
       //toto 处理业务逻辑
    }

}

2.全局监听器的配置

2.1 方式一:

package com.flowable.demo.config;

import com.flowable.demo.handle.GlobalProcistEndListener;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * Flowable全局监听配置
 * 用途:在任务特殊节点或者流程的特殊节点做一些自定义操作
 **/
@Configuration
public class FlowableGlobListenerConfig implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private SpringProcessEngineConfiguration configuration;

    @Autowired
    private GlobalProcistEndListener globalProcistEndListener;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();

        //流程结束全局监听
        dispatcher.addEventListener(globalProcistEndListener, FlowableEngineEventType.PROCESS_COMPLETED);
        //任务创建时进行监听,可根据需求 自己选择监听器触发时间
        dispatcher.addEventListener(globalProcistEndListener, FlowableEngineEventType.TASK_CREATED);
    }

}

该方式进行配置时遇到了一点问题,在flow able 6.4.0版本使用该方式时,FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();获取到的dispatcher为空,但是在更高版本(6.7.0)的flow able中没有遇到这个问题,所有还有另外一种配置方式。

2.2方式二

package com.flowable.demo.config;

import com.flowable.demo.handle.GlobalProcistEndListener;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;

public class InitListenerConfig implements CommandLineRunner {
    
    @Autowired
    private SpringProcessEngineConfiguration configuration;
    @Autowired
    private GlobalProcistEndListener globalProcistEndListener;
    
    
    @Override
    public void run(String... args) throws Exception {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        //流程完成时触发
        dispatcher.addEventListener(globalProcistEndListener, FlowableEngineEventType.PROCESS_COMPLETED);
        //任务创建时触发
        dispatcher.addEventListener(globalProcistEndListener, FlowableEngineEventType.TASK_CREATED);
    }
}

此方式在低版本的flow able中也可以配置成功,原因应该是用方式一配置时,SpringProcessEngineConfiguration 这个bean还没有初始化完毕,导致configuration.getEventDispatcher();获取到的bean为空,源码里面6.7.0版本做了特殊处理,再判断为空的时候进行了初始化操作,而6.4.0版本没有初始化操作,所以第一张方式配置不成功。此配置方式是在SpringProcessEngineConfiguration 初始化完成以后才进入到配置类里面的,所以configuration.getEventDispatcher();可以获取到bean。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flowable中注册全局监听器可以通过配置流程引擎来实现。具体步骤如下: 1. 创建全局监听器类,继承`org.flowable.engine.delegate.event.FlowableEventListener`接口,并实现其中的方法。 2. 在流程引擎配置类中添加全局监听器配置,如下所示: ``` @Bean public FlowableEventSupport flowableEventSupport() { FlowableEventSupport eventSupport = new FlowableEventSupport(); eventSupport.addEventListener(myGlobalEventListener()); return eventSupport; } @Bean public MyGlobalEventListener myGlobalEventListener() { return new MyGlobalEventListener(); } ``` 其中,`MyGlobalEventListener`为自定义的全局监听器类。 3. 将`flowableEventSupport()`方法添加到流程引擎配置中,如下所示: ``` @Bean public ProcessEngineConfiguration processEngineConfiguration() { SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration(); configuration.setDataSource(dataSource); configuration.setTransactionManager(transactionManager); configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE); configuration.setAsyncExecutorActivate(false); configuration.setEventListeners(Arrays.asList(flowableEventSupport())); return configuration; } ``` 其中,`setEventListeners()`方法用于设置全局事件监听器。 4. 启动应用程序,并验证全局监听器是否生效。 需要注意的是,在注册全局监听器时,需要将它添加到`FlowableEventSupport`对象中,而不是添加到流程引擎中。这是因为`FlowableEventSupport`是一个事件支持类,用于管理所有的事件监听器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值