zoukankan      html  css  js  c++  java
  • Spring中的观察者模式(发布订阅模式)(基于SpringBoot实现)

    概述:

      spring中的事件机制涉及到者几个类文件 :ApplicationEvent(事件类型)、ApplicationListener(事件监听类)、ApplicationEventPublisher(事件发布类)。

      ApplicationEvent:继承jdk Utill包中的 EventObject。

      ApplicationListener :接口继承了utill 包中的EventListener接口,泛型参数E必须继承ApplicationEvent类。

      ApplicationEventPublisher: 事件发布接口 ,实现类很多,其中子类ApplicationContext,发布事件就用ApplicationContext类去发布。

    举例:

      在实际开发中,有一个这样的例子,当下单成功后会发送手机短信、发送绑定邮箱、微信、等

    示例代码:

    pom文件

     1   <parent>
     2         <groupId>org.springframework.boot</groupId>
     3         <artifactId>spring-boot-starter-parent</artifactId>
     4         <version>1.5.9.RELEASE</version>
     5     </parent>
     6     <dependencies>
     7         <dependency>
     8             <groupId>org.springframework.boot</groupId>
     9             <artifactId>spring-boot-starter-web</artifactId>
    10         </dependency>
    11         <dependency>
    12             <groupId>org.springframework.boot</groupId>
    13             <artifactId>spring-boot-starter-test</artifactId>
    14             <scope>test</scope>
    15         </dependency>
    16     </dependencies>

    定义事件:

     1 /**
     2  * 定义订单事件
     3  */
     4 public class OrderEvent extends ApplicationEvent {
     5 
     6     private String message;
     7 
     8     /**
     9      * Create a new ApplicationEvent.
    10      *
    11      * @param source the object on which the event initially occurred (never {@code null})
    12      */
    13     public OrderEvent(Object source, String message) {
    14         super(source);  //强制调用
    15         this.message = message;
    16     }
    17 
    18     @Override
    19     public Object getSource() {
    20         return super.getSource();
    21     }
    22 
    23     public String getMessage() {
    24         return message;
    25     }
    26 
    27     public void setMessage(String message) {
    28         this.message = message;
    29     }
    30 }

    短信监听:

     1 /**
     2  * 短信监听(异步执行)
     3  */
     4 @Component
     5 public class SmsListener implements ApplicationListener<OrderEvent> {
     6 
     7 
     8     private static final Logger logger = Logger.getLogger(SmsListener.class);
     9 
    10     @Override
    11     @Async  //异步
    12     public void onApplicationEvent(OrderEvent event) {
    13         System.out.println(Thread.currentThread() + "...短信监听到..." + event.getMessage()+ "......" + event.getSource());
    14     }
    15 }

    邮件监听:

     1 /**
     2  * @Author zechuang
     3  * @Date 2019/8/21
     4  */
     5 @Component
     6 public class EmailListener implements ApplicationListener<OrderEvent> {
     7 
     8     private static final Logger logger = Logger.getLogger(EmailListener.class);
     9     
    10     @Override
    11     @Async
    12     public void onApplicationEvent(OrderEvent event) {
    13         System.out.println(Thread.currentThread() + "...邮件监听到..." + event.getMessage()+ "......" + event.getSource());
    14     }
    15 }

    springBoot启动类

    1 @SpringBootApplication
    2 @EnableAsync   //开启异步
    3 public class MySpringBootApplication {
    4 
    5     public static void main(String[] args) {
    6         SpringApplication.run(MySpringBootApplication.class, args);
    7     }
    8 }

    测试:

     1 @RunWith(SpringRunner.class)
     2 @SpringBootTest(classes = MySpringBootApplication.class)
     3 public class ObserverTest {
     4 
     5     @Autowired
     6     private ApplicationContext applicationContext;
     7 
     8     @Test
     9     public void test01(){
    10         OrderEvent order = new OrderEvent(this, "用户下单成功");
    11         applicationContext.publishEvent(order);
    12         System.out.println("....................over........................");
    13     }
    14 }

    测试结果:

    ....................over........................
    Thread[SimpleAsyncTaskExecutor-1,5,main]...邮件监听到...用户下单成功......com.test.ObserverTest@19dd04d
    Thread[SimpleAsyncTaskExecutor-2,5,main]...短信监听到...用户下单成功......com.test.ObserverTest@19dd04d
    
  • 相关阅读:
    Python (一)Tkinter窗口组件:Label
    Python (八)Tkinter窗口组件:Scrollbar
    Python (四)Tkinter窗口组件:Radiobutton
    Python (五)Tkinter窗口组件:LabelFrame
    Python (三)Tkinter窗口组件:Checkbutton
    Scrapy安装及相关知识点概括
    Python (九)Tkinter窗口组件:Scale
    Python (六)Tkinter窗口组件:Entry
    电脑通过蓝牙适配器连接手机与蓝牙耳机之经验
    Noi2018 归途
  • 原文地址:https://www.cnblogs.com/zchok/p/11388932.html
Copyright © 2011-2022 走看看