1. 程式人生 > >Spring Boot入門系列(九)如何實現非同步執行任務

Spring Boot入門系列(九)如何實現非同步執行任務

前面介紹了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原始碼。獲取這個系列課程的完整原始碼。