package com.ruoyi.common.thread;

import lombok.extern.slf4j.Slf4j;
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;

/**
 * 自定义线程池
 */
@Slf4j
@Configuration
@EnableAsync
public class ExecutorConfig {

    /**
     * 业务使用
     *
     * @return
     */
    @Bean(name = ThreadPoolNameConstant.ThreadServicePoolName)
    public Executor serviceThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(50);//表示线程池核心线程，正常情况下开启的线程数量。
        executor.setQueueCapacity(500); //配置队列大小
        executor.setMaxPoolSize(200);//当核心线程都在跑任务，还有多余的任务会存到此处。
        executor.setKeepAliveSeconds(300);//非核心线程的超时时长，超长后会被回收。
        executor.setThreadNamePrefix(ThreadPoolNameConstant.ThreadServicePoolName + "-");//配置线程池前缀
        //用来设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setRejectedExecutionHandler((Runnable r, ThreadPoolExecutor exe) -> {
            log.info("{}->:当前任务线程池队列已满!", ThreadPoolNameConstant.ThreadServicePoolName);
        });//配置拒绝策略
        executor.initialize();//初始化线程池。
        return executor;
    }
}