package com.hdkj.lt.si.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 状估模块 Bean 配置: RestTemplate (带超时) + 有界线程池.
 */
@Configuration
public class SeConfig {

    /**
     * 状估查询专用线程池 (有界). 跨所有请求共享, 大小 = poolSize,
     * 即并发调用远程接口的全局上限. 守护线程, 应用关闭时 shutdown.
     */
    @Bean(name = "seQueryExecutor", destroyMethod = "shutdown")
    public ExecutorService seQueryExecutor() {
        ThreadFactory factory = new ThreadFactory() {
            private final AtomicInteger counter = new AtomicInteger(0);

            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r, "se-query-" + counter.incrementAndGet());
//                t.setDaemon(true);
                return t;
            }
        };
        return new ThreadPoolExecutor(
                8,8,0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(200),factory,new ThreadPoolExecutor.AbortPolicy()
        );
    }
}
