天天看點

SpringBoot異步任務@Async

一. 什麼是異步調用?

異步調用是相對于同步調用而言的,同步調用是指程式按預定順序一步步執行,每一步必須等到上一步執行完後才能執行,異步調用則無需等待上一步程式執行完即可執行。

二. 如何實作異步調用?

多線程,這是很多人第一眼想到的關鍵詞,沒錯,多線程就是一種實作異步調用的方式。在非spring目項目中我們要實作異步調用的就是使用多線程方式,可以自己實作Runable接口或者內建Thread類,或者使用jdk1.5以上提供了的Executors線程池。在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的啟動類:

啟動類裡面使用@EnableAsync注解開啟功能,自動掃描
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
public class SpringBootAsyncApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootAsyncApplication.class, args);
    }
}
           

3、建一個service包,然後建立一個UserService類:

  • 要在異步任務的類上寫@Component
  • 在定義異步任務類寫@Async(寫在類上代表整個類都是異步,在方法加上代表該類異步執行)
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");
    }

}
           

4、建一個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.jeffrey.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";
    }
    
}
           

先注掉@EnableAsync和@Async兩個注解,看下同步調用執行的效果。執行結果如下:

####IndexController####   1
####sendSms####   2
####sendSms####   3
####IndexController####   4
           

對于sendSms方法,我們并不關注它什麼時候執行完,是以可以采用異步的方式去執行。放開@EnableAsync和@Async兩個注解,執行結果如下:

####IndexController####   1
####IndexController####   4
####sendSms####   2
####sendSms####   3
           
總結:使用了@Async的方法,會被當成是一個子線程,所有整個sendSms方法,會在主線程執行完了之後執行

四. 舉例說明(須知道執行結果):

基于上面例子,這裡隻貼核心代碼

1、啟動類

@SpringBootApplication
@EnableAsync   //開啟異步任務
public class MainApplication {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}
           

2、異步類

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

/**
 * @ClassName AsyncTestTask
 * @Author jeffrey
 * @Description 異步任務業務類
 **/
@Component
@Async
public class AsyncTestTask {
    //擷取異步結果
    public Future<String> task4() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(2000L);
        long end = System.currentTimeMillis();
        System.out.println("任務4耗時="+(end-begin));
        return new AsyncResult<String>("任務4");
    }


    public Future<String> task5() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(3000L);
        long end = System.currentTimeMillis();
        System.out.println("任務5耗時="+(end-begin));
        return new AsyncResult<String>("任務5");
    }

    public Future<String> task6() throws InterruptedException{
        long begin = System.currentTimeMillis();
        Thread.sleep(1000L);
        long end = System.currentTimeMillis();
        System.out.println("任務6耗時="+(end-begin));
        return new AsyncResult<String>("任務6");
    }
}
           
import com.ceair.service.AsyncTestTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

/**
 * @ClassName UserController
 * @Author jeffrey
 * @Description User
 **/

@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private AsyncTestTask asyncTestTask;

    @GetMapping("userAsyncTask")
    public String exeTask() throws InterruptedException{

        long begin = System.currentTimeMillis();

        Future<String> task4 = asyncTestTask.task4();
        Future<String> task5 = asyncTestTask.task5();
        Future<String> task6 = asyncTestTask.task6();

        //如果都執行往就可以跳出循環,isDone方法如果此任務完成,true
        for(;;){
            if (task4.isDone() && task5.isDone() && task6.isDone()) {
                break;
            }
        }

        long end = System.currentTimeMillis();
        long total = end-begin;
        System.out.println("執行總耗時="+total);
        return String.valueOf(total);
    }
}
           
任務6耗時=1000
任務4耗時=2000
任務5耗時=3000
執行總耗時=3012