• SpringBoot事件发布监听


    一、好处

    模块解耦、异步、消息广播、触发某一事件

    二、实现

    1.要监听的事件

    1. import org.springframework.context.ApplicationEvent;
    2. /**
    3. * @author :cxp
    4. * @description :要监听的事件
    5. * @date :2022/8/1 16:09
    6. */
    7. public class TestEvent extends ApplicationEvent {
    8. private static final long serialVersionUID = -2866878925912581424L;
    9. public TestEvent(Object source) {
    10. super(source);
    11. }
    12. }

    2.事件发布

    1. import cn.hutool.core.util.StrUtil;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.stereotype.Component;
    4. import javax.annotation.Resource;
    5. /**
    6. * @author :cxp
    7. * @description :发布者
    8. * @date :2022/8/1 16:11
    9. */
    10. @Component
    11. public class TestPublisher {
    12. @Resource
    13. private ApplicationContext applicationContext;
    14. public void publishEvent(String message) {
    15. if (StrUtil.isNotBlank(message)) {
    16. System.out.println("发布器所在线程:" + Thread.currentThread().getName());
    17. applicationContext.publishEvent(new TestEvent(message));
    18. }
    19. }
    20. }

    3.订阅者

    1. import org.springframework.context.event.EventListener;
    2. import org.springframework.stereotype.Component;
    3. /**
    4. * @author :cxp
    5. * @description :监听者1
    6. * @date :2022/8/1 16:10
    7. */
    8. @Component
    9. public class TestListener1 {
    10. @EventListener
    11. public void listen1(TestEvent testEvent) {
    12. System.out.println("TestListener1");
    13. System.out.println("所在线程:" + Thread.currentThread().getName());
    14. System.out.println("事件:" + testEvent);
    15. System.out.println("事件的数据:" + testEvent.getSource());
    16. }
    17. }
    18. *****************************************************************************************
    19. import org.springframework.context.event.EventListener;
    20. import org.springframework.scheduling.annotation.Async;
    21. import org.springframework.stereotype.Component;
    22. /**
    23. * @author :cxp
    24. * @description :监听者2(异步执行,*项目中记得加@EnableAsync注解)
    25. * @date :2022/8/1 16:10
    26. */
    27. @Component
    28. @Async("asyncExecutor")
    29. public class TestListener2 {
    30. // 支持SPEL表达式
    31. @EventListener
    32. public void listen1(TestEvent testEvent) {
    33. System.out.println("TestListener2");
    34. System.out.println("所在线程:" + Thread.currentThread().getName());
    35. System.out.println("事件:" + testEvent);
    36. System.out.println("事件的数据:" + testEvent.getSource());
    37. }
    38. }

    4.触发事件

    1. @Slf4j
    2. @RestController
    3. @RequestMapping("/event")
    4. public class TestController {
    5. @Resource
    6. private TestPublisher testPublisher;
    7. @GetMapping("/testEvent")
    8. public ResultData testEvent(String message) {
    9. log.info("[message]={}", message);
    10. testPublisher.publishEvent(message);
    11. return ResultData.success(message);
    12. }
    13. }
  • 相关阅读:
    ZCC5429 异步升压芯片
    RocketMQ批量发送消息是负载均衡的吗❓
    Audition 2024 for Mac/Win:音频录制与编辑的卓越之选
    双向链表的创建和遍历
    网络安全系列-三十六:使用Suricata IDS分析pcap文件
    【小程序源码】圣诞节头像框制作生成支持多模板
    redis的原理和源码-数据持久化方式RDB的介绍和源码解析
    今天起,Windows可以一键召唤GPT-4了
    基础概念回顾:云原生应用交付
    最新AI创作系统ChatGPT系统运营源码/支持最新GPT-4-Turbo模型/支持DALL-E3文生图
  • 原文地址:https://blog.csdn.net/weixin_43155301/article/details/126117878