Spring Boot 非同步運用

2022-07-01 21:27:16 字數 659 閱讀 4823

使用@async標籤 匯入包 org.springframework.scheduling.annotation.async 並配置併發執行緒池asynctaskconfig 實現asyncconfigurer介面 連線池註解 @enableasync @configure

重寫介面getasyncexecutor方法 並返回threadpooltaskexecutor例項 例項化threadpooltaskexecutor 並對其進行初始化:corepoolsize(最小執行緒數)、maxpoolsize(最大執行緒數)、queuecapacity(等待佇列)、awaitterminationsecond(設定執行緒池任務等待時間 超過時間強制關閉)、waitfortaskstocompleteonshutdown(true/false設定執行緒池關閉時間等待所有任務完成再繼續銷毀其他bean) 、initialize 例項化後返回例項。

重寫getasyncuncaughtexceptionhandler()方法

如需等待非同步返回結果

則需引入org.springframework.scheduling.annotation.asyncresult以及util.concurrent.future

非同步服務返回值future《集合》

countdownlatch、cyclicbarrier、semaphore

Spring Boot 非同步任務

可以使用非同步的方式載入方法 只需在專案入口上定義 enableasync 開啟非同步註解功能 以及某個需要非同步方法上面定義 async註解 即可1.專案入口上定義 enableasync 開啟非同步註解功能 enableasync 開啟非同步註解功能 public class 2.使用 asyn...

springboot非同步任務

enablerabbit 開啟rabbit enableasync 開啟非同步 public class 新建乙個service,此service休眠3秒 package com.example.vuelog.service.task import org.springframework.sched...

springboot非同步任務

springboot非同步任務 使用 async 告訴spring這是乙個非同步方法 service public class asynservice catch interruptedexception e system.out.println 資料處理中 主函式使用 enableasync開啟非...