package com.dayou.configuration;

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

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置
 *
 * @author wucl
 **/
@Configuration
public class ThreadPoolConfig {

    @Bean(name = "operateLogThreadPool")
    public ThreadPoolTaskExecutor operateLogThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(30);
        executor.setCorePoolSize(20);
        executor.setQueueCapacity(400);
        executor.setKeepAliveSeconds(300);
        // 线程池对拒绝任务(无线程可用)的处理策略
        executor.setThreadGroupName("custome-operateLogThreadPool-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

    @Bean(name = "mainThreadPool")
    @Primary
    public ThreadPoolTaskExecutor mainThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(50);
        executor.setCorePoolSize(15);
        executor.setQueueCapacity(800);
        executor.setKeepAliveSeconds(120);
        // 线程池对拒绝任务(无线程可用)的处理策略
        executor.setThreadGroupName("custome-mainThreadPool-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

}
