package com.zhgd.xmgl.config;

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

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @program: wisdomSite
 * @description: 线程池配置
 * @author: Mr.Peng
 * @create: 2021-02-23 10:04
 **/
@Configuration
public class AsyncConfig {
    /**
     * Set the ThreadPoolExecutor's core pool size.
     */
    private int corePoolSize = 8;
    /**
     * Set the ThreadPoolExecutor's maximum pool size.
     */
    private int maxPoolSize = 16;
    /**
     * Set the capacity for the ThreadPoolExecutor's BlockingQueue.
     */
    private int queueCapacity = 200;

    /**
     * 默认情况下，在创建了线程池后，线程池中的线程数为0，当有任务来之后，就会创建一个线程去执行任务，
     * 当线程池中的线程数目达到corePoolSize后，就会把到达的任务放到缓存队列当中；
     * 当队列满了，就继续创建线程，当线程数量大于等于maxPoolSize后，开始使用拒绝策略拒绝
     */

    @Bean("taskExecutor")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("taskExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    /**
     * 默认情况下，在创建了线程池后，线程池中的线程数为0，当有任务来之后，就会创建一个线程去执行任务，
     * 当线程池中的线程数目达到corePoolSize后，就会把到达的任务放到缓存队列当中；
     * 当队列满了，就继续创建线程，当线程数量大于等于maxPoolSize后，开始使用拒绝策略拒绝
     */

    @Bean("doubleCarbonExecutor")
    public ThreadPoolTaskExecutor doubleCarbonExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("doubleCarbonExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    @Bean("countAttendanceExecutor")
    public ThreadPoolTaskExecutor attendanceExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("countAttendanceExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    @Bean("sendAttendanceExecutor")
    public ThreadPoolTaskExecutor sendAttendanceExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("sendAttendanceExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    @Bean("sendWorkerExecutor")
    public ThreadPoolTaskExecutor sendWorkerExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("sendWorkerExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    @Bean("sendExitWorkerExecutor")
    public ThreadPoolTaskExecutor sendExitWorkerExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("sendExitWorkerExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    @Bean("environmentNoiseDataExecutor")
    public ThreadPoolTaskExecutor sendEnvironmentNoiseDataExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("environmentNoiseDataExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    @Bean("electricalExecutor")
    public ThreadPoolTaskExecutor sendElectricalExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("electricalExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    @Bean("towerExecutor")
    public ThreadPoolTaskExecutor sendTowerExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("towerExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    @Bean("lifterExecutor")
    public ThreadPoolTaskExecutor sendLifterExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("lifterExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    @Bean("devExcavationExecutor")
    public ThreadPoolTaskExecutor sendDevExcavationEExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("devExcavationExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    @Bean("gantryCraneExecutor")
    public ThreadPoolTaskExecutor sendGantryCraneExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("gantryCraneExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    @Bean("fileExecutor")
    public ThreadPoolTaskExecutor fileExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("fileExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }

    @Bean("asyncExecutor")
    public ThreadPoolTaskExecutor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        /** 核心线程数（默认线程数） */
        executor.setCorePoolSize(corePoolSize);
        /** 最大线程数 */
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        /** 允许线程空闲时间（单位：默认为秒） */
        executor.setKeepAliveSeconds(60);
        /** 线程池名前缀 */
        executor.setThreadNamePrefix("asyncExecutor-");
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        return executor;
    }
}
