Springboot使用異步方法優(yōu)化Service邏輯,提高接口響應(yīng)速度方式
使用異步方法優(yōu)化Service邏輯,提高接口響應(yīng)速度
一、業(yè)務(wù)場景
例如生成驗(yàn)證碼和發(fā)送驗(yàn)證碼組成的業(yè)務(wù),其實(shí)無需等到真正發(fā)送成功驗(yàn)證碼才對客戶端進(jìn)行響應(yīng),可以讓短信發(fā)送者一個(gè)耗時(shí)操作轉(zhuǎn)為異步執(zhí)行
二、異步任務(wù)在springboot的使用
@RestController
public class AsyncArticleController {
@Autowired
private ArticleService articleService;
/**
* 模擬獲取文章后閱讀量+1
*/
@PostMapping("/article")
public String getArticle() {
// 查詢文章
String article = articleService.selectArticle();
// 閱讀量+1
articleService.updateReadCount();
System.out.println("getArticle文章閱讀業(yè)務(wù)執(zhí)行完畢");
return article;
}
}
@Service
public class ArticleService {
// 查詢文章
public String selectArticle() {
// TODO 模擬文章查詢操作
System.out.println("查詢?nèi)蝿?wù)線程,線程名:"+Thread.currentThread().getName());
return "文章詳情";
}
// 文章閱讀量+1
@Async
public void updateReadCount() {
// TODO 模擬耗時(shí)操作
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("更新任務(wù)線程,線程名:"+Thread.currentThread().getName());
}
}
@SpringBootApplication
@EnableAsync
public class SpringbootRunnerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootRunnerApplication.class, args);
}
}

注意:
@EnableAsync // 使用異步方法時(shí)需要提前開啟(在啟動(dòng)類上或配置類上) @Async // 被async注解修飾的方法由SpringBoot默認(rèn)線程池(SimpleAsyncTaskExecutor)執(zhí)行
三、自定義線程池執(zhí)行異步方法
第一步配置自定義線程池
package com.hl.springbootrunner.asyncdemo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@EnableAsync // 開啟多線程, 項(xiàng)目啟動(dòng)時(shí)自動(dòng)創(chuàng)建
@Configuration
public class AsyncConfig {
@Bean("readCountExecutor") //指定自定義線程池名稱
public ThreadPoolTaskExecutor asyncOperationExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 設(shè)置核心線程數(shù)
executor.setCorePoolSize(8);
// 設(shè)置最大線程數(shù)
executor.setMaxPoolSize(20);
// 設(shè)置隊(duì)列大小
executor.setQueueCapacity(Integer.MAX_VALUE);
// 設(shè)置線程活躍時(shí)間(秒)
executor.setKeepAliveSeconds(60);
// 設(shè)置線程名前綴+分組名稱
executor.setThreadNamePrefix("AsyncOperationThread-");
executor.setThreadGroupName("AsyncOperationGroup");
// 所有任務(wù)結(jié)束后關(guān)閉線程池
executor.setWaitForTasksToCompleteOnShutdown(true);
// 初始化
executor.initialize();
return executor;
}
}
第二步, 在@Async注解上指定執(zhí)行的線程池,ArticleService中指定執(zhí)行的線程池
// 文章閱讀量+1,指定線程池
@Async("readCountExecutor")
public void updateReadCountByExecutor() {
// TODO 模擬耗時(shí)操作
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("更新任務(wù)線程,線程名:"+Thread.currentThread().getName());
}
第三步,在AsyncArcicleController中
/**
* 模擬獲取文章后閱讀量+1,指定線程池
*/
@PostMapping("/articleByExecutor")
public String getArticleByExecutor() {
// 查詢文章
String article = articleService.selectArticle();
// 閱讀量+1
articleService.updateReadCountByExecutor();
System.out.println("getArticleByExecutor文章閱讀業(yè)務(wù)執(zhí)行完畢");
return article;
}

四、捕獲(無返回值的)異步方法中的異常
自定義異常處理類CustomAsyncExceptionHandler
@Component
public class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {
System.out.println("異常捕獲---------------------------------");
System.out.println("Exception message - " + throwable.getMessage());
System.out.println("Method name - " + method.getName());
for (Object param : obj) {
System.out.println("Parameter value - " + param);
}
System.out.println("異常捕獲---------------------------------");
}
}
@Async("readCountExecutor")
public void updateReadCountNoReturnByExecutor() {
// TODO 模擬耗時(shí)操作
try {
Thread.sleep(3000);
int i = 1/0;
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("更新任務(wù)線程,線程名:"+Thread.currentThread().getName());
}

五、捕獲(有返回值)異步方法中的異常
使用Future類及其子類來接收異步方法返回值
// 文章閱讀量+1
@Async("readCountExecutor")
public CompletableFuture<Integer> updateReadCountHasResult() {
// TODO 模擬耗時(shí)操作
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("更新文章閱讀量線程"+Thread.currentThread().getName());
return CompletableFuture.completedFuture(100 + 1);
}
@GetMapping("/articleCompletableFuture")
public String getArticleCompletableFuture() throws ExecutionException, InterruptedException {
// 查詢文章
String article = articleService.selectArticle();
// 閱讀量+1
CompletableFuture<Integer> future = articleService.updateReadCountHasResult();
//無返回值的異步方法拋出異常不會(huì)影響Controller的主要業(yè)務(wù)邏輯
//有返回值的異步方法拋出異常會(huì)影響Controller的主要業(yè)務(wù)邏輯
int count = 0;
// 循環(huán)等待異步請求結(jié)果
while (true) {
if(future.isCancelled()) {
System.out.println("異步任務(wù)取消");
break;
}
if (future.isDone()) {
count = future.get();
System.out.println(count);
break;
}
}
System.out.println("getArticleCompletableFuture文章閱讀業(yè)務(wù)執(zhí)行完畢");
return article + count;
}

注意:
- 無返回值的異步方法拋出異常不會(huì)影響Controller的主要業(yè)務(wù)邏輯
- 有返回值的異步方法拋出異常會(huì)影響Controller的主要業(yè)務(wù)邏輯
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java在并發(fā)環(huán)境中SimpleDateFormat多種解決方案
這篇文章主要介紹了Java在并發(fā)環(huán)境中SimpleDateFormat多種解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
fasterxml jackson反序列化時(shí)對于非靜態(tài)內(nèi)部類報(bào)錯(cuò)問題及解決
這篇文章主要介紹了fasterxml jackson反序列化時(shí)對于非靜態(tài)內(nèi)部類報(bào)錯(cuò)問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
SVN報(bào)錯(cuò):Error Updating changes:svn:E155037的解決方案
今天小編就為大家分享一篇關(guān)于SVN報(bào)錯(cuò):Error Updating changes:svn:E155037的解決方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01
springboot項(xiàng)目突然啟動(dòng)緩慢的解決
這篇文章主要介紹了springboot項(xiàng)目突然啟動(dòng)緩慢的解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Springboot項(xiàng)目中實(shí)現(xiàn)微信小程序登錄案例(最新推薦)
文章介紹了如何通過微信開放平臺(tái)的授權(quán)登錄功能實(shí)現(xiàn)Spring Boot項(xiàng)目與微信小程序的微信登錄,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-02-02
java?poi導(dǎo)入純數(shù)字等格式問題及解決
這篇文章主要介紹了java?poi導(dǎo)入純數(shù)字等格式問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

