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

Cancel automatic reconnect calling connect or disconnect

Open SgtSilvio opened this issue 6 years ago • 8 comments

SgtSilvio avatar Jun 18 '19 20:06 SgtSilvio

Hey there. Is there any chance to cancel automatic reconnect? Calling disconnect() won't help since it will throw because we are not connected.

degill avatar May 12 '20 12:05 degill

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 avatar May 12 '20 13:05 SgtSilvio

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?

PMacho avatar Jun 01 '21 15:06 PMacho

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.

richturner avatar Oct 21 '21 12:10 richturner

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.

flopfl avatar Dec 30 '21 12:12 flopfl

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.

PMacho avatar Jan 05 '22 16:01 PMacho

Okay, thanks for the hint :).

flopfl avatar Jan 07 '22 10:01 flopfl

@flopfl you're welcome

PMacho avatar Jan 10 '22 16:01 PMacho

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.

pglombardo avatar Mar 07 '23 14:03 pglombardo