package com.elab.spring.config;

import com.elab.core.async.consumer.TaskConsumer;
import com.elab.core.async.pruducer.ITaskProducer;
import com.elab.core.async.store.TaskExecutorQueue;
import com.elab.core.utils.DataUtils;
import com.elab.log.ext.CatTaskProducer;
import com.elab.spring.config.prop.ThreadProperties;
import com.elab.spring.utils.ThreadProcessUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 线程配置
 *
 * @author ： liukx
 * @time ： 2020/8/10 - 19:50
 */
@Configuration
@EnableConfigurationProperties(ThreadProperties.class)
public class ThreadConfiguration {

    @Autowired
    private ThreadProperties threadProperties;

    @Bean
    public TaskExecutorQueue taskExecutorQueue() {
        Integer realQueueSize = DataUtils.getIntegerValue(threadProperties.getRealQueueSize(), 1000);
        TaskExecutorQueue queue = new TaskExecutorQueue(realQueueSize);
        return queue;
    }

    @Bean
    public TaskConsumer taskConsumer(@Autowired TaskExecutorQueue taskExecutorQueue) {
        TaskConsumer taskConsumer = new TaskConsumer(taskExecutorQueue);
        // 开启定时调度
        taskConsumer.startSchedulingConsumer(DataUtils.getIntegerValue(threadProperties.getSchedulingIntervalSecond(), 5));
        // 开启异步队列
        taskConsumer.startRealTimeConsumer();
        return taskConsumer;
    }

    @Bean
    public ITaskProducer taskProducer(@Autowired TaskExecutorQueue taskExecutorQueue) {
        ITaskProducer producer = new CatTaskProducer(taskExecutorQueue);
        return producer;
    }

    @Bean
    public ThreadProcessUtils threadProcessUtils() {
        return new ThreadProcessUtils();
    }

}
