同步事件和異步事件
同步事件:在一個線程里,按順序執行業務,做完一件事再去做下一件事.
異步事件:在一個線程里,做一個事的同事,可以另起一個新的線程執行另一件事,這樣兩件事可以同時執行.
用一個例子來解釋同步事件和異步事件的使用場景,有時候一段完整的代碼邏輯,可能分為幾部分,拿最常見的注冊來說,假設完整流程是,1.點擊注冊->2.檢驗信息並存庫->3.發送郵件通知->4.返回給用戶.代碼這么寫是正確,但不是最好的,缺點如下:
1.邏輯復雜,業務耦合,我們把校驗數據並存庫和發送郵件寫到一個大的業務方法里了,發郵件我們可以看做一個相對獨立的業務方法
2.效率低,假設2和3分別需要1秒的時候,那么用戶在點擊注冊2秒后才能看到相應
同步事件可以解決上面第一個問題,我們把發郵件的方法獨立出來,放到事件里執行,這樣注冊的這個方法就可以只做2操作,完成之后發布一個事件去執行3,可以很好的解決業務耦合的問題.
異步事件可以完美解決以上兩個問題,注冊方法執行2操作,執行之后發布一個異步事件,另起一個線程執行3操作,注冊方法所在的線程可直接返回給用戶,這樣不僅實現了業務解耦還提高了效率,用戶點擊注冊,1秒后就能看到響應.
Spring的事件機制
spring事件發送監聽涉及3個部分
ApplicationEvent:表示事件本身,自定義事件需要繼承該類,可以用來傳遞數據,比如上述操作,我們需要將用戶的郵箱地址傳給事件監聽器.
ApplicationEventPublisherAware:事件發送器,通過實現這個接口,來觸發事件.
ApplicationListener:事件監聽器接口,事件的業務邏輯封裝在監聽器里面.
接下來使用spring的異步事件機制來模擬上面的注冊流程.有配置文件和注解兩種方式.
配置文件的方式:
新建TestEvent:
1 public class TestEvent extends ApplicationEvent { 2 3 private TestParam source; 4 5 public TestEvent(TestParam source) { 6 super(source); 7 this.source = source; 8 } 9 } 10 11 @Data 12 public class TestParam { 13 private String email; 14 }
新建TestListener:
1 @Component 2 public class TestListener implements ApplicationListener<TestEvent> { 3 4 @Override 5 public void onApplicationEvent(TestEvent testEvent) { 6 7 TestParam param = (TestParam) testEvent.getSource(); 8 System.out.println(".......開始......."); 9 System.out.println("發送郵件:"+param.getEmail()); 10 System.out.println(".......結束....."); 11 } 12 }
新建EventPublisher:
@Component public class TestPublish implements ApplicationEventPublisherAware { private static ApplicationEventPublisher applicationEventPublisher; @Override public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { TestPublish.applicationEventPublisher = applicationEventPublisher; } public static void publishEvent(ApplicationEvent communityArticleEvent) { applicationEventPublisher.publishEvent(communityArticleEvent); } }
spring-context.xml中添加:
1 <bean id="applicationEventAsyncMulticaster" class="org.springframework.context.event.SimpleApplicationEventMulticaster"> 2 <property name="taskExecutor"> 3 <bean class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> 4 <property name="corePoolSize" value="5"/> 5 <property name="keepAliveSeconds" value="3000"/> 6 <property name="maxPoolSize" value="50"/> 7 <property name="queueCapacity" value="200"/> 8 </bean> 9 </property> 10 </bean>
注意:如果加<propery name="taskExecutor",則使用異步方式執行,否則為同步方式
使用注解方式
使用@Async需要在配置文件添加一下支持,線程池也是需要配置一下的
<!-- 開啟@AspectJ AOP代理 --> <aop:aspectj-autoproxy proxy-target-class="true"/> <!-- 任務執行器 --> <task:executor id="executor" pool-size="10"/> <!--開啟注解調度支持 @Async --> <task:annotation-driven executor="executor" proxy-target-class="true"/>
TestListener中在方法中添加@Async
1 @Component 2 public class TestListener implements ApplicationListener<TestEvent> { 3 4 @Async 5 @Override 6 public void onApplicationEvent(TestEvent testEvent) { 7 8 TestParam param = (TestParam) testEvent.getSource(); 9 System.out.println(".......開始......."); 10 System.out.println("發送郵件:"+param.getEmail()); 11 System.out.println(".......結束....."); 12 } 13 }
Listener其實還可以做得更徹底一點,使用注解@EventListener可代替實現ApplicationListener,原理是通過掃描這個注解來創建監聽器並自動添加到ApplicationContext中.
新建自定義EventHandler:
1 @Component 2 public class TestEventHandler { 3 4 @Async 5 @EventListener 6 public void handleTestEvent(TestEvent testEvent) { 7 8 TestParam param = (TestParam) testEvent.getSource(); 9 System.out.println(".......開始......."); 10 System.out.println("發送郵件:"+param.getEmail()); 11 System.out.println(".......結束....."); 12 } 13 }
測試及控制台的打印就不貼了,這里主要記錄一下具體的實現方法.
總結:
使用spring事件機制能很好地幫助我們消除不同業務間的耦合關系,也可以提高執行效率,應該根據業務場景靈活選擇.