package com.xinelu.framework.config;

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;

/**
 * @Description 异步任务线程池配置类
 * @Author 纪寒
 * @Date 2023-03-17 09:55:36
 * @Version 1.0
 */
@Slf4j
@Configuration
@EnableAsync
public class AsyncExecutorConfig {
    /**
     * 创建一个核心线程为10个，最大线程为20，等待队列为10，拒绝策略为线程最大需求满就返回给调度线程执行
     *
     * @return 创建完的线程池
     */
    @Bean
    public Executor asyncThreadServiceExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(10);
        executor.setThreadNamePrefix("async-thread-service-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        executor.initialize();
        return executor;
    }
}
