Spring Boot 揭秘與實戰(七) 實用技術篇 - 異步任務


文章目錄

  1. 1. Spring Boot 集成異步任務
  2. 2. 單發服務模式
  3. 3. 請求應答模式
  4. 4. 源代碼

Spring 對異步任務具有很好的支持。這篇文章,我們透過 Spring Boot 來講解下單發服務模式和請求應答模式。

Spring Boot 集成異步任務

在 Spring Boot 中使用 @EnableAsync 開啟異步支持。

  1. @Configuration
  2. @EnableAsync
  3. public class AsyncConfig {
  4.  
  5. }

現在,我們在 Spring Boot 中,我們只需要通過使用 @Async 注解就能將原來的同步方法變為異步方法。

單發服務模式

多個服務之間邏輯上不存在相互依賴關系,執行先后順序沒有嚴格的要求,邏輯上可以被並行執行。對於單發服務只有請求,沒有應答,很容易設計成異步的。發起服務調用后。立即返回,不需要同步阻塞等待應答。

  1. @Service
  2. public class MsgServer {
  3. @Async
  4. public void sendA() throws Exception {
  5. System.out.println("send A");
  6. Long startTime = System.currentTimeMillis();
  7. Thread.sleep(2000);
  8. Long endTime = System.currentTimeMillis();
  9. System.out.println("耗時:" + (endTime - startTime));
  10. }
  11.  
  12. @Async
  13. public void sendB() throws Exception {
  14. System.out.println("send B");
  15. Long startTime = System.currentTimeMillis();
  16. Thread.sleep(2000);
  17. Long endTime = System.currentTimeMillis();
  18. System.out.println("耗時:" + (endTime - startTime));
  19. }
  20. }

此時,因為我們添加了 @Async 注解,它就變成了異步方法。

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringApplicationConfiguration(classes = WebMain.class)
  3. public class AsyncTest {
  4.  
  5. @Autowired
  6. private MsgServer msgServer;
  7.  
  8. @Test
  9. public void test() throws Exception {
  10. msgServer.sendA();
  11. msgServer.sendB();
  12. }
  13. }

請求應答模式

對於,請求的內容,需要應答,例如我們需要在多個方法調用都完成后,才進行接下來的操作,此時我們可以利用 Java 的 Future-Listener 機制來實現異步服務調用。

  1. @Service
  2. public class MsgFutureServer {
  3. public static Random random = new Random();
  4.  
  5. @Async
  6. public Future<String> sendA() throws Exception {
  7. System.out.println("send A");
  8. Long startTime = System.currentTimeMillis();
  9. Thread.sleep(2000);
  10. Long endTime = System.currentTimeMillis();
  11. System.out.println("耗時:" + (endTime - startTime));
  12. return new AsyncResult<String>("success");
  13. }
  14.  
  15. @Async
  16. public Future<String> sendB() throws Exception {
  17. System.out.println("send B");
  18. Long startTime = System.currentTimeMillis();
  19. Thread.sleep(2000);
  20. Long endTime = System.currentTimeMillis();
  21. System.out.println("耗時:" + (endTime - startTime));
  22. return new AsyncResult<String>("success");
  23. }
  24. }

下面的單元測試,在等待完成兩個異步任務后,再統計具體耗時時長。

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringApplicationConfiguration(classes = WebMain.class)
  3. public class AsyncFutureTest {
  4.  
  5. @Autowired
  6. private MsgFutureServer msgFutureServer;
  7.  
  8. @Test
  9. public void test() throws Exception {
  10. long startTime = System.currentTimeMillis();
  11.  
  12. Future<String> task1 = msgFutureServer.sendA();
  13. Future<String> task2 = msgFutureServer.sendB();
  14.  
  15. while(true) {
  16. if(task1.isDone() && task2.isDone() ) {
  17. break;
  18. }
  19. }
  20.  
  21. long endTime = System.currentTimeMillis();
  22. System.out.println("總耗時:" + (endTime - startTime));
  23. }
  24. }

源代碼

相關示例完整代碼: springboot-action

(完)

 

微信公眾號


免責聲明!

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



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