当前位置: 首页 > news >正文

Spring Boot 集成自定义线程池和异常处理

Spring Boot 集成自定义线程池和异常处理

  • 1.Spring Boot 集成自定义线程池
    • 一、配置自定义线程池
    • 二、使用 @Async 实现声明式异步
    • 三、Controller 中编排异步结果
    • 四、关键注意事项
  • 2.Spring Boot 异步全局异常处理
    • 一、自定义异常处理器
    • 二、注册到 Spring 容器
    • 三、CompletableFuture 的异常处理
    • 四、核心区别总结

1.Spring Boot 集成自定义线程池

在 Spring Boot 中,通过@Configuration配置 Bean,并结合@Async注解或手动注入ThreadPoolTaskExecutor,可实现声明式异步调用。

一、配置自定义线程池

创建配置类,定义ThreadPoolTaskExecutorBean,确保参数可控且线程命名规范。

importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;importjava.util.concurrent.ThreadPoolExecutor;@ConfigurationpublicclassAsyncConfig{@Bean("bizTaskExecutor")publicThreadPoolTaskExecutortaskExecutor(){ThreadPoolTaskExecutorexecutor=newThreadPoolTaskExecutor();executor.setCorePoolSize(5);executor.setMaxPoolSize(10);executor.setQueueCapacity(100);executor.setKeepAliveSeconds(60);executor.setThreadNamePrefix("biz-async-");// 拒绝策略:由调用线程执行,起到背压作用executor.setRejectedExecutionHandler(newThreadPoolExecutor.CallerRunsPolicy());executor.initialize();returnexecutor;}}

二、使用 @Async 实现声明式异步

在 Service 层方法上添加@Async("bizTaskExecutor"),指定使用上述自定义线程池。

注意:

  1. 启动类需添加@EnableAsync
  2. 异步方法必须在另一个类中调用(自调用无效),因为 Spring AOP 基于代理。
importorg.springframework.scheduling.annotation.Async;importorg.springframework.stereotype.Service;importjava.util.concurrent.CompletableFuture;@ServicepublicclassOrderService{@Async("bizTaskExecutor")publicCompletableFuture<String>queryUserAsync(){try{Thread.sleep(1000);}catch(InterruptedExceptione){}returnCompletableFuture.completedFuture("User_1001");}@Async("bizTaskExecutor")publicCompletableFuture<String>queryOrderAsync(){try{Thread.sleep(1200);}catch(InterruptedExceptione){}returnCompletableFuture.completedFuture("Order_List");}}

三、Controller 中编排异步结果

在 Controller 中注入 Service,利用CompletableFuture组合多个异步任务的结果。

importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;importjava.util.concurrent.CompletableFuture;@RestControllerpublicclassOrderController{@AutowiredprivateOrderServiceorderService;@GetMapping("/order/detail")publicStringgetOrderDetail()throwsException{// 并行发起两个异步请求CompletableFuture<String>userFuture=orderService.queryUserAsync();CompletableFuture<String>orderFuture=orderService.queryOrderAsync();// 等待所有任务完成并合并结果CompletableFuture.allOf(userFuture,orderFuture).join();return"用户: "+userFuture.get()+", 订单: "+orderFuture.get();}}

四、关键注意事项

  1. 返回值类型:@Async方法若需返回结果,必须返回CompletableFuture<T>Future<T>,否则调用方无法获取返回值。
  2. 异常处理:异步方法中的异常不会直接抛出到 Controller。建议配置全局AsyncUncaughtExceptionHandler或在CompletableFuture链中处理。
  3. 事务问题:@Async方法默认不在主事务中运行。若需事务,需在异步方法内部单独开启@Transactional

2.Spring Boot 异步全局异常处理

在 Spring Boot 中,@Async方法的异常无法被 Controller 的@ExceptionHandler捕获。需配置AsyncUncaughtExceptionHandler进行统一兜底,防止异常静默丢失。

一、自定义异常处理器

实现AsyncUncaughtExceptionHandler接口,记录日志或发送告警。

importorg.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;importlombok.extern.slf4j.Slf4j;importjava.lang.reflect.Method;@Slf4jpublicclassCustomAsyncExceptionHandlerimplementsAsyncUncaughtExceptionHandler{@OverridepublicvoidhandleUncaughtException(Throwableex,Methodmethod,Object...params){log.error("异步任务异常 - 方法: {}, 参数: {}",method.getName(),params,ex);// 此处可集成监控告警(如 Sentry/Prometheus)}}

二、注册到 Spring 容器

在配置类中重写getAsyncExecutorgetAsyncUncaughtExceptionHandler方法。

importorg.springframework.context.annotation.Configuration;importorg.springframework.scheduling.annotation.AsyncConfigurer;importorg.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;importjava.util.concurrent.Executor;@ConfigurationpublicclassAsyncConfigimplementsAsyncConfigurer{@OverridepublicExecutorgetAsyncExecutor(){ThreadPoolTaskExecutorexecutor=newThreadPoolTaskExecutor();executor.setCorePoolSize(5);executor.setMaxPoolSize(10);executor.setQueueCapacity(100);executor.setThreadNamePrefix("biz-async-");executor.initialize();returnexecutor;}@OverridepublicAsyncUncaughtExceptionHandlergetAsyncUncaughtExceptionHandler(){returnnewCustomAsyncExceptionHandler();}}

三、CompletableFuture 的异常处理

若使用CompletableFuture,需在链式调用末尾添加exceptionallyhandle,确保业务级异常被捕获。

future.thenApply(res->process(res)).exceptionally(ex->{log.error("业务处理失败",ex);returndefaultResult;// 返回默认值,防止链路中断});

四、核心区别总结

场景异常捕获方式适用情况
@Async 无返回值AsyncUncaughtExceptionHandler简单通知、日志记录等“发了就不管”的场景。
CompletableFuture.exceptionally()/.handle()需要获取结果、进行业务补偿或降级的场景。
Controller 层@RestControllerAdvice无效。无法捕获异步线程抛出的异常。
http://www.jsqmd.com/news/1097177/

相关文章:

  • 2026图片去水印方法:免费手机电脑工具、APP软件与在线网站教程
  • 深度长文 | 计算机体系结构:核心原理、发展演进与未来趋势(计算机架构系列-1)
  • css中实现三角形的一些方法
  • Lenovo Legion Toolkit:深度自定义联想笔记本性能控制的终极解决方案
  • Proxy - KD 新方法:突破黑盒大语言模型知识蒸馏限制,性能超传统白盒技术!
  • 智慧教育平台电子课本下载工具:让教学资源触手可及
  • 西门子设备硬件安装调试经验速记系列1(IM151-1Standard扩展子模块-标准灯码故障识别)
  • 小程序公司排行榜有没有参考价值?选服务商更该看这几项
  • Android Studio实战:5分钟搞定OneNET设备数据实时监控(附完整Token生成代码)
  • 杰理之播提示音时连接第二个麦,第二个麦会出现无声问题【篇】
  • 鸿蒙 ArkTS 两大基础事件简单说明
  • 别再用fail2ban了?试试Linux系统自带的账户锁防暴力破解神器faillock
  • 谷歌浏览器多开
  • 太强了!输入关键词,这几款AI论文工具就能帮你搞定毕业论文
  • Windows系统文件abcCertFirm.dll丢失找不到问题解决
  • AI Agent 的模型路由:多模型切换与智能选择
  • 软考网络工程师中级
  • 2026年,行业内口碑好的90kw电力测功机工厂究竟哪家更值得选?
  • 霞鹜文楷:当传统书法美学遇见现代开源代码
  • 别再让老漏洞拖后腿:手把手教你修复CVE-1999-0526和CVE-1999-0554(附NFS安全配置)
  • 1998-2025年上市公司AI技术应用水平
  • 如何在5分钟内搭建专业的无人机强化学习环境:gym-pybullet-drones完整指南
  • AutoGen框架深度拆解:群聊、可定制发言人与嵌套Agent的编程范式
  • mavonEditor代码块增强攻略:提升技术文档编辑效率的完整解决方案
  • 人机协作环路:人在回路中决策节点、审批流转与Agent升级机制
  • CTFshow PWN入门实战:手把手教你用pwntools搞定pwn24(含shellcraft模块详解)
  • 如何高效使用智能漫画翻译工具:面向初学者的自动化解决方案
  • 如何用Sunshine搭建终极免费游戏串流系统:5分钟实现跨平台游戏自由
  • Cellpose cyto3模型:基于深度学习范式的细胞分割方法论革新
  • 800块捡漏Tesla M40,手把手教你搞定Windows 10深度学习环境(含驱动、CUDA、PyTorch避坑指南)