hivemq-mqtt-client
hivemq-mqtt-client copied to clipboard
Cancel automatic reconnect calling connect or disconnect
Hey there.
Is there any chance to cancel automatic reconnect? Calling disconnect() won't help since it will throw because we are not connected.
Hi @degill Yes, you can add a DisconnectedListener and set reconnect to false, for example:
final AtomicBoolean cancelReconnect = new AtomicBoolean(false);
Mqtt5Client.builder()
.automaticReconnectWithDefaultConfig()
.addDisconnectedListener(context -> {
if (cancelReconnect.get()) {
context.getReconnector().reconnect(false);
}
})
...
Hi @degill Yes, you can add a DisconnectedListener and set reconnect to false, for example:
final AtomicBoolean cancelReconnect = new AtomicBoolean(false); Mqtt5Client.builder() .automaticReconnectWithDefaultConfig() .addDisconnectedListener(context -> { if (cancelReconnect.get()) { context.getReconnector().reconnect(false); } }) ...
@SgtSilvio Assuming, there is already an ongoing delayed reconnection started, I guess, this approach may fail?
I just ran into this rather un-expectedly.
I would expect disconnect() to also cancel and stop any pending reconnect task having to do the above seems a little strange to me as a user.
Hei @PMacho Did you find a solution to cancel the ongoing delayed reconnect? You won't be able to manually call connect either since the client thinks it's in the process of reconnecting.
Hei @PMacho
Did you find a solution to cancel the ongoing delayed reconnect?
You won't be able to manually call connect either since the client thinks it's in the process of reconnecting.
Kind of. We wrote a wrapper around this client that imitates the reconnect by rebuilding and reconnecting all pipelines. It still suffers from the fact that occasionally the server refuses to reconnect. However, I can't share the source here. Sorry for that.
Just a hint if you are planning the same. When reconnecting you'll get messages from the server before actually subscribing.
Okay, thanks for the hint :).
@flopfl you're welcome
Hi all - since this issue hasn't had any updates recently I will close it out. Thanks everyone for the input. If anything remains, feel free to file another issue. We'd be happy to help out.