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

       同一個類中,一個方法調用另外一個有@Async的方法,注解是不會生效的

 


免責聲明!

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



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