package com.zcloud.config;

import com.zcloud.common.threadPool.ThreadPoolProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author zhangyue
 * @date 2023/1/9/009 10:06
 * 视频上传阿里云--异步方法自定义线程池配置
 */
@Configuration
public class ThreadPoolConfig {



    @ConfigurationProperties(prefix = "spring.task.execution.pool")
    @Bean
    public ThreadPoolProperties localPoolProperties() {
        return new ThreadPoolProperties();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.task.execution.pool2")
    public ThreadPoolProperties uploadPoolProperties() {
        return new ThreadPoolProperties();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.task.execution.pool3")
    public ThreadPoolProperties syncCurriculumPoolProperties() {
        return new ThreadPoolProperties();
    }
    @Bean
    @ConfigurationProperties(prefix = "spring.task.execution.pool4")
    public ThreadPoolProperties syncChapterReplacePoolProperties() {
        return new ThreadPoolProperties();
    }
    @Bean
    public Executor localAsync() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //最大线程数
        executor.setMaxPoolSize(localPoolProperties().getMaxSize());
        //核心线程数
        executor.setCorePoolSize(localPoolProperties().getCoreSize());
        //任务队列的大小
        executor.setQueueCapacity(localPoolProperties().getQueueCapacity());
        //线程前缀名
        executor.setThreadNamePrefix("test");
        //线程存活时间
        executor.setKeepAliveSeconds(localPoolProperties().getKeepAlive());
        /**
         * 拒绝处理策略
         * CallerRunsPolicy()：交由调用方线程运行，比如 main 线程。
         * AbortPolicy()：直接抛出异常。
         * DiscardPolicy()：直接丢弃。
         * DiscardOldestPolicy()：丢弃队列中最老的任务。
         */
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        //线程初始化
        executor.initialize();
        return executor;
    }
    @Bean
    public Executor uploadAsync() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //最大线程数
        executor.setMaxPoolSize(uploadPoolProperties().getMaxSize());
        //核心线程数
        executor.setCorePoolSize(uploadPoolProperties().getCoreSize());
        //任务队列的大小
        executor.setQueueCapacity(uploadPoolProperties().getQueueCapacity());
        //线程前缀名
        executor.setThreadNamePrefix(uploadPoolProperties().getThreadNamePrefix());
        //线程存活时间
        executor.setKeepAliveSeconds(uploadPoolProperties().getKeepAlive());
        /**
         * 拒绝处理策略
         * CallerRunsPolicy()：交由调用方线程运行，比如 main 线程。
         * AbortPolicy()：直接抛出异常。
         * DiscardPolicy()：直接丢弃。
         * DiscardOldestPolicy()：丢弃队列中最老的任务。
         */
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        //线程初始化
        executor.initialize();
        return executor;
    }



    @Bean
    public Executor syncCurriculumAsync() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //最大线程数
        executor.setMaxPoolSize(syncCurriculumPoolProperties().getMaxSize());
        //核心线程数
        executor.setCorePoolSize(syncCurriculumPoolProperties().getCoreSize());
        //任务队列的大小
        executor.setQueueCapacity(syncCurriculumPoolProperties().getQueueCapacity());
        //线程前缀名
        executor.setThreadNamePrefix(syncCurriculumPoolProperties().getThreadNamePrefix());
        //线程存活时间
        executor.setKeepAliveSeconds(syncCurriculumPoolProperties().getKeepAlive());
        /**
         * 拒绝处理策略
         * CallerRunsPolicy()：交由调用方线程运行，比如 main 线程。
         * AbortPolicy()：直接抛出异常。
         * DiscardPolicy()：直接丢弃。
         * DiscardOldestPolicy()：丢弃队列中最老的任务。
         */
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        //线程初始化
        executor.initialize();
        return executor;
    }

    @Bean
    public Executor syncCurriculumReplace() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //最大线程数
        executor.setMaxPoolSize(syncChapterReplacePoolProperties().getMaxSize());
        //核心线程数
        executor.setCorePoolSize(syncChapterReplacePoolProperties().getCoreSize());
        //任务队列的大小
        executor.setQueueCapacity(syncChapterReplacePoolProperties().getQueueCapacity());
        //线程前缀名
        executor.setThreadNamePrefix(syncChapterReplacePoolProperties().getThreadNamePrefix());
        //线程存活时间
        executor.setKeepAliveSeconds(syncChapterReplacePoolProperties().getKeepAlive());
        /**
         * 拒绝处理策略
         * CallerRunsPolicy()：交由调用方线程运行，比如 main 线程。
         * AbortPolicy()：直接抛出异常。
         * DiscardPolicy()：直接丢弃。
         * DiscardOldestPolicy()：丢弃队列中最老的任务。
         */
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        //线程初始化
        executor.initialize();
        return executor;
    }
}
