package com.elab.spring.utils;

import com.elab.core.async.RealTaskExecutor;
import com.elab.core.async.SchedulingTaskExecutor;
import com.elab.core.async.model.TaskStoreData;
import com.elab.core.async.pruducer.ITaskProducer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @Module 工具类
 * @Description 线程执行工具类
 * @Author liukaixiong
 * @Date 2020/11/18 15:51
 */
public class ThreadProcessUtils implements ApplicationContextAware, InitializingBean {
    private ApplicationContext applicationContext;

    private static ITaskProducer taskProducer;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static void addRealTimeQueue(RealTaskExecutor taskExecutor, TaskStoreData taskStoreData) {
        if (taskProducer != null) {
            taskProducer.sendRealTimeQueue(taskExecutor, taskStoreData);
        }
    }

    public static void addSchedulingList(SchedulingTaskExecutor schedulingTaskExecutor) {
        if (taskProducer != null) {
            taskProducer.sendSchedulingList(schedulingTaskExecutor);
        }
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        taskProducer = this.applicationContext.getBean(ITaskProducer.class);
    }
}
