(九)如何實現異步執行任務


前面介紹了Spring Boot 如何整合定時任務,不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/1657780.html

今天主要講解Spring Boot中的另外一個任務:異步任務。所謂異步任務,其實就是異步執行程序,有些時候遇到一些耗時的的任務,如果一直卡等待,肯定會影響其他程序的執行,所以就讓這些程序需要以異步的方式去執行。那么下面就來介紹Spring Boot 如何實現異步任務。

 

一、使用注解@EnableAsync 開啟異步調用方法

在application啟動類中,加上@EnableAsync注解,Spring Boot 會自動掃描異步任務。

復制代碼
復制代碼
package com.weiz;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
//掃描 mybatis mapper 包路徑
@MapperScan(basePackages = "com.weiz.mapper")
//掃描 所有需要的包, 包含一些自用的工具類包 所在的路徑
@ComponentScan(basePackages = {"com.weiz","org.n3r.idworker"})
//開啟定時任務
@EnableScheduling
//開啟異步調用方法
@EnableAsync
public class SpringBootStarterApplication {
public static void main(String[] args) { SpringApplication.run(SpringBootStarterApplication.class, args); } }
復制代碼
復制代碼

 

二、創建異步執行類,定義@Component及@Async組件

創建com.weiz.tasks包,在tasks包里增加AsyncTask 異步任務類,加上@Component 注解,然后在需要異步執行的方法前面加上@Async注解,這樣Spring Boot容器掃描到相關異步方法之后,調用時就會將這些方法異步執行。

復制代碼
package com.weiz.tasks;

import java.util.concurrent.Future;

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

@Component
public class AsyncTask {
    
    @Async
    public Future<Boolean> doTask11() throws Exception {
        long start = System.currentTimeMillis();
        Thread.sleep(1000);
        long end = System.currentTimeMillis();
        System.out.println("任務1耗時:" + (end - start) + "毫秒");
        return new AsyncResult<>(true);
    }
    
    @Async
    public Future<Boolean> doTask22() throws Exception {
        long start = System.currentTimeMillis();
        Thread.sleep(700);
        long end = System.currentTimeMillis();
        System.out.println("任務2耗時:" + (end - start) + "毫秒");
        return new AsyncResult<>(true);
    }
    
    @Async
    public Future<Boolean> doTask33() throws Exception {
        long start = System.currentTimeMillis();
        Thread.sleep(600);
        long end = System.currentTimeMillis();
        System.out.println("任務3耗時:" + (end - start) + "毫秒");
        return new AsyncResult<>(true); 
    }
}
復制代碼

說明:@Async 加上這個注解,就表示該方法是異步執行方法。

 

三、調用

創建一個DoTask調用類,我們看看這幾個方法,是怎么執行的:

復制代碼
package com.weiz.tasks;

import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("tasks")
public class DoTask {

    @Autowired
    private AsyncTask asyncTask;

    @RequestMapping("test1")
    public String test1() throws Exception {

        long start = System.currentTimeMillis();

        Future<Boolean> a = asyncTask.doTask11();
        Future<Boolean> b = asyncTask.doTask22();
        Future<Boolean> c = asyncTask.doTask33();

        while (!a.isDone() || !b.isDone() || !c.isDone()) {
            if (a.isDone() && b.isDone() && c.isDone()) {
                break;
            }
        }

        long end = System.currentTimeMillis();

        String times = "任務全部完成,總耗時:" + (end - start) + "毫秒";
        System.out.println(times);

        return times;
    }
}
復制代碼

 

四、測試

啟動程序之后,在瀏覽器輸入:http://localhost:8080/tasks/test1  。

 

從這個總耗時可以看出,三個方法確實是異步執行的。耗時接近時間最長的doTask11方法。

 

最后

以上,就把Spring Boot 創建異步任務的方法簡單介紹完了,是不是特別簡單。

這個系列課程的完整源碼,也會提供給大家。大家關注我的微信公眾號(架構師精進),回復:springboot源碼。獲取這個系列課程的完整源碼。


免責聲明!

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



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