package cn.iocoder.yudao.module.product.emqx;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
@DependsOn({"deviceMqttService"})
public class EmqxStart implements ApplicationListener<ApplicationReadyEvent>, Ordered {

    @Resource
    DeviceMqttService deviceMqttService;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        log.info("**********************开始执行启动mqtt*************************");
        deviceMqttService.checkAndAutoConnectMqttServer();
    }
    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }

    public static final ExecutorService executorService = new ThreadPoolExecutor(
            Runtime.getRuntime().availableProcessors(),
            Runtime.getRuntime().availableProcessors() * 2, 60, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(50000));
}
