|
|
|
@ -10,21 +10,31 @@ import org.eclipse.paho.mqttv5.client.MqttTopic;
|
|
|
|
|
import org.eclipse.paho.mqttv5.client.persist.MemoryPersistence;
|
|
|
|
|
import org.eclipse.paho.mqttv5.common.MqttException;
|
|
|
|
|
import org.eclipse.paho.mqttv5.common.MqttMessage;
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Component
|
|
|
|
|
@EnableConfigurationProperties(MqttProperties.class)
|
|
|
|
|
public class CustomerMqttClient {
|
|
|
|
|
private static MqttClient client;
|
|
|
|
|
@Resource
|
|
|
|
|
private MyMqttCallback callback;
|
|
|
|
|
private MqttClient client;
|
|
|
|
|
@Resource
|
|
|
|
|
private MqttProperties properties;
|
|
|
|
|
|
|
|
|
|
public static MqttClient getClient() {
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void setClient(MqttClient client) {
|
|
|
|
|
CustomerMqttClient.client = client;
|
|
|
|
|
@PostConstruct
|
|
|
|
|
public void init() {
|
|
|
|
|
try {
|
|
|
|
|
connect();
|
|
|
|
|
} catch (MqttException e) {
|
|
|
|
|
log.error("连接MQTT服务失败", e);
|
|
|
|
|
throw new BizIllegalException("连接MQTT服务失败");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void connect() throws MqttException {
|
|
|
|
@ -40,8 +50,9 @@ public class CustomerMqttClient {
|
|
|
|
|
options.setKeepAliveInterval(properties.getKeepAlive());
|
|
|
|
|
options.setConnectionTimeout(properties.getTimeout());
|
|
|
|
|
options.setAutomaticReconnect(true);
|
|
|
|
|
options.setAutomaticReconnectDelay(1, 3);
|
|
|
|
|
options.setSessionExpiryInterval(0L);//会话过期时间
|
|
|
|
|
client.setCallback(new MyMqttCallback(client));
|
|
|
|
|
client.setCallback(callback);
|
|
|
|
|
if (!client.isConnected()) {
|
|
|
|
|
client.connect(options);
|
|
|
|
|
log.info("连接MQTT服务成功 success");
|
|
|
|
@ -50,6 +61,7 @@ public class CustomerMqttClient {
|
|
|
|
|
client.connect(options);
|
|
|
|
|
log.info("重新连接MQTT服务成功 success");
|
|
|
|
|
}
|
|
|
|
|
callback.registerClient(client);
|
|
|
|
|
initSubscribe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -83,19 +95,6 @@ public class CustomerMqttClient {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new BizIllegalException("mqtt 发布主题失败,发布主题:" + topic + ",发布消息:" + msg);
|
|
|
|
|
}
|
|
|
|
|
// MqttDeliveryToken token;//Delivery:配送
|
|
|
|
|
// synchronized (this) {//注意:这里一定要同步,否则,在多线程publish的情况下,线程会发生死锁,分析见文章最后补充
|
|
|
|
|
// try {
|
|
|
|
|
// token = mqttTopic.publish(message);//也是发送到执行队列中,等待执行线程执行,将消息发送到消息中间件
|
|
|
|
|
// token.waitForCompletion(1000L);
|
|
|
|
|
// } catch (MqttPersistenceException e) {
|
|
|
|
|
// log.error("发布主题失败 topic:{}, qos: {} ", topic, qos);
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
// } catch (MqttException e) {
|
|
|
|
|
// log.error("发布主题失败 topic:{}, qos: {} ", topic, qos);
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void subscribe(String topicFilter, int qos) {
|
|
|
|
@ -129,6 +128,9 @@ public class CustomerMqttClient {
|
|
|
|
|
public void initSubscribe() {
|
|
|
|
|
if (client != null && client.isConnected()) {
|
|
|
|
|
Map<String, Integer> topics = properties.getTopics();
|
|
|
|
|
if(topics == null || topics.size() == 0) {
|
|
|
|
|
log.info("未配置初始化订阅主题!!!");
|
|
|
|
|
}
|
|
|
|
|
for (Map.Entry<String, Integer> entry : topics.entrySet()) {
|
|
|
|
|
String topic = entry.getKey();
|
|
|
|
|
int qos = entry.getValue();
|
|
|
|
@ -139,8 +141,4 @@ public class CustomerMqttClient {
|
|
|
|
|
log.error("客户端未连接,初始化订阅失败!!!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setMqttProperties(MqttProperties mqttProperties) {
|
|
|
|
|
this.properties = mqttProperties;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|