前面讲解了Java普通项目如何创建线程池,使用的是java.util.concurrent.ThreadPoolExecutor
jdk自带线程池对象。
Spring Mvc/ Spring boot web项目中,请使用spring 实现的线程池,保证在其更好的在spring web 处理异步线程。
WebMvcConfigurer.configureAsyncSupport
是 Spring Framework 中用于配置异步请求处理支持的一个方法。
当你在 Spring MVC 应用中想要支持异步处理 HTTP 请求时(例如使用 Callable
、DeferredResult
、WebAsyncTask
或响应式编程模型如 Mono
/Flux
),Spring 会使用底层的异步支持机制(如 Servlet 3.0+ 的异步支持)来处理这些请求。
configureAsyncSupport
方法允许你自定义这些异步处理的底层行为。你可以在实现 WebMvcConfigurer
接口的配置类中重写这个方法,来设置:
默认超时时间 (Default Timeout):
configurer.setDefaultTimeout(long timeout)
设置异步操作的默认超时时间(毫秒)。如果异步任务在这个时间内没有完成,将会触发超时处理。configurer.setTaskExecutor(AsyncTaskExecutor executor)
指定一个自定义的线程池来执行异步任务。默认情况下,Spring 会使用一个简单的 SimpleAsyncTaskExecutor
,但在生产环境中,通常建议配置一个更高效的线程池(如 ThreadPoolTaskExecutor
)来避免创建过多线程。DeferredResult
的拦截器:
configurer.registerDeferredResultInterceptors(HandlerInterceptor... interceptors)
注册拦截器,这些拦截器可以在 DeferredResult
的生命周期(如超时、完成、错误)中执行一些自定义逻辑,比如清理资源、记录日志等。WebAsyncManager
的拦截器 (较少使用):
configurer.registerCallableInterceptors(AsyncHandlerInterceptor... interceptors)
为 Callable
类型的异步请求注册拦截器。
如果项目中出现了下面错误,请务必配置请求线支持程池
2025-09-13T13:09:28.013+08:00 WARN 27620 --- [boot3-ai] [ctor-http-nio-2] s.w.s.m.m.a.RequestMappingHandlerAdapter : !!!
Performing asynchronous handling through the default Spring MVC SimpleAsyncTaskExecutor.
This executor is not suitable for production use under load.
Please, configure an AsyncTaskExecutor through the WebMvc config.
-------------------------------
!!!
@Bean
public ThreadPoolTaskExecutor taskPool(){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(8);
executor.setMaxPoolSize(16);
executor.setQueueCapacity(1000);
executor.setKeepAliveSeconds(0);
executor.setThreadNamePrefix("task-pool-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
return executor;
}
创建一个配置类,通过实现接口WebMvcConfigurer
并重写方法configureAsyncSupport
配置自定义的spring线程池
import jakarta.annotation.PreDestroy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
configurer.setTaskExecutor(taskPool());
configurer.setDefaultTimeout(60000);//60s
}
@Bean
public ThreadPoolTaskExecutor taskPool(){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(8);
executor.setMaxPoolSize(16);
executor.setQueueCapacity(1000);
executor.setKeepAliveSeconds(0);
executor.setThreadNamePrefix("task-pool-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
return executor;
}
@PreDestroy
public void preDestroy(){
taskPool().shutdown();
}
}
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
// 设置默认超时时间为30秒
configurer.setDefaultTimeout(30_000);
// 配置一个自定义的线程池
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(50);
executor.setThreadNamePrefix("async-");
executor.initialize();
configurer.setTaskExecutor(executor);
// 注册一个 DeferredResult 拦截器
configurer.registerDeferredResultInterceptors(new MyDeferredResultInterceptor());
}
}
总结:WebMvcConfigurer.configureAsyncSupport
是一个强大的钩子方法,让你能够精细地控制 Spring MVC 中异步请求的执行环境、超时策略和生命周期管理,对于构建高性能、高可用的 Web 应用至关重要。
https://blog.xqlee.com/article/2509151234318543.html