package com.reyun.saas.mob.user.thread.workorder;


import io.netty.util.concurrent.DefaultThreadFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class WorkOrderTheradConfig {

    /**
     * 核心线程数
     */
    @Value("${WorkOrderThreadPool.corePollSize}")
    private Integer corePollSize;

    /**
     * 最大线程数
     */
    @Value("${WorkOrderThreadPool.maxPollSize}")
    private Integer maxPollSize;

    /**
     * 空闲线程等待时间 单位 : 秒
     */
    @Value("${WorkOrderThreadPool.leisureAwaitTime}")
    private Long leisureAwaitTime;

    /**
     * 空闲线程等待时间 时间单位 默认为秒
     */
    private TimeUnit timeUnit = TimeUnit.SECONDS;

    /**
     * 工作队列长度
     */
    @Value("${WorkOrderThreadPool.queueSize}")
    private Integer queueSize;

    private final String THREAD_GROUP_NAME =  "WorkOrderThread";



    @Bean(name = "workOrderThreadPool")
    public ThreadPoolExecutor workOrderThreadPool(){

        //工作队列
        LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>(queueSize);

        //线程工厂
        ThreadFactory factory = new DefaultThreadFactory(THREAD_GROUP_NAME);

        //拒绝策略
        ThreadPoolExecutor.AbortPolicy policy = new ThreadPoolExecutor.AbortPolicy();

        //线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePollSize,maxPollSize,
                leisureAwaitTime,timeUnit,queue,factory,policy);

        return threadPoolExecutor;
    }





}
