使用EMQX集群版本 [5.0.20],通过负载均衡访问, mqtt客户端出现断开连接后,重连一直失败的情况:
客户端配置:
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName(username);
options.setPassword(password.toCharArray());
options.setConnectionTimeout(timeout);
options.setKeepAliveInterval(keepalive);
options.setCleanSession(false);
return options;
重连代码:
public void connectionLost(Throwable throwable) {
LOGGER.error(“MqttCallback 连接断开,下面做重连…”);
long reconnectTimes = 1;
while (true) {
try {
if (MqttClient.getClient().isConnected()) {
LOGGER.warn(“mqtt reconnect success end”);
mqttClient.subscribe(“/test/#”, 1);
return;
}
LOGGER.warn(“mqtt reconnect times = {} try again…”, reconnectTimes++);
MqttClient.getClient().reconnect();
} catch (MqttException e) {
LOGGER.error(“”, e);
}
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
}
}
}
在监听到连接断开后,进行重连一直报错: MQTT reconnect error: Connect already in progress
2023-10-07 22:07:12 [MQTT Call: back2SubForEvent] ERROR com.apsystems.inverter.event.mqtt.MqttCustomerCallback.connectionLost(MqttCustomerCallback.java:42) - MQTT reconnect error: Connect already in progress
org.eclipse.paho.client.mqttv3.MqttException: Connect already in progress
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.reconnect(MqttAsyncClient.java:1399) ~[org.eclipse.paho.client.mqttv3-1.2.5.jar!/:?]
at org.eclipse.paho.client.mqttv3.MqttClient.reconnect(MqttClient.java:711) ~[org.eclipse.paho.client.mqttv3-1.2.5.jar!/:?]
at com.apsystems.inverter.event.mqtt.MqttCustomerCallback.connectionLost(MqttCustomerCallback.java:40) ~[classes!/:0.0.1]
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.connectionLost(CommsCallback.java:304) ~[org.eclipse.paho.client.mqttv3-1.2.5.jar!/:?]
at org.eclipse.paho.client.mqttv3.internal.ClientComms.shutdownConnection(ClientComms.java:441) ~[org.eclipse.paho.client.mqttv3-1.2.5.jar!/:?]
at org.eclipse.paho.client.mqttv3.internal.CommsCallback.run(CommsCallback.java:228) ~[org.eclipse.paho.client.mqttv3-1.2.5.jar!/:?]
at java.lang.Thread.run(Thread.java:748) ~[?:1.8.0_271]