package com.njcn.gather.system.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;

@Slf4j
@Configuration
public class LogExecutorConfig {

    @Bean(name = "logAuditExecutor", destroyMethod = "shutdown")
    public ExecutorService logAuditExecutor() {
        AtomicInteger threadIndex = new AtomicInteger(1);
        return new ThreadPoolExecutor(
                4, 8, 30, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(100),
                runnable -> {
                    Thread thread = new Thread(runnable);
                    thread.setName("log-audit-" + threadIndex.getAndIncrement());
                    return thread;
                },
                (runnable, executor) -> log.warn("审计日志线程池已满，丢弃本次日志任务")
        );
    }
}
