From 75e5a8de288722913dea5ac7234c29b97e83f1c4 Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期二, 22 四月 2025 17:44:04 +0800
Subject: [PATCH] chore: 更新 .gitignore 文件
---
pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/workOrder/mqtt/MqttMsgSubscriber.java | 71 +++++++++++++++++++++++++++++++++++
1 files changed, 71 insertions(+), 0 deletions(-)
diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/workOrder/mqtt/MqttMsgSubscriber.java b/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/workOrder/mqtt/MqttMsgSubscriber.java
new file mode 100644
index 0000000..dabba38
--- /dev/null
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/workOrder/mqtt/MqttMsgSubscriber.java
@@ -0,0 +1,71 @@
+package com.dy.pipIrrApp.workOrder.mqtt;
+
+import lombok.extern.slf4j.Slf4j;
+import org.eclipse.paho.client.mqttv3.*;
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
+
+/**
+ * @author ZhuBaoMin
+ * @date 2024-11-16 11:29
+ * @LastEditTime 2024-11-16 11:29
+ * @Description 鍒濆鍖栦竴涓狹qtt瀹㈡埛绔紝骞舵牴鎹厤缃闃卼opic
+ */
+
+@Slf4j
+public class MqttMsgSubscriber {
+
+ public void readSubscribeTopicMessage(){
+ try {
+ //String broker = "tcp://127.0.0.1:1883";
+ //String username = "mqtt_u";
+ //String password = "yjy";
+ //String topic = "workOrder";
+ String broker = "tcp://127.0.0.1:1884";
+ String username = "server";
+ String password = "1234";
+ String topic = "report/#";
+
+ Integer qos = 2;
+ String clientId = System.currentTimeMillis() + "";
+
+ MqttClient client = new MqttClient(broker, clientId, new MemoryPersistence());
+
+ // 杩炴帴鍙傛暟
+ MqttConnectOptions options = new MqttConnectOptions();
+ options.setUserName(username);
+ options.setPassword(password.toCharArray());
+ //鏄惁娓呴櫎浼氳瘽
+ options.setCleanSession(true);
+ options.setConnectionTimeout(60);
+ options.setKeepAliveInterval(60);
+ client.setCallback(new MqttCallback() {
+
+ @Override
+ public void connectionLost(Throwable throwable) {
+ log.error("杩炴帴涓㈠け");
+ }
+
+ @Override
+ public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
+ log.info("topic涓�: " + topic);
+ log.info("qos涓�: " + mqttMessage.getQos());
+ log.info("娑堟伅鍐呭涓�: " + new String(mqttMessage.getPayload()));
+ }
+
+ @Override
+ public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
+ // 褰撴秷鎭瀹屽叏浼犻�佸嚭鍘诲悗璋冪敤
+ log.info("浜や粯瀹屾垚 ---Delivery complete!");
+ // 鍙互鍦ㄨ繖閲屽鐞嗕竴浜涘彂閫佸畬鎴愬悗鐨勬竻鐞嗗伐浣�
+ }
+ });
+
+ client.connect(options);
+ client.subscribe(topic, qos);
+ } catch (MqttException e){
+ log.error("MqttMsgSubscriber 杩炴帴鍚姩寮傚父锛歿}", e.getMessage());
+ } catch (Exception e){
+ log.error("MqttMsgSubscriber 璇诲彇娑堟伅寮傚父锛歿}", e.getMessage());
+ }
+ }
+}
\ No newline at end of file
--
Gitblit v1.8.0