package com.cusc.nirvana.user.rnr.fp.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @program: workspace-rnr-new
 * @description:
 * @author: lig131
 * @create: 2022-05-20 18:06
 **/
@Configuration
@Slf4j
public class ThreadPoolExecutorConfig {
    @Value("${rnr.image.watermark.threadpool.createWaterMark.size:5}")
    private int textWaterMarkThreadPoolSize4createWaterMark;
    @Value("${rnr.image.watermark.threadpool.createWaterMark.queueSize:1000}")
    private int textWaterMarkThreadPoolQueueSize4createWaterMark;
    @Value("${rnr.image.watermark.threadpool.removeTemp.size:5}")
    private int textWaterMarkThreadPoolSize4RemoveTemp;
    @Value("${rnr.image.watermark.threadpool.removeTemp.queue.size:1000}")
    private int textWaterMarkThreadPoolQueueSize4RemoveTemp;

    /**
     * 文字水印线程池
     */
    @Bean(name = "textWaterMarkThreadPool4createWaterMark")
    public ExecutorService getTextWaterMarkThreadPoolTaskServiceExecutor() {
        log.info("textWaterMarkThreadPool4createWaterMark线程池开始初始化，线程池中线程数：{}", textWaterMarkThreadPoolSize4createWaterMark);
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("textWaterMarkThreadPool4createWaterMark-%d").build();
        return new ThreadPoolExecutor(textWaterMarkThreadPoolSize4createWaterMark, textWaterMarkThreadPoolSize4createWaterMark, 0L,
                TimeUnit.SECONDS, new LinkedBlockingDeque<>(textWaterMarkThreadPoolQueueSize4createWaterMark),
                threadFactory, new ThreadPoolExecutor.AbortPolicy());
    }


    /**
     * 文字水印线程池-删除临时文件
     */
    @Bean(name = "textWaterMarkThreadPool4RemoveTemp")
    public ExecutorService getTextWaterMarkRemoveTempThreadPoolTaskServiceExecutor() {
        log.info("textWaterMarkThreadPool4RemoveTemp线程池开始初始化，线程池中线程数：{}", textWaterMarkThreadPoolSize4RemoveTemp);
        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("textWaterMarkThreadPool4RemoveTemp-%d").build();
        return new ThreadPoolExecutor(textWaterMarkThreadPoolSize4RemoveTemp, textWaterMarkThreadPoolSize4RemoveTemp, 0L,
                TimeUnit.SECONDS, new LinkedBlockingDeque<>(textWaterMarkThreadPoolQueueSize4RemoveTemp),
                threadFactory, new ThreadPoolExecutor.AbortPolicy());
    }

}
