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

Enhancement Request - Optional fixed automatic reconnect Interval

Open miketran78727 opened this issue 9 years ago • 4 comments
trafficstars

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

  • [x] Bug exists Release Version 1.1.0 ( Master Branch)
  • [x] Bug exists in Snapshot Version 1.1.1-SNAPSHOT (Develop Branch)

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

miketran78727 avatar Oct 04 '16 20:10 miketran78727

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.

jpwsutton avatar Oct 18 '16 08:10 jpwsutton

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

miketran78727 avatar Oct 18 '16 15:10 miketran78727

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.

icraggs avatar Nov 21 '18 19:11 icraggs

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

mrpiggi avatar Nov 02 '23 22:11 mrpiggi