ApplicationEventPublisher与ApplicationEvent的使用和学习

概要

今天看到MallChat源码中有使用ApplicationEventPublisher去做消息发布,看到是org.springframework.context包中的,应该是spring自带的一种消息订阅方式,所以学习一下。
参考链接:https://www.jianshu.com/p/125815d5dd7c

ApplicationEventPublisherAware

ApplicationEventPublisherAware 是由 Spring 提供的用于为 Service 注入 ApplicationEventPublisher 事件发布器的接口,使用这个接口,我们自己的 Service
就拥有了发布事件的能力。

这个我倒是没用到,我这是service里直接自动注入ApplicationEventPublisher直接就publish了。

ApplicationListener

ApplicationListener接口是由 Spring 提供的事件订阅者必须实现的接口,我们一般把该 Service 关心的事件类型作为泛型传入。处理事件,通过 event.getSource() 即可拿到事件的具体内容

ApplicationEventPublisher

pplicationEventPublisher是ApplicationContext的父接口之一。这接口的作用是:Interface that encapsulates event publication functionality.

小结

下面附上我写的测试demo

import com.xiayuchen.mall.product.view.SpuSaveVo;
import lombok.Getter;
import org.springframework.context.ApplicationEvent;

@Getter
public class ProductEvent extends ApplicationEvent {

    private SpuSaveVo vo;

    public ProductEvent(Object source, SpuSaveVo vo) {
        super(source);
        this.vo = vo;
    }


}

首先创建一个自定义event类,需要继承ApplicationEvent

package com.xiayuchen.mall.product.event.listener;

import com.xiayuchen.mall.product.event.ProductEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class ProductEventListener {

    @EventListener(classes = ProductEvent.class,condition = "#event.vo.spuName=='666'")
    public void test(ProductEvent event){
        System.out.println("监听器接受到");
        System.out.println(event.getVo());
        System.out.println(Thread.currentThread());
    }
}

然后创建一个自定义的监听类,需要加入到IOC容器
在处理事件的方法上需要打上@EventListener注解,参数可以指明消费的事件类并做条件筛选,符合条件的消息才会被当前方法处理。

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EventListener {
    @AliasFor("classes")
    Class<?>[] value() default {};

    @AliasFor("value")
    Class<?>[] classes() default {};

    String condition() default "";

    String id() default "";
}

目前没有用异步线程去做,下面附上controller测试接口

    @GetMapping("/testPublisher")
    public void testPublisher() {
        SpuSaveVo vo = SpuSaveVo.builder().spuName("666").build();
        applicationEventPublisher.publishEvent(new ProductEvent(this, vo));
        System.out.println(Thread.currentThread());
    }

在这里插入图片描述
可以看到是同一个线程处理,下面用异步线程池试一下
首先在启动类上打上@EnableAsync注解开启异步

@EnableAsync
public class MallProductApplication {

    public static void main(String[] args) {
        SpringApplication.run(MallProductApplication.class, args);
    }

}

然后再监听方法上打上@Async注解,参数可以指定线程池名称

@Component
public class ProductEventListener {

    @Async("threadPoolExecutor")
    @EventListener(classes = ProductEvent.class,condition = "#event.vo.spuName=='666'")
    public void test(ProductEvent event){
        System.out.println("监听器接受到");
        System.out.println(event.getVo());
        System.out.println(Thread.currentThread());
    }
}

在这里插入图片描述
可以看到打印的就不是一个线程了
当异步的时候就会有一个问题,如果在推送之前有与数据库交互的动作,在监听中需要查询前一步插入的数据时,异步就会存在有前一步insert异常回滚或操作还未完成,那在监听中就会获取不到数据,那就无法做后续动作,因此在异步时需要使用@TransactionalEventListener注解替换@EventListener注解,并表明在事务提交完成后执行。

@Component
public class ProductEventListener {

    @Async("threadPoolExecutor")
    //@EventListener(classes = ProductEvent.class,condition = "#event.vo.spuName=='666'")
    @TransactionalEventListener(classes = ProductEvent.class, condition = "#event.vo.spuName=='666'", phase = TransactionPhase.AFTER_COMMIT)
    public void test(ProductEvent event) {
        System.out.println("监听器接受到");
        System.out.println(event.getVo());
        System.out.println(Thread.currentThread());
    }
}

TransactionPhase这个枚举类中有四个值

public enum TransactionPhase {
    BEFORE_COMMIT,
    AFTER_COMMIT,
    AFTER_ROLLBACK,
    AFTER_COMPLETION;

    private TransactionPhase() {
    }
}

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值