mqtt 优化

master
guochaojie 4 months ago
parent 615a12af00
commit 32e3afb2a7

@ -1,30 +0,0 @@
package cc.yunxi.config;
import cc.yunxi.config.props.MqttProperties;
import cc.yunxi.utils.CustomerMqttClient;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
@Slf4j
//@Configuration
//@EnableConfigurationProperties(MqttProperties.class)
public class MqttConfig {
// @Resource
private MqttProperties properties;
// @Bean
public CustomerMqttClient customerMqttClient() throws MqttException {
CustomerMqttClient client = new CustomerMqttClient();
// client.setMqttProperties(properties);
client.connect();
log.info("===========> MQTT连接成功 <===========");
return client;
}
}

@ -1,54 +0,0 @@
package cc.yunxi.utils;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.mqttv5.client.MqttClient;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.eclipse.paho.mqttv5.common.MqttMessage;
//@Component
@Slf4j
public class MqttTool {
// @Resource
private MqttClient client;
public boolean publish(String topic, MqttMessage message) {
try {
client.publish(topic, message);
log.warn("发布成功topic:{}message:{}", topic, message.getPayload());
return true;
} catch (MqttException e) {
log.error("发布失败topic:{}message:{}", topic, message.getPayload(), e);
e.printStackTrace();
return false;
}
}
public boolean subscribe(String topic, int qos) {
try {
if (client.isConnected() == false) {
log.warn("链接已断开topic:{}qos:{}", topic, qos);
}
client.subscribe(topic, qos);
log.warn("订阅成功topic:{}qos:{}", topic, qos);
return true;
} catch (MqttException e) {
log.warn("订阅主题失败topic:{}qos:{}", topic, qos);
e.printStackTrace();
return false;
}
}
public boolean isConnected() {
return client.isConnected();
}
public void setWill() throws MqttException {
// 设置遗嘱
String willTopic = "willTopic";
String willMessage = "willMessage";
int willQos = 2;
boolean willRetain = false;
// (willTopic, willMessage.getBytes(), willQos, willRetain);
}
}
Loading…
Cancel
Save