paho.mqtt.java icon indicating copy to clipboard operation
paho.mqtt.java copied to clipboard

Calling disconnect should turn off auto reconnect

Open jianglijs opened this issue 8 years ago • 7 comments
trafficstars

  • library version org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.1

jianglijs avatar Oct 11 '17 11:10 jianglijs

  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

jianglijs avatar Oct 11 '17 11:10 jianglijs

  • Release Version 1.2.0 ( Master Branch)

I have also observed the behavior.

Steps to reproduce:

  1. Establish a successful connection to a broker using MqttConnectOptions.setAutomaticReconnect(true).
  2. Shutdown the broker.
  3. Disconnect and close the MQTT client. reconnectTimer seems to remain alive and prevents an application from terminating properly.

fross avatar Jan 29 '18 19:01 fross

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).
     }
}

arunseshagiri avatar Apr 11 '18 07:04 arunseshagiri

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:

  1. Page with MQTT functionality is opened. Here, we connect with automaticReconnect true.
  2. Network goes bad, client gets disconnected. Reconnect cycle starts.
  3. 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 close because we want to reuse it when page gets reopened)

manas-chaudhari avatar Aug 20 '18 13:08 manas-chaudhari

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. image

curtiseng avatar Jan 12 '21 06:01 curtiseng

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();
	}

imcloud avatar Jun 28 '23 10:06 imcloud

Temporary fix:

image

bcichocki avatar Oct 13 '23 10:10 bcichocki