eclipse-paho / paho.mqtt.java

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

Enhancement Request - Optional fixed automatic reconnect Interval #276

Open miketran78727 opened 8 years ago

miketran78727 commented 8 years ago

Please fill out the form below before submitting, thank you!

Currently, the reconnect interval starts from 1 second, then double the previous interval in the next attempt, with the max interval of 2 minutes. This implement works nicely for short interruption of network connectivity, but it does not work well for devices move between networks (e.g. cellular to WIFI) or in and out of a network. When the network layer and security settle, it is desirable to reconnect to the MQTT broker as soon as possible instead of the maximum 2-minute interval.

I am proposing an option to allow the application to set a fixed reconnect interval. If this option is not specified, the default behavior is the same as before.

    /**
     * Sets the reconnect interval.
     * This value, measured in seconds, defines the time interval
     * the client will attempt to reconnect.
     * The default behavior is the client will attempt to reconnect at one second
     * and double the interval on the next attempt.
     * A value of 0 meaning the client will use the default behavior.
     * @param reconnectInterval the time interval value, measured in seconds. It must be >0;
     */
    public void setAutomaticReconnectInterval(int reconnectInterval) {
        if (reconnectInterval < 0) {
            throw new IllegalArgumentException();
        }
        this.automaticReconnectInterval = reconnectInterval;
    }
jpwsutton commented 8 years ago

This sounds reasonable, we'll also have to add a flag that prevents the reconnect logic attempting to increment or reset the reconnectInterval if it has been set this way.

miketran78727 commented 8 years ago

Hi James, I will submit a pull request for this in the near future :)

icraggs commented 5 years ago

There is already a setting in the connect options: automaticReconnectMaxDelay. If minDelay and MaxDelay are set to the same value, then there will be a fixed reconnect interval.

mrpiggi commented 12 months ago

There is already a setting in the connect options: automaticReconnectMaxDelay. If minDelay and MaxDelay are set to the same value, then there will be a fixed reconnect interval.

see #864