package cn.iododer.yudao.module.mqtt.config;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 初始化执行
 */
@Slf4j
@Order(1)
@Component
public class MqttBeforePoint implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 初始化Mqtt连接
        try {
            MqttFactory.getInstance();
            log.info("[MQTT]初始化成功");
        } catch (MqttException e) {
            log.info("[MQTT]初始化失败。考虑是否服务端掉线");
            MqttFactory.reconnect();
        }
    }

}