‘liusuyi’
2023-07-07 85446b5b526ac53af9add7c83cfd72f39ec39611
src/main/java/com/ard/utils/mqtt/MqttConsumer.java
@@ -1,12 +1,17 @@
package com.ard.utils.mqtt;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.expression.spel.ast.NullLiteral;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
/**
 * @Description: mqtt消费客户端
 * @ClassName: MqttConsumer
@@ -16,6 +21,7 @@
 **/
@Component
@Slf4j(topic = "mqtt")
@Order(1)
public class MqttConsumer implements ApplicationRunner {
    private static MqttClient client;
@@ -23,8 +29,7 @@
    @Override
    public void run(ApplicationArguments args) {
        log.info("初始化并启动mqtt......");
        if(PropertiesUtil.MQTT_ENABLED)
        {
        if (PropertiesUtil.MQTT_ENABLED) {
            this.connect();
        }
    }
@@ -130,7 +135,7 @@
    /**
     * 发布,非持久化
     *
     * <p>
     *  qos根据文档设置为1
     *
     * @param topic
@@ -144,6 +149,7 @@
     * 发布
     */
    public static void publish(int qos, boolean retained, String topic, String pushMessage) {
        if (client != null) {
        log.info("【主题】:" + topic + "【qos】:" + qos + "【pushMessage】:" + pushMessage);
        MqttMessage message = new MqttMessage();
        message.setQos(qos);
@@ -171,4 +177,5 @@
        }
    }
}
}