|
|
@@ -1,210 +1,333 @@
|
|
|
package com.nb.core.utils;
|
|
|
|
|
|
-import cn.hutool.core.util.ObjectUtil;
|
|
|
-import cn.hutool.json.JSONUtil;
|
|
|
-import com.nb.core.entity.MqttMessage;
|
|
|
-import org.springframework.integration.channel.DirectChannel;
|
|
|
-import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
|
|
|
-import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
|
|
|
-import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
|
|
|
-import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
|
|
|
-import org.springframework.messaging.MessageChannel;
|
|
|
-import org.springframework.messaging.MessageHandler;
|
|
|
-import org.springframework.messaging.MessagingException;
|
|
|
-import org.springframework.context.ApplicationContext;
|
|
|
-import org.springframework.context.ApplicationContextAware;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.scheduling.annotation.Async;
|
|
|
-
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
-import javax.annotation.PreDestroy;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.function.Consumer;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.eclipse.paho.client.mqttv3.*;
|
|
|
+import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
|
|
+
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.UUID;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
/**
|
|
|
* MQTT客户端工具类
|
|
|
- * 提供MQTT连接、消息发布、订阅等功能
|
|
|
+ * 提供MQTT连接、消息发布、订阅等通用功能
|
|
|
*
|
|
|
- * @author YourName
|
|
|
+ * @author lingma
|
|
|
* @version 1.0.0
|
|
|
+ * @since 1.0.0
|
|
|
*/
|
|
|
-@Component
|
|
|
-public class MqttClientUtil implements ApplicationContextAware {
|
|
|
- public static final String CLIENT_ID = "nb-netpump"+"-"+System.currentTimeMillis();
|
|
|
- private ApplicationContext applicationContext;
|
|
|
-
|
|
|
- private final Map<String, MqttPahoMessageDrivenChannelAdapter> subscribers = new ConcurrentHashMap<>();
|
|
|
-
|
|
|
- private MqttPahoClientFactory mqttClientFactory;
|
|
|
-
|
|
|
- private MessageChannel mqttInputChannel;
|
|
|
-
|
|
|
- public Boolean isOnline(){
|
|
|
- return ObjectUtil.isNotNull(mqttClientFactory);
|
|
|
- }
|
|
|
-
|
|
|
- @PostConstruct
|
|
|
- public void init() {
|
|
|
- // 创建默认的消息通道
|
|
|
- mqttInputChannel = new DirectChannel();
|
|
|
- // 尝试从Spring容器中获取MqttPahoClientFactory
|
|
|
- try {
|
|
|
- mqttClientFactory = applicationContext.getBean(MqttPahoClientFactory.class);
|
|
|
- } catch (Exception e) {
|
|
|
- mqttClientFactory=null;
|
|
|
- // 忽略异常,如果获取不到工厂则无法进行MQTT操作
|
|
|
+@Slf4j
|
|
|
+public class MqttClientUtil {
|
|
|
+
|
|
|
+ private MqttClient client;
|
|
|
+ private MqttConnectOptions options;
|
|
|
+ private String serverURI;
|
|
|
+ private String clientId;
|
|
|
+ private String username;
|
|
|
+ private String password;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ *
|
|
|
+ * @param serverURI MQTT服务器地址,例如:tcp://localhost:1883
|
|
|
+ * @param clientId 客户端ID,需要唯一
|
|
|
+ */
|
|
|
+ public MqttClientUtil(String serverURI, String clientId) {
|
|
|
+ this.serverURI = serverURI;
|
|
|
+ this.clientId = clientId;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造函数
|
|
|
+ *
|
|
|
+ * @param serverURI MQTT服务器地址,例如:tcp://localhost:1883
|
|
|
+ * @param clientId 客户端ID,需要唯一
|
|
|
+ * @param username 用户名
|
|
|
+ * @param password 密码
|
|
|
+ */
|
|
|
+ public MqttClientUtil(String serverURI, String clientId, String username, String password) {
|
|
|
+ this(serverURI, clientId);
|
|
|
+ this.username = username;
|
|
|
+ this.password = password;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化MQTT客户端
|
|
|
+ *
|
|
|
+ * @throws MqttException MQTT异常
|
|
|
+ */
|
|
|
+ public void init() throws MqttException {
|
|
|
+ if (client == null) {
|
|
|
+ client = new MqttClient(serverURI, clientId, new MemoryPersistence());
|
|
|
+ }
|
|
|
+
|
|
|
+ options = new MqttConnectOptions();
|
|
|
+ options.setCleanSession(true);
|
|
|
+ options.setConnectionTimeout(30);
|
|
|
+ options.setKeepAliveInterval(60);
|
|
|
+
|
|
|
+ if (username != null && password != null) {
|
|
|
+ options.setUserName(username);
|
|
|
+ options.setPassword(password.toCharArray());
|
|
|
+ }
|
|
|
+
|
|
|
+ client.setCallback(new MqttCallbackExtended() {
|
|
|
+ @Override
|
|
|
+ public void connectComplete(boolean reconnect, String serverURI) {
|
|
|
+ log.info("MQTT客户端连接完成 - serverURI: {}, clientId: {}, reconnect: {}", serverURI, clientId, reconnect);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void connectionLost(Throwable cause) {
|
|
|
+ log.error("MQTT客户端连接丢失 - clientId: {}", clientId, cause);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void messageArrived(String topic, MqttMessage message) throws Exception {
|
|
|
+ log.info("MQTT客户端收到消息 - topic: {}, message: {}", topic, new String(message.getPayload(), StandardCharsets.UTF_8));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deliveryComplete(IMqttDeliveryToken token) {
|
|
|
+ log.debug("MQTT消息发送完成 - token: {}", token);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 连接到MQTT服务器
|
|
|
+ *
|
|
|
+ * @throws MqttException MQTT异常
|
|
|
+ */
|
|
|
+ public void connect() throws MqttException {
|
|
|
+ if (client == null) {
|
|
|
+ init();
|
|
|
+ }
|
|
|
+ if (!client.isConnected()) {
|
|
|
+ client.connect(options);
|
|
|
+ log.info("MQTT客户端连接成功 - serverURI: {}, clientId: {}", serverURI, clientId);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @PreDestroy
|
|
|
- public void destroy() {
|
|
|
- // 清理所有订阅者
|
|
|
- subscribers.values().forEach(adapter -> {
|
|
|
+ /**
|
|
|
+ * 异步连接到MQTT服务器
|
|
|
+ *
|
|
|
+ * @return CompletableFuture<Void>
|
|
|
+ */
|
|
|
+ public CompletableFuture<Void> connectAsync() {
|
|
|
+ return CompletableFuture.runAsync(() -> {
|
|
|
try {
|
|
|
- adapter.stop();
|
|
|
- } catch (Exception e) {
|
|
|
- // 忽略停止异常
|
|
|
+ connect();
|
|
|
+ } catch (MqttException e) {
|
|
|
+ log.error("MQTT客户端连接失败 - serverURI: {}, clientId: {}", serverURI, clientId, e);
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
});
|
|
|
- subscribers.clear();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 断开MQTT连接
|
|
|
+ *
|
|
|
+ * @throws MqttException MQTT异常
|
|
|
+ */
|
|
|
+ public void disconnect() throws MqttException {
|
|
|
+ if (client != null && client.isConnected()) {
|
|
|
+ client.disconnect();
|
|
|
+ log.info("MQTT客户端断开连接 - clientId: {}", clientId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
- * 设置应用上下文
|
|
|
- * @param applicationContext 应用上下文
|
|
|
+ * 异步断开MQTT连接
|
|
|
+ *
|
|
|
+ * @return CompletableFuture<Void>
|
|
|
*/
|
|
|
- @Override
|
|
|
- public void setApplicationContext(ApplicationContext applicationContext) {
|
|
|
- this.applicationContext = applicationContext;
|
|
|
+ public CompletableFuture<Void> disconnectAsync() {
|
|
|
+ return CompletableFuture.runAsync(() -> {
|
|
|
+ try {
|
|
|
+ disconnect();
|
|
|
+ } catch (MqttException e) {
|
|
|
+ log.error("MQTT客户端断开连接失败 - clientId: {}", clientId, e);
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关闭MQTT客户端
|
|
|
+ *
|
|
|
+ * @throws MqttException MQTT异常
|
|
|
+ */
|
|
|
+ public void close() throws MqttException {
|
|
|
+ if (client != null) {
|
|
|
+ if (client.isConnected()) {
|
|
|
+ client.disconnect();
|
|
|
+ }
|
|
|
+ client.close();
|
|
|
+ log.info("MQTT客户端已关闭 - clientId: {}", clientId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步关闭MQTT客户端
|
|
|
+ *
|
|
|
+ * @return CompletableFuture<Void>
|
|
|
+ */
|
|
|
+ public CompletableFuture<Void> closeAsync() {
|
|
|
+ return CompletableFuture.runAsync(() -> {
|
|
|
+ try {
|
|
|
+ close();
|
|
|
+ } catch (MqttException e) {
|
|
|
+ log.error("MQTT客户端关闭失败 - clientId: {}", clientId, e);
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 发布消息
|
|
|
- * @param topic 主题
|
|
|
+ *
|
|
|
+ * @param topic 主题
|
|
|
* @param payload 消息内容
|
|
|
+ * @param qos 服务质量等级(0,1,2)
|
|
|
+ * @param retained 是否保留消息
|
|
|
+ * @throws MqttException MQTT异常
|
|
|
*/
|
|
|
- public void publish(String hospitalCode,String topic, Object payload) {
|
|
|
- publish(hospitalCode,topic, payload, 0, false);
|
|
|
+ public void publish(String topic, String payload, int qos, boolean retained) throws MqttException {
|
|
|
+ MqttMessage message = new MqttMessage(payload.getBytes(StandardCharsets.UTF_8));
|
|
|
+ message.setQos(qos);
|
|
|
+ message.setRetained(retained);
|
|
|
+ publish(topic, message);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
- * 发布消息到指定客户端
|
|
|
- * @param topic 主题
|
|
|
+ * 发布消息
|
|
|
+ *
|
|
|
+ * @param topic 主题
|
|
|
+ * @param message 消息
|
|
|
+ * @throws MqttException MQTT异常
|
|
|
+ */
|
|
|
+ public void publish(String topic, MqttMessage message) throws MqttException {
|
|
|
+ checkConnected();
|
|
|
+ client.publish(topic, message);
|
|
|
+ log.debug("MQTT消息发布成功 - topic: {}", topic);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步发布消息
|
|
|
+ *
|
|
|
+ * @param topic 主题
|
|
|
* @param payload 消息内容
|
|
|
- * @param qos 服务质量等级
|
|
|
+ * @param qos 服务质量等级(0,1,2)
|
|
|
* @param retained 是否保留消息
|
|
|
+ * @return CompletableFuture<Void>
|
|
|
*/
|
|
|
- public void publish(String hospitalCode, String topic, Object payload, int qos, boolean retained) {
|
|
|
- try {
|
|
|
- if (mqttClientFactory == null) {
|
|
|
- return;
|
|
|
+ public CompletableFuture<Void> publishAsync(String topic, String payload, int qos, boolean retained) {
|
|
|
+ return CompletableFuture.runAsync(() -> {
|
|
|
+ try {
|
|
|
+ publish(topic, payload, qos, retained);
|
|
|
+ } catch (MqttException e) {
|
|
|
+ log.error("MQTT消息发布失败 - topic: {}", topic, e);
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
-
|
|
|
- // 创建消息实体
|
|
|
- MqttMessage message = new MqttMessage(topic, JSONUtil.toJsonStr(payload));
|
|
|
- message.setClientId(CLIENT_ID);
|
|
|
-
|
|
|
- MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(CLIENT_ID, mqttClientFactory);
|
|
|
- messageHandler.setDefaultTopic("hospitalInfo/"+hospitalCode);
|
|
|
- messageHandler.setDefaultQos(qos);
|
|
|
- messageHandler.setDefaultRetained(retained);
|
|
|
- // 确保处理器已初始化
|
|
|
- messageHandler.afterPropertiesSet();
|
|
|
-
|
|
|
- org.springframework.messaging.Message<String> springMessage =
|
|
|
- org.springframework.messaging.support.MessageBuilder.withPayload(JSONUtil.toJsonStr(message)).build();
|
|
|
- messageHandler.handleMessage(springMessage);
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException("发布MQTT消息失败: " + e.getMessage(), e);
|
|
|
- }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 订阅主题
|
|
|
+ *
|
|
|
* @param topic 主题
|
|
|
- * @param messageHandler 消息处理器
|
|
|
+ * @param qos 服务质量等级
|
|
|
+ * @throws MqttException MQTT异常
|
|
|
*/
|
|
|
- public void subscribe(String topic, MessageHandler messageHandler) {
|
|
|
- try {
|
|
|
- if (mqttClientFactory == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- // 如果已经订阅了该主题,则先取消订阅
|
|
|
- if (subscribers.containsKey(topic)) {
|
|
|
- unsubscribe(topic);
|
|
|
- }
|
|
|
-
|
|
|
- MqttPahoMessageDrivenChannelAdapter adapter =
|
|
|
- new MqttPahoMessageDrivenChannelAdapter(CLIENT_ID, mqttClientFactory, topic);
|
|
|
- adapter.setConverter(new DefaultPahoMessageConverter());
|
|
|
- adapter.setOutputChannel(mqttInputChannel);
|
|
|
-
|
|
|
- // 创建一个带有处理逻辑的通道
|
|
|
- DirectChannel channel = new DirectChannel();
|
|
|
- channel.subscribe(messageHandler);
|
|
|
- adapter.setOutputChannel(channel);
|
|
|
-
|
|
|
- adapter.start();
|
|
|
-
|
|
|
- subscribers.put(topic, adapter);
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException("订阅MQTT主题失败: " + e.getMessage(), e);
|
|
|
- }
|
|
|
+ public void subscribe(String topic, int qos) throws MqttException {
|
|
|
+ checkConnected();
|
|
|
+ client.subscribe(topic, qos);
|
|
|
+ log.info("MQTT订阅主题成功 - topic: {}, qos: {}", topic, qos);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
- * 订阅主题(使用函数式接口处理消息)
|
|
|
+ * 异步订阅主题
|
|
|
+ *
|
|
|
* @param topic 主题
|
|
|
- * @param messageConsumer 消息消费者
|
|
|
+ * @param qos 服务质量等级
|
|
|
+ * @return CompletableFuture<Void>
|
|
|
*/
|
|
|
- public void subscribe(String topic, Consumer<String> messageConsumer) {
|
|
|
- MessageHandler handler = new MessageHandler() {
|
|
|
- @Override
|
|
|
- public void handleMessage(org.springframework.messaging.Message<?> message) throws MessagingException {
|
|
|
- String payload = new String((byte[]) message.getPayload());
|
|
|
- messageConsumer.accept(payload);
|
|
|
+ public CompletableFuture<Void> subscribeAsync(String topic, int qos) {
|
|
|
+ return CompletableFuture.runAsync(() -> {
|
|
|
+ try {
|
|
|
+ subscribe(topic, qos);
|
|
|
+ } catch (MqttException e) {
|
|
|
+ log.error("MQTT订阅主题失败 - topic: {}", topic, e);
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
- };
|
|
|
- subscribe(topic, handler);
|
|
|
+ });
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
- * 取消订阅
|
|
|
+ * 取消订阅主题
|
|
|
+ *
|
|
|
* @param topic 主题
|
|
|
+ * @throws MqttException MQTT异常
|
|
|
*/
|
|
|
- public void unsubscribe(String topic) {
|
|
|
- MqttPahoMessageDrivenChannelAdapter adapter = subscribers.get(topic);
|
|
|
- if (adapter != null) {
|
|
|
+ public void unsubscribe(String topic) throws MqttException {
|
|
|
+ checkConnected();
|
|
|
+ client.unsubscribe(topic);
|
|
|
+ log.info("MQTT取消订阅主题成功 - topic: {}", topic);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步取消订阅主题
|
|
|
+ *
|
|
|
+ * @param topic 主题
|
|
|
+ * @return CompletableFuture<Void>
|
|
|
+ */
|
|
|
+ public CompletableFuture<Void> unsubscribeAsync(String topic) {
|
|
|
+ return CompletableFuture.runAsync(() -> {
|
|
|
try {
|
|
|
- adapter.stop();
|
|
|
- } catch (Exception e) {
|
|
|
- // 忽略停止异常
|
|
|
+ unsubscribe(topic);
|
|
|
+ } catch (MqttException e) {
|
|
|
+ log.error("MQTT取消订阅主题失败 - topic: {}", topic, e);
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
- subscribers.remove(topic);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查客户端是否连接
|
|
|
+ *
|
|
|
+ * @throws MqttException MQTT异常
|
|
|
+ */
|
|
|
+ private void checkConnected() throws MqttException {
|
|
|
+ if (client == null || !client.isConnected()) {
|
|
|
+ throw new MqttException(MqttException.REASON_CODE_CLIENT_NOT_CONNECTED);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 异步发布消息
|
|
|
- * @param topic 主题
|
|
|
- * @param payload 消息内容
|
|
|
+ * 获取客户端ID
|
|
|
+ *
|
|
|
+ * @return 客户端ID
|
|
|
*/
|
|
|
- @Async
|
|
|
- public void asyncPublish(String hospitalCode,String topic, Object payload) {
|
|
|
- publish(hospitalCode,topic, payload);
|
|
|
+ public String getClientId() {
|
|
|
+ return clientId;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 异步发布消息到指定客户端
|
|
|
- * @param topic 主题
|
|
|
- * @param payload 消息内容
|
|
|
- * @param qos 服务质量等级
|
|
|
- * @param retained 是否保留消息
|
|
|
+ * 获取MQTT客户端
|
|
|
+ *
|
|
|
+ * @return MqttClient
|
|
|
+ */
|
|
|
+ public MqttClient getClient() {
|
|
|
+ return client;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成随机客户端ID
|
|
|
+ *
|
|
|
+ * @return 随机客户端ID
|
|
|
*/
|
|
|
- @Async
|
|
|
- public void asyncPublish(String hospitalCode, String topic, String payload, int qos, boolean retained) {
|
|
|
- publish(hospitalCode, topic, payload, qos, retained);
|
|
|
+ public static String generateClientId() {
|
|
|
+ return "mqtt_client_" + UUID.randomUUID().toString().replace("-", "");
|
|
|
}
|
|
|
}
|