springboot监听器

最近在项目中引入了一个事务提交后的监听器,现对这些功能做一个阶段性的总结:

什么是监听器?
  1. 就是用来监听程序执行的。监听器可以做什么事?可以监听程序执行,使程序在不同的阶段做不同的事情,并且与程序主业务逻辑解耦.
  2. web监听器就是Servlet中特殊的类,他们能帮助开发者监听web中的特定事件,比如ServletContext、HttpSession、ServletRequest的创建和销毁;变量的创建、销毁和修改等。可以在某些动作前后增加处理,实现监控等等。
Spring 监听器包含一下模块:
  • 发布器(ApplicationEventPublisher):用来在业务逻辑中发布事件。
    
  • 广播器(ApplicationEventMulticaster):用来把发布的事件广播给支持当前事件的监听器。
    
  • 监听器(ApplicationListener):用来监听自己感兴趣的事件,当程序发布事件时,可以执行一些相应的业务逻辑。ApplicationListener定义了onApplicationEvent方法,监听器监听到事件发布后执行onApplicationEvent方法。
    
  • 事件(ApplicationEvent):用来定义事件。
    
Spring的监听器执行步骤:
  • 小知识:Spring boot项目启动时会扫描项目中的监听器并加载到广播器中,所以广播器广播的时候能获取到所有的监听器。 Spring boot识别项目中的监听器规则是:@EventListener注解标注的方法,ApplicationListener接口的实现类。
  1. 程序在执行时调用SpringBoot的发布器(ApplicationEventPublisher)发布事件(ApplicationEvent)
    
  2. 发布器(ApplicationEventPublisher)调用广播器(ApplicationEventMulticaster)把事件广播给支持当前事件的监听器(ApplicationListener)。通俗的话描述,就是广播器根据发布的事件,从所有的监听器中筛选出支持当前事件的监听器。
    
  3. 执行监听器。Spring boot的监听器必须实现ApplicationListener接口并重写onApplicationEvent(Eevent)方法。执行监听器就是广播器筛选出符合条件的监听器后执行onApplicationEvent方法。
    

对于网上查询的一些监听器的应用,很多都是根据上下文初始化一些数据,做一些统计等,而针对于监听事务提交后触发事件这一个,springboot提供了一个@TransactionalEventListener

具体的实现如下:
  1. 需要我们创建3个类,对应的分别是 事件, 监听器, 发布器,
//发布器中实现ApplicationEventPublisherAware接口,同时初始化ApplicationEventPublisher
@Slf4j
@Component
public class cachePublisher implements ApplicationEventPublisherAware {

    //spring boot的事件发布器
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }
    
    /**
     * Desc: 发布事件,根据发布缓存事件的参数类型先构造对应的Event对象再发布事件 <br>
     * date: 2022/9/3   
     * @param event
     * @return void
     */
    public void publishEvent(Object event){
        applicationEventPublisher.publishEvent(new RefreshCacheEvent(this,(RefreshCache)event));
    }
}
  1. 事件继承ApplicationEvent 声明事件的类型,同时监听器中通过入参的类型确定是哪一种事件监听
public class RefreshCacheEvent extends ApplicationEvent {

    private final RefreshCache refreshCache;

    public RefreshCache getRefreshCache() {
        return refreshCache;
    }

    public RefreshEvent(Object source, RefreshCache refreshCache) {
        super(source);
        Assert.notNull(refreshCache, "refreshCache must not be null");
        this.refreshCache = refreshCache;
    }
}

  1. 监听器采用注解方式实现,@TransactionalEventListener 中间制定了监听器生效的时间,事件的类型,发生异常后如何处理
@Slf4j
@Component
@ConditionalOnBean(name={"refreshCacheService"})
public class RefreshCacheListener {

    @Resource
    private RefreshCacheService refreshCacheService;

    /***
     * Desc: 门户缓存刷新事件 <br>
     *     @TransactionalEventListener 注解表示此监听器是一个事务监听器,在事件发布方事务的对应阶段执行此监听器事件。
     *                                   phase 表示事务的阶段
     *                                   classes 表示监听的事件类型
     *                                   fallbackExecution 没有事务运行是否处理此事件,默认false
     */
    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT,
                                classes = {RefreshCacheEvent.class},
                                fallbackExecution = true)
    public void onApplicationEvent(RefreshCacheEvent event){
        //发送刷新缓存的消息
        refreshCacheService.sendMessage(event.getRefreshCache().getType());
    }

}

使用的地方注入 RefreshPublisher

refreshCachePublisher.publishEvent(refreshSingleJournalCache);
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值