package com.njcn.gather.tool.adddata.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * add-data 模块任务线程池配置。
 */
@Slf4j
@Configuration
public class AddDataExecutorConfig {

    @Bean(name = "addDataTaskExecutorService", destroyMethod = "shutdown")
    public ExecutorService addDataTaskExecutorService() {
        AtomicInteger threadIndex = new AtomicInteger(1);
        return new ThreadPoolExecutor(
                1,
                1,
                30,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(8),
                runnable -> {
                    Thread thread = new Thread(runnable);
                    thread.setName("add-data-task-" + threadIndex.getAndIncrement());
                    return thread;
                },
                (runnable, executor) -> log.warn("数据补录任务线程池已满，拒绝新的补数任务")
        );
    }
}
