|
|
@@ -1,10 +1,17 @@
|
|
|
package com.yunfeiyun.agmp.iots.core.mqtt.network;
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.extra.spring.SpringUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
|
import org.eclipse.paho.client.mqttv3.MqttException;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
+
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
|
@Slf4j
|
|
|
public class MqttPublisher {
|
|
|
@@ -13,13 +20,27 @@ public class MqttPublisher {
|
|
|
|
|
|
private MqttClient mqttClient;
|
|
|
|
|
|
+ private ThreadPoolTaskExecutor threadPoolTaskExecutor;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public void init(MqttCore mqttCore) {
|
|
|
this.mqttCore = mqttCore;
|
|
|
this.mqttClient = mqttCore.getClient();
|
|
|
+ this.threadPoolTaskExecutor = SpringUtil.getBean("MqttTopicPublishExecutor");
|
|
|
}
|
|
|
|
|
|
public void publish(String topic, String message) throws MqttException {
|
|
|
- log.info("发MQTT消息,topic:{},message:{}",topic,message);
|
|
|
- mqttClient.publish(topic, message.getBytes(), 0, false);
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
+ try {
|
|
|
+ log.info("发MQTT消息,topic:{},message:{}",topic,message);
|
|
|
+ mqttClient.publish(topic, message.getBytes(), 0, false);
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("发MQTT消息失败",e);
|
|
|
+ }
|
|
|
+ },threadPoolTaskExecutor);
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|