package com.mosty.common.base.threadpool.log;

import com.mosty.common.base.threadpool.SimpleThreadPool;
import com.mosty.common.base.threadpool.ThreadPoolEntity;
import com.mosty.common.base.threadpool.ThreadPoolInit;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

import static com.mosty.common.base.entity.log.LogConst.LOG_THREAD_POOL;

/**
 * 操作日志线程池 单体服务、基础微服务、普通微服务都需要
 * @author kevin
 * @date 2022/1/25 8:40 PM
 * @since 1.0.0
 */
@Component
@AutoConfigureAfter(SimpleThreadPool.class)
public class OperationLogThreadPool implements InitializingBean {

    /**
     * 操作日志核心和最大线程数，可以根据并发量调整生效，默认单线程
     */
    @Value("${operation-log.thread-pool.thread-mum:1}")
    private int threadNum;

    /**
     * 操作日志最大队列数，可以根据并发量调整生效， 默认1000
     */
    @Value("${operation-log.thread-pool.log-queue-size:1000}")
    private int logQueueSize;

    private OperationLogThreadPool() {

    }

    @Override
    public void afterPropertiesSet() {
        SimpleThreadPool.putThreadPool(new ThreadPoolEntity(LOG_THREAD_POOL, ThreadPoolInit.PoolModel.IO,
                false, true, "操作日志线程池",
                threadNum, threadNum, 0, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(logQueueSize), new ThreadPoolExecutor.DiscardOldestPolicy()));
    }

}
