一文搞懂SpringBoot如何利用@Async實(shí)現(xiàn)異步調(diào)用_第1頁
一文搞懂SpringBoot如何利用@Async實(shí)現(xiàn)異步調(diào)用_第2頁
一文搞懂SpringBoot如何利用@Async實(shí)現(xiàn)異步調(diào)用_第3頁
一文搞懂SpringBoot如何利用@Async實(shí)現(xiàn)異步調(diào)用_第4頁
一文搞懂SpringBoot如何利用@Async實(shí)現(xiàn)異步調(diào)用_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第一文搞懂SpringBoot如何利用@Async實(shí)現(xiàn)異步調(diào)用目錄前言簡(jiǎn)單使用異步處理異步回調(diào)自定義線程池注意事項(xiàng)(一定注意)

前言

異步調(diào)用幾乎是處理高并發(fā),解決性能問題常用的手段,如何開啟異步調(diào)用?SpringBoot中提供了非常簡(jiǎn)單的方式,就是一個(gè)注解@Async。今天我們重新認(rèn)識(shí)一下@Async,以及注意事項(xiàng)

簡(jiǎn)單使用

新建三個(gè)作業(yè)任務(wù):

@Service

publicclassTaskDemo{

privatestaticLoggerlogger=LoggerFactory.getLogger(TaskDemo.class);

publicvoidexecute1(){

("處理耗時(shí)任務(wù)1......開始");

try{

TimeUnit.SECONDS.sleep(3);

}catch(InterruptedExceptione){

e.printStackTrace();

("處理耗時(shí)任務(wù)1......結(jié)束");

publicvoidexecute2(){

("處理耗時(shí)任務(wù)2......開始");

try{

TimeUnit.SECONDS.sleep(3);

}catch(InterruptedExceptione){

e.printStackTrace();

("處理耗時(shí)任務(wù)2......結(jié)束");

publicvoidexecute3(){

("處理耗時(shí)任務(wù)3......開始");

try{

TimeUnit.SECONDS.sleep(3);

}catch(InterruptedExceptione){

e.printStackTrace();

("處理耗時(shí)任務(wù)3......結(jié)束");

}

測(cè)試代碼:

@RestController

publicclassTaskController{

@Autowired

privateTaskDemotaskDemo;

@GetMapping("/task/test")

publicStringtestTask(){

taskDemo.execute1();

taskDemo.execute2();

taskDemo.execute3();

return"ok";

}

執(zhí)行后我們可以發(fā)現(xiàn),上面的代碼是同一個(gè)線程的同步執(zhí)行,整體耗時(shí)9秒才完成。

異步處理

springboot的異步,是非常簡(jiǎn)單的,加2個(gè)注解即可

@Service

publicclassTaskDemo{

privatestaticLoggerlogger=LoggerFactory.getLogger(TaskDemo.class);

@Async

publicvoidexecute1(){

("處理耗時(shí)任務(wù)1......開始");

try{

TimeUnit.SECONDS.sleep(3);

}catch(InterruptedExceptione){

e.printStackTrace();

("處理耗時(shí)任務(wù)1......結(jié)束");

@Async

publicvoidexecute2(){

("處理耗時(shí)任務(wù)2......開始");

try{

TimeUnit.SECONDS.sleep(3);

}catch(InterruptedExceptione){

e.printStackTrace();

("處理耗時(shí)任務(wù)2......結(jié)束");

@Async

publicvoidexecute3(){

("處理耗時(shí)任務(wù)3......開始");

try{

TimeUnit.SECONDS.sleep(3);

}catch(InterruptedExceptione){

e.printStackTrace();

("處理耗時(shí)任務(wù)3......結(jié)束");

}

@SpringBootApplication

@EnableAsync

publicclassDemoApp{

publicstaticvoidmain(String[]args){

SpringApplication.run(DemoApp.class,args);

}

增加了@Async和@EnableAsync兩個(gè)注解

從執(zhí)行結(jié)果發(fā)現(xiàn),整個(gè)流程用了3秒,以及用了3個(gè)線程執(zhí)行。完成了異步調(diào)用

異步回調(diào)

有些場(chǎng)景我們需要知道異步處理的任務(wù)什么時(shí)候完成,需要做額外的業(yè)務(wù)處理。如:我們需要在3個(gè)任務(wù)都完成后,提示一下給用戶

@Service

publicclassTaskDemo{

privatestaticLoggerlogger=LoggerFactory.getLogger(TaskDemo.class);

@Async

publicFutureStringexecute1(){

("處理耗時(shí)任務(wù)1......開始");

try{

TimeUnit.SECONDS.sleep(3);

}catch(InterruptedExceptione){

e.printStackTrace();

("處理耗時(shí)任務(wù)1......結(jié)束");

returnnewAsyncResult("任務(wù)1ok");

@Async

publicFutureStringexecute2(){

("處理耗時(shí)任務(wù)2......開始");

try{

TimeUnit.SECONDS.sleep(3);

}catch(InterruptedExceptione){

e.printStackTrace();

("處理耗時(shí)任務(wù)2......結(jié)束");

returnnewAsyncResult("任務(wù)2ok");

@Async

publicFutureStringexecute3(){

("處理耗時(shí)任務(wù)3......開始");

try{

TimeUnit.SECONDS.sleep(3);

}catch(InterruptedExceptione){

e.printStackTrace();

("處理耗時(shí)任務(wù)3......結(jié)束");

returnnewAsyncResult("任務(wù)3ok");

}

@RestController

publicclassTaskController{

privatestaticLoggerlogger=LoggerFactory.getLogger(TaskController.class);

@Autowired

privateTaskDemotaskDemo;

@GetMapping("/task/test")

publicStringtestTask()throwsInterruptedException{

FutureStringtask1=taskDemo.execute1();

FutureStringtask2=taskDemo.execute2();

FutureStringtask3=taskDemo.execute3();

while(true){

if(task1.isDone()task2.isDone()task3.isDone()){

break;

TimeUnit.SECONDS.sleep(1);

("3個(gè)任務(wù)都處理完成");

return"ok";

}

執(zhí)行結(jié)果發(fā)現(xiàn),在請(qǐng)求線程里面給用戶提示了3個(gè)任務(wù)都處理完成了。

這段代碼主要改變了什么:

1、把具體任務(wù)返回類型改為了Future類型對(duì)象

2、在調(diào)用任務(wù)時(shí),循環(huán)判斷任務(wù)是否處理完

自定義線程池

說到異步處理,一定要考慮到線程池,什么是線程池,小伙伴可自行網(wǎng)補(bǔ)。@Async的線程池定義比較方便,直接上代碼:

@Configuration

publicclassThreadPoolConfig{

@Bean(name="taskPool01Executor")

publicThreadPoolTaskExecutorgetTaskPool01Executor(){

ThreadPoolTaskExecutortaskExecutor=newThreadPoolTaskExecutor();

//核心線程數(shù)

taskExecutor.setCorePoolSize(10);

//線程池維護(hù)線程的最大數(shù)量,只有在緩沖隊(duì)列滿了之后才會(huì)申請(qǐng)超過核心線程數(shù)的線程

taskExecutor.setMaxPoolSize(100);

//緩存隊(duì)列

taskExecutor.setQueueCapacity(50);

//許的空閑時(shí)間,當(dāng)超過了核心線程出之外的線程在空閑時(shí)間到達(dá)之后會(huì)被銷毀

taskExecutor.setKeepAliveSeconds(200);

//異步方法內(nèi)部線程名稱

taskExecutor.setThreadNamePrefix("TaskPool-01-");

*當(dāng)線程池的任務(wù)緩存隊(duì)列已滿并且線程池中的線程數(shù)目達(dá)到maximumPoolSize,如果還有任務(wù)到來就會(huì)采取任務(wù)拒絕策略

*通常有以下四種策略:

*ThreadPoolExecutor.AbortPolicy:丟棄任務(wù)并拋出RejectedExecutionException異常。

*ThreadPoolExecutor.DiscardPolicy:也是丟棄任務(wù),但是不拋出異常。

*ThreadPoolExecutor.DiscardOldestPolicy:丟棄隊(duì)列最前面的任務(wù),然后重新嘗試執(zhí)行任務(wù)(重復(fù)此過程)

*ThreadPoolExecutor.CallerRunsPolicy:重試添加當(dāng)前的任務(wù),自動(dòng)重復(fù)調(diào)用execute()方法,直到成功

taskExecutor.setRejectedExecutionHandler(newThreadPoolExecutor.AbortPolicy());

taskExecutor.setWaitForTasksToCompleteOnShutdown(true);

taskExecutor.initialize();

returntaskExecutor;

@Bean(name="taskPool02Executor")

publicThreadPoolTaskExecutorgetTaskPool02Executor(){

ThreadPoolTaskExecutortaskExecutor=newThreadPoolTaskExecutor();

//核心線程數(shù)

taskExecutor.setCorePoolSize(10);

//線程池維護(hù)線程的最大數(shù)量,只有在緩沖隊(duì)列滿了之后才會(huì)申請(qǐng)超過核心線程數(shù)的線程

taskExecutor.setMaxPoolSize(100);

//緩存隊(duì)列

taskExecutor.setQueueCapacity(50);

//許的空閑時(shí)間,當(dāng)超過了核心線程出之外的線程在空閑時(shí)間到達(dá)之后會(huì)被銷毀

taskExecutor.setKeepAliveSeconds(200);

//異步方法內(nèi)部線程名稱

taskExecutor.setThreadNamePrefix("TaskPool-02-");

*當(dāng)線程池的任務(wù)緩存隊(duì)列已滿并且線程池中的線程數(shù)目達(dá)到maximumPoolSize,如果還有任務(wù)到來就會(huì)采取任務(wù)拒絕策略

*通常有以下四種策略:

*ThreadPoolExecutor.AbortPolicy:丟棄任務(wù)并拋出RejectedExecutionException異常。

*ThreadPoolExecutor.DiscardPolicy:也是丟棄任務(wù),但是不拋出異常。

*ThreadPoolExecutor.DiscardOldestPolicy:丟棄隊(duì)列最前面的任務(wù),然后重新嘗試執(zhí)行任務(wù)(重復(fù)此過程)

*ThreadPoolExecutor.CallerRunsPolicy:重試添加當(dāng)前的任務(wù),自動(dòng)重復(fù)調(diào)用execute()方法,直到成功

taskExecutor.setRejectedExecutionHandler(newThreadPoolExecutor.AbortPolicy());

taskExecutor.setWaitForTasksToCompleteOnShutdown(true);

taskExecutor.initialize();

returntaskExecutor;

}

定義了2個(gè)線程池Bean

@Service

publicclassTaskDemo{

privatestaticLoggerlogger=LoggerFactory.getLogger(TaskDemo.class);

@Async("taskPool01Executor")

publicFutureStringexecute1(){

("處理耗時(shí)任務(wù)1......開始");

try{

TimeUnit.SECONDS.sleep(3);

}catch(InterruptedExceptione){

e.printStackTrace();

("處理耗時(shí)任務(wù)1......結(jié)束");

returnnewAsyncResult("任務(wù)1ok");

@Async("taskPool01Executor")

publicFutureStringexecute2(){

("處理耗時(shí)任務(wù)2......開始");

try{

TimeUnit.SECONDS.sleep(3);

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論