|
|
@@ -1,8 +1,8 @@
|
|
|
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.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.integration.channel.DirectChannel;
|
|
|
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
|
|
|
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
|
|
|
@@ -14,6 +14,7 @@ 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;
|
|
|
@@ -30,22 +31,23 @@ import java.util.function.Consumer;
|
|
|
*/
|
|
|
@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 String defaultClientId = "nb-netpump";
|
|
|
-
|
|
|
+
|
|
|
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);
|
|
|
@@ -81,103 +83,60 @@ public class MqttClientUtil implements ApplicationContextAware {
|
|
|
* @param topic 主题
|
|
|
* @param payload 消息内容
|
|
|
*/
|
|
|
- public void publish(String topic, String payload) {
|
|
|
- publish(topic, payload, 0, false);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 发布消息
|
|
|
- * @param topic 主题
|
|
|
- * @param payload 消息内容
|
|
|
- * @param qos 服务质量等级 (0, 1, 2)
|
|
|
- * @param retained 是否保留消息
|
|
|
- */
|
|
|
- public void publish(String topic, String payload, int qos, boolean retained) {
|
|
|
- try {
|
|
|
- // 创建消息实体
|
|
|
- MqttMessage message = new MqttMessage(topic, payload);
|
|
|
- message.setClientId(defaultClientId + "-pub-" + System.currentTimeMillis());
|
|
|
- // 确保客户端工厂已初始化
|
|
|
- if (mqttClientFactory == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(defaultClientId + "-pub-" + System.currentTimeMillis(), mqttClientFactory);
|
|
|
- messageHandler.setDefaultTopic(topic);
|
|
|
- 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);
|
|
|
- }
|
|
|
+ public void publish(String hospitalCode,String topic, Object payload) {
|
|
|
+ publish(hospitalCode,topic, payload, 0, false);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发布消息到指定客户端
|
|
|
- * @param clientId 客户端ID
|
|
|
* @param topic 主题
|
|
|
* @param payload 消息内容
|
|
|
* @param qos 服务质量等级
|
|
|
* @param retained 是否保留消息
|
|
|
*/
|
|
|
- public void publish(String clientId, String topic, String payload, int qos, boolean retained) {
|
|
|
+ public void publish(String hospitalCode, String topic, Object payload, int qos, boolean retained) {
|
|
|
try {
|
|
|
// 创建消息实体
|
|
|
- MqttMessage message = new MqttMessage(topic, payload);
|
|
|
- message.setClientId(clientId + "-pub-" + System.currentTimeMillis());
|
|
|
+ MqttMessage message = new MqttMessage(topic, JSONUtil.toJsonStr( payload));
|
|
|
+ message.setClientId(CLIENT_ID);
|
|
|
|
|
|
if (mqttClientFactory == null) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(clientId + "-pub-" + System.currentTimeMillis(), mqttClientFactory);
|
|
|
- messageHandler.setDefaultTopic(topic);
|
|
|
+ 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(payload).build();
|
|
|
+ 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 消息处理器
|
|
|
*/
|
|
|
- public void subscribe(String topic, MessageHandler messageHandler) {
|
|
|
- subscribe(defaultClientId + "-sub-" + System.currentTimeMillis(), topic, messageHandler);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 订阅主题
|
|
|
- * @param clientId 客户端ID
|
|
|
- * @param topic 主题
|
|
|
- * @param messageHandler 消息处理器
|
|
|
- */
|
|
|
- public void subscribe(String clientId, String topic, MessageHandler messageHandler) {
|
|
|
+ public void subscribe( String topic, MessageHandler messageHandler) {
|
|
|
try {
|
|
|
if (mqttClientFactory == null) {
|
|
|
return;
|
|
|
}
|
|
|
// 如果已经订阅了该主题,则先取消订阅
|
|
|
- String key = clientId + ":" + topic;
|
|
|
- if (subscribers.containsKey(key)) {
|
|
|
- unsubscribe(clientId, topic);
|
|
|
+ if (subscribers.containsKey(topic)) {
|
|
|
+ unsubscribe( topic);
|
|
|
}
|
|
|
|
|
|
MqttPahoMessageDrivenChannelAdapter adapter =
|
|
|
- new MqttPahoMessageDrivenChannelAdapter(clientId, mqttClientFactory, topic);
|
|
|
+ new MqttPahoMessageDrivenChannelAdapter(CLIENT_ID, mqttClientFactory, topic);
|
|
|
adapter.setConverter(new DefaultPahoMessageConverter());
|
|
|
adapter.setOutputChannel(mqttInputChannel);
|
|
|
|
|
|
@@ -188,7 +147,7 @@ public class MqttClientUtil implements ApplicationContextAware {
|
|
|
|
|
|
adapter.start();
|
|
|
|
|
|
- subscribers.put(key, adapter);
|
|
|
+ subscribers.put(topic, adapter);
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException("订阅MQTT主题失败: " + e.getMessage(), e);
|
|
|
}
|
|
|
@@ -200,16 +159,6 @@ public class MqttClientUtil implements ApplicationContextAware {
|
|
|
* @param messageConsumer 消息消费者
|
|
|
*/
|
|
|
public void subscribe(String topic, Consumer<String> messageConsumer) {
|
|
|
- subscribe(defaultClientId + "-sub-" + System.currentTimeMillis(), topic, messageConsumer);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 订阅主题(使用函数式接口处理消息)
|
|
|
- * @param clientId 客户端ID
|
|
|
- * @param topic 主题
|
|
|
- * @param messageConsumer 消息消费者
|
|
|
- */
|
|
|
- public void subscribe(String clientId, String topic, Consumer<String> messageConsumer) {
|
|
|
MessageHandler handler = new MessageHandler() {
|
|
|
@Override
|
|
|
public void handleMessage(org.springframework.messaging.Message<?> message) throws MessagingException {
|
|
|
@@ -217,20 +166,41 @@ public class MqttClientUtil implements ApplicationContextAware {
|
|
|
messageConsumer.accept(payload);
|
|
|
}
|
|
|
};
|
|
|
- subscribe(clientId, topic, handler);
|
|
|
+ subscribe(topic, handler);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 取消订阅
|
|
|
- * @param clientId 客户端ID
|
|
|
* @param topic 主题
|
|
|
*/
|
|
|
- public void unsubscribe(String clientId, String topic) {
|
|
|
- String key = clientId + ":" + topic;
|
|
|
+ public void unsubscribe( String topic) {
|
|
|
+ String key = topic;
|
|
|
MqttPahoMessageDrivenChannelAdapter adapter = subscribers.get(key);
|
|
|
if (adapter != null) {
|
|
|
adapter.stop();
|
|
|
subscribers.remove(key);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步发布消息
|
|
|
+ * @param topic 主题
|
|
|
+ * @param payload 消息内容
|
|
|
+ */
|
|
|
+ @Async
|
|
|
+ public void asyncPublish(String hospitalCode,String topic, Object payload) {
|
|
|
+ publish(hospitalCode,topic, payload);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 异步发布消息到指定客户端
|
|
|
+ * @param topic 主题
|
|
|
+ * @param payload 消息内容
|
|
|
+ * @param qos 服务质量等级
|
|
|
+ * @param retained 是否保留消息
|
|
|
+ */
|
|
|
+ @Async
|
|
|
+ public void asyncPublish(String hospitalCode, String topic, String payload, int qos, boolean retained) {
|
|
|
+ publish(hospitalCode, topic, payload, qos, retained);
|
|
|
+ }
|
|
|
}
|