springboot之异步调用@Async
生活随笔
收集整理的這篇文章主要介紹了
springboot之异步调用@Async
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
引言: 在Java應用中,絕大多數情況下都是通過同步的方式來實現交互處理的;但是在處理與第三方系統交互的時候,容易造成響應遲緩的情況,之前大部分都是使用多線程來完成此類任務,其實,在spring 3.x之后,就已經內置了@Async來完美解決這個問題,本文將介紹在springboot中如何使用@Async。
1、pom.xml中導入必要的依賴:
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version></parent><dependencies><!-- SpringBoot 核心組件 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></dependency></dependencies>
?
2、寫一個springboot的啟動類:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;@ComponentScan(basePackages = { "com.xwj.controller", "com.xwj.service" })
@EnableAsync //開啟異步調用
@EnableAutoConfiguration
public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}} 注意在這里一定要加上@EnableAsync注解開啟異步調用
?
3、建一個controller包,然后新建一個IndexController類,用來獲取請求
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.xwj.service.UserService;@RestController
public class IndexController {@Autowiredprivate UserService userService;@RequestMapping("/async")public String async(){System.out.println("####IndexController#### 1");userService.sendSms();System.out.println("####IndexController#### 4");return "success";}} ?
4、建一個service包,然后新建一個UserService類:
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;@Service
public class UserService {@Asyncpublic void sendSms(){System.out.println("####sendSms#### 2");IntStream.range(0, 5).forEach(d -> {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}});System.out.println("####sendSms#### 3");}} ?
先注掉@EnableAsync和@Async兩個注解,看下同步調用執行的效果。執行結果如下:
####IndexController#### 1 ####sendSms#### 2 ####sendSms#### 3 ####IndexController#### 4
對于sendSms方法,我們并不關注它什么時候執行完,所以可以采用異步的方式去執行。放開@EnableAsync和@Async兩個注解,執行結果如下:
####IndexController#### 1 ####IndexController#### 4 ####sendSms#### 2 ####sendSms#### 3
bingo!達到了我們預期的效果
總結:
使用了@Async的方法,會被當成是一個子線程,所有整個sendSms方法,會在主線程執行完了之后執行
?
知識改變世界轉載于:https://www.cnblogs.com/lywJ/p/10937985.html
總結
以上是生活随笔為你收集整理的springboot之异步调用@Async的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 返回函数
- 下一篇: ★☆★☆★☆求助~~~~山东哪里有好玩的