前面介紹了Spring Boot 如何整合定時任務,不清楚的朋友能夠看看以前的文章:https://www.cnblogs.com/zhangweizhong/category/1657780.html。html
今天主要講解Spring Boot中的另一個任務:異步任務。所謂異步任務,其實就是異步執行程序,有些時候遇到一些耗時的的任務,若是一直卡等待,確定會影響其餘程序的執行,因此就讓這些程序須要以異步的方式去執行。那麼下面就來介紹Spring Boot 如何實現異步任務。java
在application啓動類中,加上@EnableAsync註解,Spring Boot 會自動掃描異步任務。web
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); } }
建立com.weiz.tasks包,在tasks包裏增長AsyncTask 異步任務類,加上@Component 註解,而後在須要異步執行的方法前面加上@Async註解,這樣Spring Boot容器掃描到相關異步方法以後,調用時就會將這些方法異步執行。spring
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調用類,咱們看看這幾個方法,是怎麼執行的:springboot
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方法。mybatis
以上,就把Spring Boot 建立異步任務的方法簡單介紹完了,是否是特別簡單。架構
這個系列課程的完整源碼,也會提供給你們。你們關注個人微信公衆號(架構師精進),回覆:springboot源碼。獲取這個系列課程的完整源碼。app