package com.bycrm.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; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; /** * 异步任务配置:为消息推送等异步操作提供线程池。 */ @Configuration @EnableAsync public class AsyncConfig { /** * pushplus 推送专用线程池。 * 满载时降级为调用线程同步执行(CallerRunsPolicy),宁可变慢也不丢弃推送任务。 */ @Bean("pushplusExecutor") public Executor pushplusExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(2); executor.setMaxPoolSize(4); executor.setQueueCapacity(100); executor.setThreadNamePrefix("pushplus-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.setWaitForTasksToCompleteOnShutdown(true); executor.initialize(); return executor; } }