package com.njcn.influx.config;

import org.springframework.beans.factory.annotation.Value;
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:
 * Date: 2026/04/20  下午 1:21【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Configuration
@EnableAsync
public class ThreadPoolConfig {

    @Value("${task.pool.core-size:10}")
    private int coreSize;
    @Value("${task.pool.max-size:30}")
    private int maxSize;
    @Value("${task.pool.queue-capacity:1000}")
    private int queueCapacity;

    @Bean("eventTaskExecutor")
    public Executor eventTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(coreSize);
        executor.setMaxPoolSize(maxSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("event-task-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}
