Spring Event观察者模式事件监听详解

 更新时间:2022年08月18日 10:11:58   作者:llp1110  
这篇文章主要介绍了Java Spring Event事件监听详情解析,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下

Spring Event事件监听

Spring Event(Application Event)其实就是一个观察者设计模式,一个 Bean 处理完成任务后希望通知其它 Bean 或者说一个 Bean 想观察监听另一个Bean 的行为。在开发中我们经常就会遇到修改一个bean时,同时需要去修改其他得bean。或者说当一个bean得值发生变化时,需要修改另一个bean得业务。还有一些业务场景不需要在一次请求中同步完成,比如邮件发送、短信发送等。

MQ 确实可以解决这个问题,但 MQ比较重,非必要不提升架构复杂度。因此Spring Event是非常好得选择。

依赖:引入Spring得核心依赖即可

Spring Event同步使用

自定义事件

定义事件,继承 ApplicationEvent 的类成为一个事件类:

@Data
public class OrderProductEvent extends ApplicationEvent {
  /** 该类型事件携带的信息 */
  private String orderId;
  public OrderProductEvent(Object source, String orderId) {
    super(source);
    this.orderId = orderId;
  }
}

定义监听器

监听并处理事件,实现 ApplicationListener 接口或者使用 @EventListener 注解:

/**
 * 实现 ApplicationListener 接口,并指定监听的事件类型
 */
@Slf4j
@Component
public class OrderProductListener implements ApplicationListener<OrderProductEvent> {
  /**
   *  使用 onApplicationEvent 方法对消息进行接收处理
   *  
   * */
  @SneakyThrows
  @Override
  public void onApplicationEvent(OrderProductEvent event) {
    String orderId = event.getOrderId();
    long start = System.currentTimeMillis();
    Thread.sleep(2000);
    long end = System.currentTimeMillis();
    log.info("{}:校验订单商品价格耗时:({})毫秒", orderId, (end - start));
  }
}

定义发布者

发布事件,通过 ApplicationEventPublisher 发布事件:

@Slf4j
@Service
@RequiredArgsConstructor
public class OrderService {
  /** 注入ApplicationContext用来发布事件 */
  private final ApplicationContext applicationContext;
  /**
   * 下单
   *
   * @param orderId 订单ID
   */
  public String buyOrder(String orderId) {
    long start = System.currentTimeMillis();
    // 1.查询订单详情
    // 2.检验订单价格 (同步处理)
    applicationContext.publishEvent(new OrderProductEvent(this, orderId));
    long end = System.currentTimeMillis();
    log.info("任务全部完成,总耗时:({})毫秒", end - start);
    return "购买成功";
  }
}

测试执行

@SpringBootTest
public class OrderServiceTest {
  @Autowired
  private OrderService orderService;
  @Test
  public void buyOrderTest() {
    orderService.buyOrder("732171109");
  }
}

c.l.l.event.OrderProductListener : 732171109:校验订单商品价格耗时:(2001)毫秒

c.llp.llpspringretry.event.OrderService : 任务全部完成,总耗时:(2005)毫秒

Debug执行流程

Spring Event 异步使用

有些业务场景不需要在一次请求中同步完成,比如邮件发送、短信发送等。

自定义事件

import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class MsgEvent {
  /** 该类型事件携带的信息 */
  public String orderId;
}

定义监听器

推荐使用 @EventListener 注解:

@Slf4j
@Component
public class MsgListener {
  @Async
  @SneakyThrows
  @EventListener(MsgEvent.class)
  public void sendMsg(MsgEvent event) {
    String orderId = event.getOrderId();
    long start = System.currentTimeMillis();
    log.info("开发发送短信");
    log.info("开发发送邮件");
    Thread.sleep(4000);
    long end = System.currentTimeMillis();
    log.info("{}:发送短信、邮件耗时:({})毫秒", orderId, (end - start));
  }
}

定义发布者

