Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[paho-dev] Long timeouts to real sites with no MQTT

When I make a connection to a valid looking Host Address, but which does not have an MQTT server running I get a seriously long timeout (I think it may actually never end in some cases).  I tried setting the Connection Timeout to 20, or 5, or anything, but it doesn't work.  Anything I can try to make the client realize the connection is no-good earlier?  Alternatively is there something I can manually do in another thread to make the connection stop trying?

This is a big issue for me, because the setting will be user-configurable and if it takes this long to fail it means a negative and very visible user experience.  I would like it to fail in 30 seconds or less so I can prompt for them to modify the address without making them wait for minutes to set up.

Thanks,
Steve

Here is sample code which shows the problem.  In plain desktop Java it takes a consistent 1 minute, 3 seconds to fail.  I use the same code on android, and on WiFi it takes about the same time (a little over a minute), but on 3G/4G it takes 2+ minutes, and I timed one (google.com) at >10 minutes before killing it.

public class SuperSimple
{
    public static final String timeDiffFormat = "%1$tM:%1$tS.%1$tL";
    private static MqttClient client;

    public static void main(String... args)
    {
        final long startTime = System.currentTimeMillis();
        try
        {
            client = new MqttClient("tcp://some.company.net:1883", "this_should_fail_fast");

            MqttConnectOptions mOptions = new MqttConnectOptions();
            mOptions.setKeepAliveInterval(20);
            mOptions.setConnectionTimeout(20);
            client.connect(mOptions);

        }
        catch (MqttException mEx)
        {
            long errorTime = System.currentTimeMillis();
            Date difference = new Date(errorTime - startTime);
            String time = String.format(Locale.US, timeDiffFormat, difference);
            String message = mEx.getMessage();
            System.out.printf("(%s)Exception: %s%n", time, message);
        }

        try
        {
            client.disconnect();
        }
        catch (MqttException e)
        {
            //This always happens: already disconnected.
        }
    }
}


Back to the top