1、修改mqtt的bug

This commit is contained in:
weidong 2024-01-26 14:11:56 +08:00
parent 4d03fc11c7
commit c40a3c2d05
2 changed files with 11 additions and 7 deletions

View File

@ -36,22 +36,27 @@ public class MQTTClient {
options.setCleanSession(true);
options.setUserName(username);
options.setPassword(password.toCharArray());
options.setAutomaticReconnect(true);
//options.setKeepAliveInterval();default:30s
// Connect to the MQTT broker.
client.connect(options);
}
public void publish(String message) throws MqttException {
public boolean publish(String message) throws MqttException {
if(!client.isConnected()){
logger.info("mqtt disconnected");
return false;
}
try { // Create a new MQTT message.
MqttMessage mqttMessage = new MqttMessage(message.getBytes());
mqttMessage.setQos(qos);
// Publish the MQTT message to the topic.
client.publish(topic, mqttMessage);
System.out.println("Message published");
System.out.println("topic: " + topic);
System.out.println("message content: " + mqttMessage);
} catch (Exception e) {
logger.error("mqtt推送gnss数据异常:", e);
return false;
}
return true;
}
public void disconnect() throws MqttException {

View File

@ -76,11 +76,10 @@ public class GZYMQTTForwarder extends Forwarder {
tranData.setDeviceType(2);
tranData.setDeviceSn(locationRecord.getDeviceid());
String json = GsonUtil.toJson(tranData);
String msg = "JGKJ" + json + "#!";
logger.info("forward to GZY mqtt");
logger.info(msg);
logger.info(json);
try {
mqttClient.publish(msg);
if(!mqttClient.publish(json)) break;
Thread.sleep(200);
} catch (Exception e1) {
e1.printStackTrace();