package com.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; import org.springframework.context.annotation.Primary; @Configuration @EnableAsync public class ThreadPoolConfig { @Value("${app.processor.thread-pool.core-pool-size:10}") private int corePoolSize; @Value("${app.processor.thread-pool.max-pool-size:20}") private int maxPoolSize; @Value("${app.processor.thread-pool.queue-capacity:2000}") private int queueCapacity; @Value("${app.processor.thread-pool.keep-alive-seconds:60}") private int keepAliveSeconds; @Bean("logNormalProcessorExecutor") @Primary // 添加这个注解 public Executor logNormalProcessorExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(corePoolSize); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); executor.setKeepAliveSeconds(keepAliveSeconds); executor.setThreadNamePrefix("log-processor-"); // 拒绝策略:由调用线程处理该任务(保证不丢失消息) executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 等待所有任务结束后再关闭线程池 executor.setWaitForTasksToCompleteOnShutdown(true); executor.setAwaitTerminationSeconds(60); executor.initialize(); return executor; } }