1. 程式人生 > >springboot 異步調用Async使用方法

springboot 異步調用Async使用方法

depend except .com XML log index oot 核心 方法

引言: 在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 {
    
    @Autowired
    private 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 {

    @Async
    public 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方法,會在主線程執行完了之後執行

調用異步方法的類 和 異步方法所在的類 不能在同一個類裏,這一點尤其要註意,否則達不到異步的效果!!

springboot 異步調用Async使用方法