package top.baogutang.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @description: 线程池配置
 * @author: nikooh
 * @date: 2023/06/15 : 12:18
 */
@Slf4j
@Configuration
public class ExecutorConfig {

    @Value("${spring.application.name}")
    private String appName;

    @Value("${thread.pool.core.pool.size:5}")
    private Integer corePoolSize;

    @Value("${thread.pool.max.pool.size:10}")
    private Integer maxPoolSize;

    @Value("${thread.pool.keep.alive.second:10}")
    private Integer keepAliveSecond;

    @Value("${thread.pool.queue.capacity:100}")
    private Integer queueCapacity;

    @Bean("commonExecutor")
    public ThreadPoolTaskExecutor commonExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix(appName + "-common-");
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setKeepAliveSeconds(keepAliveSecond);
        executor.setQueueCapacity(queueCapacity);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.initialize();
        return executor;
    }

    @Bean("taskExecutor")
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(maxPoolSize);
        taskScheduler.setThreadNamePrefix(appName + "-schedule-");
        taskScheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        taskScheduler.setPoolSize(10);
        return taskScheduler;
    }

}
