1、优化MQTT初始连接

This commit is contained in:
weidong 2025-06-20 18:58:56 +08:00
parent 57de48e899
commit c7b1438d02
2 changed files with 19 additions and 2 deletions

View File

@ -41,6 +41,10 @@ public class MQTTClient {
client.connect(options);
}
public boolean isConnected(){
return client.isConnected();
}
public boolean publish(String topic, String message) {
if(!client.isConnected()){
logger.info("mqtt disconnected");

View File

@ -38,8 +38,13 @@ public class GXJKForwarder extends Forwarder {
void registerMe() throws MqttException {
init(FORWARDER_NAME, "MQTT "+brokerUrl,7,FWD_DEVICE_NAME,10);
mqttClient = new MQTTClient(brokerUrl, username, password,clientid);
try{
mqttClient.connect();
}
catch (Exception e){
}
}
/**
* 每半小时转发GNSS解算结果
@ -47,7 +52,15 @@ public class GXJKForwarder extends Forwarder {
@Scheduled(cron = "0 0/10 * * * ?") // 每30分钟执行一次
private void forwardGnss() {
logger.debug("gxjk forwardGnss");
forwardCurrentGnss();
if(mqttClient.isConnected()) forwardCurrentGnss();
else{
try{
mqttClient.connect();
}
catch (Exception e){
}
}
}
@Override