flowable全局监听器方式,监听器注入的bean为空

看到很多文章写监听器然而大多数只是说任务监听器和事务监听器,如果要做统一的处理比如整个流程结束了我要做处理业务数据,不可能对每个流程都添加一个监听器,而使用flowable全局监听器就能处理这些需要统一处理的操作,全局监听主要有两种使用方式
1.实现FilowableEventLister
代码如下:

@Component
public class GlobalTaskListener  implements FlowableEventListener{
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    RepositoryService repositoryService;
    RuntimeService runtimeService;
    HistoryService historyService;
    private TaskService taskService;
``

    @Override
    public void onEvent(FlowableEvent event) {

          //可以看到这个地方可以获取流程实例引擎和流程定义 其实任务实体、流程历史数据等等也是也够拿到的比较强大,但是缺点就是只有一个FlowableEvent 参数需要去做转换
        FlowableEngineEventImpl engineEvent = (FlowableEngineEventImpl) 

        System.out.println("状态类型:" + engineEvent.getType());
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(engineEvent.getProcessDefinitionId())
                .singleResult();
   ;
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId(engineEvent.getProcessInstanceId())
                .singleResult();
    //业务数据

                }
              
    }
    同时还需要写一个类继承ApplicationListener
    @Configuration
public class FlowableGlobListenerConfig implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private SpringProcessEngineConfiguration configuration;
    @Autowired
    private GlobalTaskListener globalTaskListener;
    @Autowired
    private RuntimeService runtimeService;



    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();

        //任务创建全局监听
        dispatcher.addEventListener(globalTaskListener, FlowableEngineEventType.PROCESS_COMPLETED);
        dispatcher.addEventListener(globalTaskListener, FlowableEngineEventType.TASK_COMPLETED);
        dispatcher.addEventListener(globalTaskListener, FlowableEngineEventType.TASK_CREATED);
    }

flowchatableEngineEventType是处理事件的类型比如任务完成或者流程结束监听等等方便我们这个时候要处理业务系统的数据

2.继承AbstractFlowableEngineEventListener
@Component
public class GlobalTaskListener  extends AbstractFlowableEngineEventListener 
显然方式二更简单细看源码其实AbstractFlowableEngineEventListener的父类已
同时也要像方式一一样写一个配置类实现ApplicationListener因为代码一样就不重复写了
经实现了FilowableEventLister做了处理所以更方便


然而当我们使用@Autowired 还是 @Resource注入bean时却会报错 提示注入的bean为空,主要还是因为spring加载时机的原因 

解决方案是去实现ApplicationContextAware这个手动获取bean,


@Component
public class SpringBeanUtilFactory implements ApplicationContextAware {


    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringBeanUtilFactory .applicationContext=applicationContext;

    }
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    /**
     * 从当前IOC获取bean
     * @param clazz
     */
    public static <T> T getObject(Class<T> clazz){
        return applicationContext.getBean(clazz);
    }
}
然后在监听器使用

```SpringBeanUtilFactory.getObject(xxx.class)
示例如下:
   WfFormMapper form= SpringJobBeanFactory.getObject(WfFormMapper.class);
   这样就获取到了WfFormMapper 的bean可以做数据库的crud操作了

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flowable 全局监听器可以用于监听所有流程引擎实例中的事件。全局监听器必须实现 org.flowable.common.engine.api.delegate.event.FlowableEventListener 接口,并且注册到流程引擎配置中。当事件发生时,监听器将被触发并执行特定的业务逻辑。 以下是一个简单的 Flowable 全局监听器示例: ```java public class MyGlobalEventListener implements FlowableEventListener { @Override public void onEvent(FlowableEvent event) { // 处理事件 System.out.println("Event received: " + event.getType()); } @Override public boolean isFailOnException() { return false; } @Override public boolean isFireOnTransactionLifecycleEvent() { return false; } } ``` 在上面的示例中,onEvent() 方法处理监听到的事件。isFailOnException() 方法返回 false,表示如果监听器出现异常,流程引擎仍将继续执行。isFireOnTransactionLifecycleEvent() 方法也返回 false,表示不监听事务生命周期事件。 要将全局监听器注册到流程引擎配置中,可以使用以下代码: ```java ProcessEngineConfiguration config = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault(); config.setAsyncExecutorActivate(false); List<FlowableEventListener> eventListeners = new ArrayList<>(); eventListeners.add(new MyGlobalEventListener()); config.setEventListeners(eventListeners); ``` 在上面的示例中,将 MyGlobalEventListener 添加到 eventListeners 列表中,并将列表设置为流程引擎配置的事件监听器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值