package vip.xiaonuo.coldchain.core.config;

/**
 * @author jackzhou
 * @version 1.0
 * @project jfcloud-coldchain
 * @description
 * @date 2024/11/12 15:13:41
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
public class ColdChainAsyncConfig {
    /**
     * 创建自定义的线程池配置
     * 线程池会根据实际需求调整线程数量，但不会超过最大线程数
     */
    @Bean
    public ThreadPoolTaskExecutor coldChainAsyncTask() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(200);  // 设置核心线程数，假设并发的最小线程数为200
        executor.setMaxPoolSize(1000);  // 设置最大线程数，允许最多1000个线程并行处理
        executor.setQueueCapacity(10000);  // 设置队列容量，临时保存10000个请求
        executor.setKeepAliveSeconds(60);  // 设置线程空闲60秒后销毁
        executor.setThreadNamePrefix("coldchain-async-");  // 设置线程名称的前缀
        executor.initialize();
        return executor;
    }

}
