hivemq-mqtt-client icon indicating copy to clipboard operation
hivemq-mqtt-client copied to clipboard

connect() blocks forever if reconnect configured and broker cannot be reached

Open dowhiletrue opened this issue 5 years ago • 5 comments

Expected behavior

Connecting a client with reconnect configured should throw if it can't connect initially.

Actual behavior

It blocks infinitely

To Reproduce

Reproducer code

Mqtt5Client.builder().serverHost("hostWithoutRunningBroker").automaticReconnectWithDefaultConfig().toBlocking().connect(...)

Details

  • Affected HiveMQ MQTT Client version(s): 1.1.0
  • Used JVM version: 11

dowhiletrue avatar Jun 18 '19 15:06 dowhiletrue

This was actually intended. We should discuss what the default behavior should be. You can work around in the mean time:

Mqtt5Client.builder()
        ...
        .automaticReconnectWithDefaultConfig()
        .addDisconnectedListener(context -> {
            if (context.getClientConfig().getState() == MqttClientState.CONNECTING) {
                context.getReconnector().reconnect(false);
            }
        })
        ...

SgtSilvio avatar Jun 18 '19 20:06 SgtSilvio

Thanks for the workaround!

There are three options on top of my mind:

  • block
  • throw
  • continue and try to connect later

Which option fits best depends on the use-case. Make the behavior configurable?

dowhiletrue avatar Jun 19 '19 06:06 dowhiletrue

Hello i just want to poke this, cause it tooks me 2 days to understand why "exceptionally" is not working

This definitely needs to be highlighted somewhere

Razikus avatar Apr 09 '20 19:04 Razikus

I think I might have encountered this problem. @dowhiletrue I think throwing an exception should be the default behavior as it informs the developers if their connection configuration is incorrect; I think jdbc connection pools also have such default behavior. @SgtSilvio can you please explain what happens if I use your workaround? does it attempt to reconnect automatically initially or otherwise or does it abandon reconnection and throw an exception?

ravenblackdusk avatar Oct 30 '21 12:10 ravenblackdusk

@SgtSilvio I also had this problem: https://stackoverflow.com/questions/71467511/hivemq-mqtt-client?noredirect=1#comment126319388_71467511

Is it not possible to have automaticReconnectWithDefaultConfig() and that

.whenComplete((connAck, throwable) ->

throws error?

CJure avatar Mar 15 '22 11:03 CJure

Since this issue has gone stale, I've filed a feature request with summary (and current work-around) in #622. If anyone has other ideas for improvement, please add to #622.

Thanks all for pointing this out and the helpful feedback!

pglombardo avatar Mar 05 '24 12:03 pglombardo