From e8c2c6d59405c68c855efd15de711e68fb13e658 Mon Sep 17 00:00:00 2001 From: lau572 <1010031226@qq.com> Date: Mon, 29 Jul 2024 11:23:16 +0800 Subject: [PATCH] =?UTF-8?q?mqtt=E8=BF=9E=E6=8E=A5=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NonAutomaticWarningController.java | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/zc-business/src/main/java/com/zc/business/controller/NonAutomaticWarningController.java b/zc-business/src/main/java/com/zc/business/controller/NonAutomaticWarningController.java index 18f5e314..e2642d2d 100644 --- a/zc-business/src/main/java/com/zc/business/controller/NonAutomaticWarningController.java +++ b/zc-business/src/main/java/com/zc/business/controller/NonAutomaticWarningController.java @@ -277,19 +277,29 @@ public class NonAutomaticWarningController extends BaseController { }); // 建立连接 - logger.info("连接到 broker: " + host); - client.connect(options); + try { + logger.info("连接到 broker: " + host); + client.connect(options); - logger.info("连接成功."); - //订阅消息 - //client.subscribe(topicName.getString("6274883585"), 1); - //logger.info("开始监听" + topicName.getString("6274883585")); - client.subscribe(topicName.getString("6274879489"), UniversalEnum.ONE.getNumber()); - logger.info("开始监听" + topicName.getString("6274879489")); - //client.subscribe(topicName.getString("7768236033"), 1); - //logger.info("开始监听" + topicName.getString("7768236033")); - //client.subscribe(topicName.getString("7835340801"), 1); - //logger.info("开始监听" + topicName.getString("7835340801")); + logger.info("连接成功."); + //订阅消息 + //client.subscribe(topicName.getString("6274883585"), 1); + //logger.info("开始监听" + topicName.getString("6274883585")); + client.subscribe(topicName.getString("6274879489"), UniversalEnum.ONE.getNumber()); + logger.info("开始监听" + topicName.getString("6274879489")); + //client.subscribe(topicName.getString("7768236033"), 1); + //logger.info("开始监听" + topicName.getString("7768236033")); + //client.subscribe(topicName.getString("7835340801"), 1); + //logger.info("开始监听" + topicName.getString("7835340801")); + } catch (MqttException e) { + if (e.getReasonCode() == MqttException.REASON_CODE_CLIENT_TIMEOUT) { + logger.warn("连接超时."); + // 尝试重连 + getAjaxResult(parameter); + } else { + throw e; // 抛出其他类型的异常 + } + } } catch (Exception e) { e.printStackTrace(); }