package me.zhengjie.config.thread;

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;
/**
 * 创建人：lyc
 * 时间：2021-10-08
 * 线程池配置
 */
@Configuration
public class ThreadPoolConfig {

    @Value("${thread.corePoolSize}")
    private int corePoolSize;

    @Value("${thread.maxPoolSize}")
    private int maxPoolSize;

    @Value("${thread.queueCapacity}")
    private int queueCapacity;

    @Value("${thread.keepAliveSeconds}")
    private int keepAliveSeconds;

    @Bean(name = "threadPoolTaskExecutor1")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor1() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(maxPoolSize);
        executor.setCorePoolSize(corePoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setKeepAliveSeconds(keepAliveSeconds);
        // 线程池对拒绝任务(无线程可用)的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }
    @Bean(name = "threadPoolTaskExecutor2")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor2() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(maxPoolSize);
        executor.setCorePoolSize(corePoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setKeepAliveSeconds(keepAliveSeconds);
        // 线程池对拒绝任务(无线程可用)的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

//    @Bean("brianThreadPool")
//    public ThreadPoolTaskExecutor brianThreadPool(){
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        //核心线程数
//        executor.setCorePoolSize(corePoolSize);
//        //最大线程数
//        executor.setMaxPoolSize(maxPoolSize);
//        //队列中最大的数
//        executor.setQueueCapacity(queueCapacity);
//        //县城名称前缀
//        executor.setThreadNamePrefix("brianThreadPool_");
//        //rejectionPolicy：当pool已经达到max的时候，如何处理新任务
//        //callerRuns：不在新线程中执行任务，而是由调用者所在的线程来执行
//        //对拒绝task的处理策略
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        //线程空闲后最大的存活时间
//        executor.setKeepAliveSeconds(keepAliveSeconds);
//        //初始化加载
//        executor.initialize();
//        return executor;
//    }

//    @Bean("brianThreadPool")
//    public SimpleRabbitListenerContainerFactory containerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
////        factory.setConcurrentConsumers(10);  //设置线程数
////        factory.setMaxConcurrentConsumers(10); //最大线程数
//        //核心线程数
//        factory.setConcurrentConsumers(corePoolSize);
//        //最大线程数
//        factory.setMaxConcurrentConsumers(maxPoolSize);
//        configurer.configure(factory, connectionFactory);
//        return factory;
//    }
}
