1、修改mqtt的bug
This commit is contained in:
parent
4d03fc11c7
commit
c40a3c2d05
@ -36,22 +36,27 @@ public class MQTTClient {
|
|||||||
options.setCleanSession(true);
|
options.setCleanSession(true);
|
||||||
options.setUserName(username);
|
options.setUserName(username);
|
||||||
options.setPassword(password.toCharArray());
|
options.setPassword(password.toCharArray());
|
||||||
|
options.setAutomaticReconnect(true);
|
||||||
|
//options.setKeepAliveInterval();default:30s
|
||||||
// Connect to the MQTT broker.
|
// Connect to the MQTT broker.
|
||||||
client.connect(options);
|
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.
|
try { // Create a new MQTT message.
|
||||||
MqttMessage mqttMessage = new MqttMessage(message.getBytes());
|
MqttMessage mqttMessage = new MqttMessage(message.getBytes());
|
||||||
mqttMessage.setQos(qos);
|
mqttMessage.setQos(qos);
|
||||||
// Publish the MQTT message to the topic.
|
// Publish the MQTT message to the topic.
|
||||||
client.publish(topic, mqttMessage);
|
client.publish(topic, mqttMessage);
|
||||||
System.out.println("Message published");
|
|
||||||
System.out.println("topic: " + topic);
|
|
||||||
System.out.println("message content: " + mqttMessage);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("mqtt推送gnss数据异常:", e);
|
logger.error("mqtt推送gnss数据异常:", e);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect() throws MqttException {
|
public void disconnect() throws MqttException {
|
||||||
|
|||||||
@ -76,11 +76,10 @@ public class GZYMQTTForwarder extends Forwarder {
|
|||||||
tranData.setDeviceType(2);
|
tranData.setDeviceType(2);
|
||||||
tranData.setDeviceSn(locationRecord.getDeviceid());
|
tranData.setDeviceSn(locationRecord.getDeviceid());
|
||||||
String json = GsonUtil.toJson(tranData);
|
String json = GsonUtil.toJson(tranData);
|
||||||
String msg = "JGKJ" + json + "#!";
|
|
||||||
logger.info("forward to GZY mqtt");
|
logger.info("forward to GZY mqtt");
|
||||||
logger.info(msg);
|
logger.info(json);
|
||||||
try {
|
try {
|
||||||
mqttClient.publish(msg);
|
if(!mqttClient.publish(json)) break;
|
||||||
Thread.sleep(200);
|
Thread.sleep(200);
|
||||||
} catch (Exception e1) {
|
} catch (Exception e1) {
|
||||||
e1.printStackTrace();
|
e1.printStackTrace();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user