paho.mqtt.java
paho.mqtt.java copied to clipboard
Calling disconnect should turn off auto reconnect
- library version org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1
- connection lost,such as close the network 2.disconnect,we can call it manually.thus,we host the reconnect recycle is canceled.however,it isn't
- Release Version 1.2.0 ( Master Branch)
I have also observed the behavior.
Steps to reproduce:
- Establish a successful connection to a broker using
MqttConnectOptions.setAutomaticReconnect(true). - Shutdown the broker.
- Disconnect and close the MQTT client.
reconnectTimerseems to remain alive and prevents an application from terminating properly.
I was also facing the same issue. I did a hacky way to resolve it.
when mqttClient.disconnect() is successful, set the Auto reconnect flag to false which actually stops the retry attempts.
mqttClient.disconnect(0, clientId, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
MqttConnectOptions.setAutomaticReconnect(false).
}
}
There needs to be a way to stop the reconnection. OR at least the disconnect function should stop the reconnection cycle (in disconnected state).
Usecase:
- Page with MQTT functionality is opened. Here, we connect with automaticReconnect true.
- Network goes bad, client gets disconnected. Reconnect cycle starts.
- Page is closed. Here, there needs to be a way to stop the reconnection cycle as connection is no longer needed. (We don't want to call
closebecause we want to reuse it when page gets reopened)
I think this happened to our system too, and cause connect infinite loop. Because of when closed connection, we create new connection, but the last connection still connecting automatically, and the client-id is same.

This behavior is really strange, why is there no interface provided to stop the task
private void stopReconnectCycle() {
String methodName = "stopReconnectCycle";
// @Trace 504=Stop reconnect timer for client: {0}
log.fine(CLASS_NAME, methodName, "504", new Object[] { this.clientId });
synchronized (clientLock) {
if (this.connOpts.isAutomaticReconnect()) {
if (reconnectTimer != null) {
reconnectTimer.cancel();
reconnectTimer = null;
}
reconnectDelay = 1000; // Reset Delay Timer
}
}
}
This is an internal method that will only be called when 'reconnect' is called
public void reconnect() throws MqttException {
final String methodName = "reconnect";
// @Trace 500=Attempting to reconnect client: {0}
log.fine(CLASS_NAME, methodName, "500", new Object[] { this.clientId });
// Some checks to make sure that we're not attempting to reconnect an
// already connected client
if (comms.isConnected()) {
throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_CLIENT_CONNECTED);
}
if (comms.isConnecting()) {
throw new MqttException(MqttException.REASON_CODE_CONNECT_IN_PROGRESS);
}
if (comms.isDisconnecting()) {
throw new MqttException(MqttException.REASON_CODE_CLIENT_DISCONNECTING);
}
if (comms.isClosed()) {
throw new MqttException(MqttException.REASON_CODE_CLIENT_CLOSED);
}
// We don't want to spam the server
stopReconnectCycle();
attemptReconnect();
}
Temporary fix: