異步執行一般用來發送一些消息數據,數據一致性不要求太高的場景,對於spring來說,它把這個異步進行了封裝,使用一個注解就可以實現。
Spring中通過在方法上設置@Async
注解,可使得方法被異步調用。也就是說該方法會在調用時立即返回,而這個方法的實際執行交給Spring的TaskExecutor去完成。
用法
- 程序啟動時開啟
@EnableAsync
注解 - 建立新的類型,建立
異步方法
,為方法添加@Async
注解 -
在業務代碼中,
@Autowired
注入你的類型,使用它即可
我們可以關注到在配置task的時候,是有參數讓我們配置線程池的數量的。因為這種實現方法,所以在同一個類中的方法調用,添加@async注解是失效的!,原因是當你在同一個類中的時候,方法調用是在類體內執行的,spring無法截獲這個方法調用.
事例
Spring的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <!-- 包掃描 --> <context:component-scan base-package="com.gdut"/> <!-- 執行異步任務的線程池TaskExecutor --> <task:executor id="myexecutor" pool-size="5" /> <task:annotation-driven executor="myexecutor"/> </beans>
如果是在springboot項目中使用的話,則更加簡單。只需要在啟動類上面加一個注解:@EnableAsync即可,如:
package com.gdut; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication @EnableAsync //開啟異步調用 public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
接下來我們要進入實例部分,我通過一個聊天對話的demo來介紹。
調用方法依次為1,2,3。現在我想實現的是如下場景:
(1)A:你愛我嗎?
(3)A:你不回我,肯定不愛我,分手吧!
(2)B:當然愛你!(這里假設有延遲,導致消息不及時,A沒有收到)
如果這里不用異步實現的話,在3之前一定會等到2完成,所以最終導致對話是:
(1)A:你愛我嗎?
(2)B:當然愛你!(沒有延遲的情況下)
(3)A:你不回我,肯定不愛我,分手吧!
不符合我們的要求,所以這里我們必須采用的是異步。
現在我們先什么都不加,相關代碼如下:
package com.gdut.conponent; import org.springframework.stereotype.Component; @Component public class ChatTest { public void chat1(){ System.out.println("你愛我嗎?"); } public void chat2(){ try { Thread.sleep(2*1000); System.out.println("等了大概2秒...!"); System.out.println("當然愛呀!"); } catch (InterruptedException e) { e.printStackTrace(); } } public void chat3(){ System.out.println("你回的這么慢,肯定不愛我。分手!"); } }
package com.gdut.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.gdut.conponent.ChatTest; @RestController @RequestMapping("/chat") public class ChatController { @Autowired private ChatTest chatTest; @RequestMapping("/chatTest") public String chatTest(){ chatTest.chat1(); chatTest.chat2(); chatTest.chat3(); return "成功"; } }
console輸出:
可以看到我們的目的還沒有達到,現在我們在chat2方法上面加上@Async注解
package com.gdut.conponent; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; @Component public class ChatTest { public void chat1(){ System.out.println("你愛我嗎?"); } @Async public void chat2(){ try { Thread.sleep(2*1000); System.out.println("等了大概2秒...!"); System.out.println("當然愛呀!"); } catch (InterruptedException e) { e.printStackTrace(); } } public void chat3(){ System.out.println("你回的這么慢,肯定不愛我。分手!"); } }
console輸出:
在調用方法3的時候,還沒有等到方法2執行結束便執行了3。所以才能達到我們最終的情境。
Async幾種方式
1:沒有返回值的,不會阻塞主線程,相當於開啟新線程在后台執行這個任務
@Async public String sayHello2() throws InterruptedException { Thread.sleep(2 * 1000);//網絡連接中 。。。消息發送中。。。 return "我愛你啊!";// 調用方調用后會立即返回,所以返回null }
2:帶有返回值的,返回類型必須為Future<>,它會開啟新的線程執行任務,並阻塞主線程,執行完成后把結果返回給主線程
@Async public Future<String> asyncFunc() throws InterruptedException { int thinking = 2; Thread.sleep(thinking * 1000); System.out.println("async!"); return new AsyncResult<String>("發送消息用了" + thinking + "秒"); }
調用方法
@GetMapping("/lind-demo/asyncFunc") public void async() throws Exception { Future<String> future = null; future = asyncService.asyncFunc(); System.out.println(future.get()); System.out.println("主線程被阻塞執行完成"); }
執行結果
async! 發送消息用了2秒 主線程執行完成
@Async的使用注意點
- 返回值:不要返回值直接void;需要返回值用AsyncResult或者CompletableFuture
- 所使用的@Async注解方法的類對象應該是Spring容器管理的bean對象
- 調用異步方法類上需要配置上注解@EnableAsync
- 可自定義執行器並指定例如:@Async("otherExecutor")
@Async
必須不同類間調用: A類—>B類.C方法()(@Async
注釋在B類/方法中),如果在同一個類中調用,會變同步執行,例如:A類.B()—>A類.@Async C()。- @Async也可以加到類,表示這個類的所有方法都是異步執行,並且方法上的注解會覆蓋類上的注解。但一般不這么用!
總結
其實在我們實際應用中,大多數方法都是用同步的。但是在處理與第三方系統交互的時候,容易造成響應遲緩的情況,之前大部分都是使用多線程來完成此類任務,其實,在spring 3.x之后,就已經內置了@Async來完美解決這個問題。