@Slf4j
@Service
@RequiredArgsConstructor
public class OrderService {
  /** 注入ApplicationContext用来发布事件 */
  private final ApplicationContext applicationContext;
  /**
   * 下单
   *
   * @param orderId 订单ID
   */
  public String buyOrder(String orderId) {
    long start = System.currentTimeMillis();
    // 1.查询订单详情
    // 2.检验订单价格 (同步处理)
//    applicationContext.publishEvent(new OrderProductEvent(this, orderId));
    // 3.短信通知(异步处理) 新开线程执行监听得业务
    applicationContext.publishEvent(new MsgEvent(orderId));
    long end = System.currentTimeMillis();
    log.info("任务全部完成,总耗时:({})毫秒", end - start);
    return "购买成功";
  }
}

开启异步支持

@EnableAsync开启异步支持

@EnableAsync
@EnableRetry
@SpringBootApplication
public class LlpSpringRetryApplication {
    public static void main(String[] args) {
        SpringApplication.run(LlpSpringRetryApplication.class, args);
    }
}

c.llp.llpspringretry.event.OrderService : 任务全部完成,总耗时:(6)毫秒

c.llp.llpspringretry.event.MsgListener : 开发发送短信

c.llp.llpspringretry.event.MsgListener : 开发发送邮件

到此这篇关于Spring Event观察者模式事件监听详解的文章就介绍到这了,更多相关Spring Event事件监听内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java中的阻塞队列BlockingQueue使用详解

    Java中的阻塞队列BlockingQueue使用详解

    这篇文章主要介绍了Java中的阻塞队列BlockingQueue使用详解,阻塞队列是一种线程安全的数据结构,用于在多线程环境下进行数据交换,它提供了一种阻塞的机制,当队列为空时,消费者线程将被阻塞,直到队列中有数据可供消费,需要的朋友可以参考下
    2023-10-10
  • SpringBoot + minio实现分片上传、秒传、续传功能

    SpringBoot + minio实现分片上传、秒传、续传功能

    MinIO是一个基于Go实现的高性能、兼容S3协议的对象存储,使用MinIO构建用于机器学习,分析和应用程序数据工作负载的高性能基础架构,这篇文章主要介绍了SpringBoot + minio实现分片上传、秒传、续传,需要的朋友可以参考下
    2023-06-06
  • java substring(a)与substring(a,b)的使用说明

    java substring(a)与substring(a,b)的使用说明

    这篇文章主要介绍了java substring(a)与substring(a,b)的使用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-10-10
  • java中ThreadLocalRandom的使用详解

    java中ThreadLocalRandom的使用详解

    这篇文章主要介绍了java中ThreadLocalRandom的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • java实现网站微信扫码支付

    java实现网站微信扫码支付

    这篇文章主要为大家详细介绍了java实现网站微信扫码支付,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • java程序运行时内存分配详解

    java程序运行时内存分配详解

    这篇文章主要介绍了java程序运行时内存分配详解 ,需要的朋友可以参考下
    2016-07-07
  • Python基础之如何使用multiprocessing模块

    Python基础之如何使用multiprocessing模块

    今天带大家学习python多进程的相关知识,文中对multiprocessing模块的使用作了非常详细的介绍,需要的朋友可以参考下
    2021-06-06
  • Java全能工具类之Hutool的用法详解

    Java全能工具类之Hutool的用法详解

    Hutool是一个Java工具类库,由国内的程序员loolly开发,目的是提供一些方便、快捷、实用的工具类和工具方法,本文就来详细聊聊它的使用吧
    2023-03-03
  • MyBatis映射文件中的动态SQL实例详解

    MyBatis映射文件中的动态SQL实例详解

    在本文中,我们深入探讨了动态SQL的各种标签,包括<if>、<choose>、<trim>、<foreach>等,通过实际的例子演示了它们的用法,感兴趣的朋友一起揭开动态SQL的神秘面纱,带你领略它的魅力
    2024-01-01
  • Java源码解析之接口List

    Java源码解析之接口List

    今天带大家复习Java基础的一些知识点,对接口List进行了详细的解析,对正在学习Java的小伙伴们有很好地帮助,需要的朋友可以参考下
    2021-05-05

最新评论