hivemq-mqtt-client
hivemq-mqtt-client copied to clipboard
connect() blocks forever if reconnect configured and broker cannot be reached
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
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);
}
})
...
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?
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
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?
@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?
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!