| | |
| | | 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 |
| | |
| | | **/ |
| | | @Component |
| | | @Slf4j(topic = "mqtt") |
| | | @Order(1) |
| | | public class MqttConsumer implements ApplicationRunner { |
| | | |
| | | private static MqttClient client; |
| | |
| | | @Override |
| | | public void run(ApplicationArguments args) { |
| | | log.info("初始化并启动mqtt......"); |
| | | if(PropertiesUtil.MQTT_ENABLED) |
| | | { |
| | | if (PropertiesUtil.MQTT_ENABLED) { |
| | | this.connect(); |
| | | } |
| | | } |
| | |
| | | |
| | | /** |
| | | * 发布,非持久化 |
| | | * |
| | | * <p> |
| | | * qos根据文档设置为1 |
| | | * |
| | | * @param topic |
| | |
| | | * 发布 |
| | | */ |
| | | 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); |
| | |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |