hivemq / hivemq-mqtt-client

HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
https://hivemq.github.io/hivemq-mqtt-client/
Apache License 2.0
832 stars 153 forks source link

How to force cancel/interrupt a reconnecting state client? #582

Closed bigcat26 closed 11 months ago

bigcat26 commented 1 year ago

Checklist

❓ Question

How to force cancel/interrupt a reconnecting state client?

📎 Additional context

When I try to disconnect a Mqtt3RxClient that is in the DISCONNECTED_RECONNECT state, it returns a throwable with the message ‘com.hivemq.client.mqtt.exceptions.MqttClientStateException: MQTT client is not connected.’ However, it may still successfully reconnect after a while, which is not what I want. I need to force an interruption of the reconnecting process immediately, such as by closing the underlying socket, but I did not find any API to access the underlying socket. Do you have any suggestions?

Blafasel3 commented 4 months ago

@bigcat26 How did you fix this? We are experiencing the same issue.

pglombardo commented 4 months ago

Would this do it or do you have to specifically interrupt a hanging socket?

.addDisconnectedListener(context -> {
    context.getReconnector()
        .reconnect(false);
})

More examples in this file.

bigcat26 commented 4 months ago

@bigcat26 How did you fix this? We are experiencing the same issue.

I have applied the following changes in MqttDisconnectCompletable.java:

    protected void subscribeActual(final @NotNull CompletableObserver s) {
        final MqttClientConnectionConfig connectionConfig = clientConfig.getRawConnectionConfig();
        if (connectionConfig == null) {
//            EmptyDisposable.error(MqttClientStateExceptions.notConnected(), s);
            clientConfig.getRawState().set(MqttClientState.DISCONNECTED);
            clientConfig.releaseEventLoop();
            EmptyDisposable.complete(s);
            return;
        }

It seems works.