前面介紹了Spring Boot 如何整合定時任務,不清楚的朋友可以看看之前的文章,
今天主要講解Spring Boot中的另外一個任務:異步任務。所謂異步任務,其實就是異步執行程序,有些時候遇到一些耗時的的任務,如果一直卡等待,肯定會影響其他程序的執行,所以就讓這些程序需要以異步的方式去執行。那麼下面就來介紹Spring Boot 如何實現異步任務。
一、使用註解@EnableAsync 開啟異步調用方法
在application啟動類中,加上@EnableAsync註解,Spring Boot 會自動掃描異步任務。
<code>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);
}
}/<code>
二、創建異步執行類,定義@Component及@Async組件
創建com.weiz.tasks包,在tasks包裡增加AsyncTask 異步任務類,加上@Component 註解,然後在需要異步執行的方法前面加上@Async註解,這樣Spring Boot容器掃描到相關異步方法之後,調用時就會將這些方法異步執行。
<code>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);
}
}/<boolean>/<boolean>/<boolean>/<code>
說明:@Async 加上這個註解,就表示該方法是異步執行方法。
三、調用
創建一個DoTask調用類,我們看看這幾個方法,是怎麼執行的:
<code>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;
}
}/<boolean>/<boolean>/<boolean>/<code>
四、測試
啟動程序之後,在瀏覽器輸入:http://localhost:8080/tasks/test1 。
從這個總耗時可以看出,三個方法確實是異步執行的。耗時接近時間最長的doTask11方法。
最後
以上,就把Spring Boot 創建異步任務的方法簡單介紹完了,是不是特別簡單。
閱讀更多 章為忠學架構 的文章
關鍵字: annotation Future springframework