package com.ruoyi.common.core.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池配置
 */
@EnableConfigurationProperties(ThreadPoolProperties.class)
@ConditionalOnProperty(prefix = "thread-pool", value = "enable", havingValue = "true")
@Configuration
public class ThreadPoolConfig {

    @Bean(name = "customThreadPoolExecutor")
    public ThreadPoolExecutor customThreadPoolExecutor(ThreadPoolProperties config) {
        return new ThreadPoolExecutor(config.getCoreSize(),
                config.getMaxSize(),
                config.getKeepAlive(),
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(config.getQueueSize()),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }

}
