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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM