eclipse / paho.mqtt.java

Eclipse Paho Java MQTT client library. Paho is an Eclipse IoT project.
https://eclipse.org/paho
Other
2.12k stars 884 forks source link

Calling disconnect should turn off auto reconnect #425

Open jianglijs opened 6 years ago

jianglijs commented 6 years ago
jianglijs commented 6 years ago
  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
fross commented 6 years ago

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.
arunseshagiri commented 6 years ago

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).
     }
}
manas-chaudhari commented 6 years ago

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)
curtiseng commented 3 years ago

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

imcloud commented 1 year ago

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();
    }
bcichocki commented 11 months ago

Temporary fix:

image