spring中的異步事件


這里講解一下Spring對異步事件機制的支持,實現方式有兩種:

 

1、全局異步

即只要是觸發事件都是以異步執行,具體配置(spring-config-register.xml)如下:

 

Java代碼   收藏代碼
  1. <task:executor id="executor" pool-size="10" />  
  2. <!-- 名字必須是applicationEventMulticaster和messageSource是一樣的,默認找這個名字的對象 -->  
  3. <!-- 名字必須是applicationEventMulticaster,因為AbstractApplicationContext默認找個 -->  
  4. <!-- 如果找不到就new一個,但不是異步調用而是同步調用 -->  
  5. <bean id="applicationEventMulticaster" class="org.springframework.context.event.SimpleApplicationEventMulticaster">  
  6.     <!-- 注入任務執行器 這樣就實現了異步調用(缺點是全局的,要么全部異步,要么全部同步(刪除這個屬性即是同步))  -->  
  7.     <property name="taskExecutor" ref="executor"/>  
  8. </bean>  
通過注入taskExecutor來完成異步調用。具體實現可參考之前的代碼介紹。這種方式的缺點很明顯:要么大家都是異步,要么大家都不是。所以不推薦使用這種方式。
 

2、更靈活的異步支持

spring3提供了@Aync注解來完成異步調用。此時我們可以使用這個新特性來完成異步調用。不僅支持異步調用,還支持簡單的任務調度,比如我的項目就去掉Quartz依賴,直接使用spring3這個新特性,具體可參考spring-config.xml

 

2.1、開啟異步調用支持

 

Java代碼   收藏代碼
  1. <!-- 開啟@AspectJ AOP代理 -->  
  2. <aop:aspectj-autoproxy proxy-target-class="true"/>  
  3.   
  4. <!-- 任務調度器 -->  
  5. <task:scheduler id="scheduler" pool-size="10"/>  
  6.   
  7. <!-- 任務執行器 -->  
  8. <task:executor id="executor" pool-size="10"/>  
  9.   
  10. <!--開啟注解調度支持 @Async @Scheduled-->  
  11. <task:annotation-driven executor="executor" scheduler="scheduler" proxy-target-class="true"/>  

 

 

2.2、配置監聽器讓其支持異步調用

Java代碼   收藏代碼
  1. @Component  
  2. public class EmailRegisterListener implements ApplicationListener<RegisterEvent> {  
  3.     @Async  
  4.     @Override  
  5.     public void onApplicationEvent(final RegisterEvent event) {  
  6.         System.out.println("注冊成功,發送確認郵件給:" + ((User)event.getSource()).getUsername());  
  7.     }  
  8. }  

使用@Async注解即可,非常簡單。 

 

這樣不僅可以支持通過調用,也支持異步調用,非常的靈活,實際應用推薦大家使用這種方式。

 

 

通過如上,大體了解了Spring的事件機制,可以使用該機制非常簡單的完成如注冊流程,而且對於比較耗時的調用,可以直接使用Spring自身的異步支持來優化。

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 3     xmlns:tx="http://www.springframework.org/schema/tx"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" 
 6     xmlns:mvc="http://www.springframework.org/schema/mvc"
 7     xmlns:task="http://www.springframework.org/schema/task"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans    
 9     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    
10     http://www.springframework.org/schema/tx    
11     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd   
12     http://www.springframework.org/schema/context   
13     http://www.springframework.org/schema/context/spring-context-4.0.xsd   
14     http://www.springframework.org/schema/mvc   
15     http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
16     http://www.springframework.org/schema/task
17     http://www.springframework.org/schema/task/spring-task-4.0.xsd">
18 
19     <context:annotation-config />
20     <!--掃描注解 -->
21     <context:component-scan base-package="com.tf" />
22     <!-- 支持異步方法執行 -->
23     <task:annotation-driven /> 
View Code

 

這個注解用於標注某個方法或某個類里面的所有方法都是需要異步處理的。被注解的方法被調用的時候,會在新線程中執行,而調用它的方法會在原來的線程中執行。這樣可以避免阻塞、以及保證任務的實時性。適用於處理log、發送郵件、短信……等。

注解的應用范圍:
  • 類:表示這個類中的所有方法都是異步的
  • 方法:表示這個方法是異步的,如果類也注解了,則以這個方法的注解為准

相關的配置:
<task:annotation-driven />配置:
  • executor:指定一個缺省的executor給@Async使用。
例子:
<task:annotation-driven executor="asyncExecutor" />

<task:executor />配置參數:
  • id:當配置多個executor時,被@Async("id")指定使用;也被作為線程名的前綴。
  • pool-size
  • core size:最小的線程數,缺省:1
  • max size:最大的線程數,缺省:Integer.MAX_VALUE
  • queue-capacity:當最小的線程數已經被占用滿后,新的任務會被放進queue里面,當這個 queue的capacity也被占滿之后,pool里面會創建新線程處理這個任務,直到總線程數達到了max size,這時系統會拒絕這個任務並拋出TaskRejectedException異常(缺省配置的情況下,可以通過rejection-policy 來決定如何處理這種情況)。缺省值為:Integer.MAX_VALUE
  • keep-alive:超過core size的那些線程,任務完成后,再經過這個時長(秒)會被結束掉
  • rejection-policy:當pool已經達到max size的時候,如何處理新任務
  • ABORT(缺省):拋出TaskRejectedException異常,然后不執行
  • DISCARD:不執行,也不拋出異常
  • DISCARD_OLDEST:丟棄queue中最舊的那個任務
  • CALLER_RUNS:不在新線程中執行任務,而是有調用者所在的線程來執行


免責聲明!

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



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