Pārlūkot izejas kodu

相机树开发

gao.qiang 3 mēneši atpakaļ
vecāks
revīzija
785b18015f

+ 3 - 2
business-service/src/main/java/com/ozs/service/utils/PublishCallback.java

@@ -26,7 +26,8 @@ import java.util.List;
 public class PublishCallback implements MqttCallback {
 
     public static final Logger logger = LoggerFactory.getLogger(PublishCallback.class);
-
+    @Resource
+    private PublishClient publishClient;
 
     //在断开连接时调用
     @Override
@@ -35,7 +36,7 @@ public class PublishCallback implements MqttCallback {
         logger.error("连接断开,原因:" + cause);
         try {
             logger.error("------connectionLost-------");
-            PublishClient.reconnect();
+            publishClient.reconnect();
         } catch (Exception e) {
             logger.error("重连失败:" + e);
         }

+ 17 - 9
business-service/src/main/java/com/ozs/service/utils/PublishClient.java

@@ -287,7 +287,7 @@ public class PublishClient {
         }
     }
 
-    public static void reconnect() throws MqttException {
+    public  void reconnect() throws MqttException {
         log.error("尝试重连...");
         if (client != null) {
             try {
@@ -299,17 +299,25 @@ public class PublishClient {
 
         MqttConnectOptions options = new MqttConnectOptions();
         options.setCleanSession(true);
-        options.setUserName(USER_NAME);
-        options.setPassword(PASS_WORD.toCharArray());
+        options.setUserName("camera-update");
+        options.setPassword("05J5+mtYzx.Ry".toCharArray());
         options.setConnectionTimeout(60);
         options.setKeepAliveInterval(40);
 
-        client = new MqttClient(HOST, String.valueOf(clientId), new MemoryPersistence());
-        client.setCallback(new PublishCallback());
-        client.connect(options);
-        int[] Qos = {1};
-        String[] topics = {"config", "update", "heart", "test"};
-        client.subscribe(topics, Qos);
+        try {
+            /**
+             * 设置发布回调
+             */
+            client = new MqttClient("tcp://10.161.12.60:1883", "HAZARD-CAMERA-CLIENTID-123", new MemoryPersistence());
+
+            client.setCallback(new PublishCallback());
+            client.connect(options);
+            String[] topic1 = {"config", "update", "heart", "test"};
+            client.subscribe(topic1);
+        } catch (Exception e) {
+            log.error(e.getMessage());
+            e.printStackTrace();
+        }
         log.error("重连成功!");
     }
 }