package com.hdkj.lt.bf.config;

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

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 状估监测模块异步线程池配置
 * <p>
 * 用于 @Async 标注的异步方法（当前: 配变越限→查用户电压的异步监听器）。
 * 线程名前缀 se-async- 便于日志排查。
 *
 * @author lsl
 * @since 2026-07-23
 */
@Configuration
public class SeAsyncConfig implements AsyncConfigurer {

    @Bean(name = "seAsyncExecutor")
    public ThreadPoolTaskExecutor seAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(4);
        executor.setMaxPoolSize(8);
        executor.setQueueCapacity(200);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("se-async-");
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        // CallerRunsPolicy: 队列满时由调用线程执行，避免丢任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

    @Override
    public Executor getAsyncExecutor() {
        return seAsyncExecutor();
    }
}