Default SpringBoot asynchronous task configuration
By default, Spring searches for associated thread pool definitions: Context only org. Springframework.. The core task. TaskExecutor bean, or called “TaskExecutor” Java. Util. Concurrent. The Executor bean. If both are not resolved, will use the org. Springframework.. The core task. SimpleAsyncTaskExecutor to handle the asynchronous method invocation.
/** * Default asynchronous task configuration **@author Tan
* @version 1.0
* @date2021/6/2 * /
@EnableAsync(proxyTargetClass = true)
@Configuration
public class DefaultAsyncTaskConfiguration {
@Bean
public TaskExecutor taskExecutor(a) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// The core pool size is the minimum number of worker threads that remain active
executor.setCorePoolSize(4);
// Maximum number of threads
executor.setMaxPoolSize(64);
// Maximum idle time per second (based on usage)
executor.setKeepAliveSeconds(60);
// Task queue size (depending on usage)
executor.setQueueCapacity(128);
// Thread name prefix
executor.setThreadNamePrefix("BaseExecutor-");
/ * rejectedExecutionHandler: when the number of thread pool achieves maxPoolSize, how to handle rejection thread ThreadPoolExecutor task. CallerRunsPolicy: Instead of executing tasks in a new thread, the caller's thread executes */
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
returnexecutor; }}Copy the code
@async is configured using the taskExecutor thread pool