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
855 stars 158 forks source link

How to configure client to resend messages after reconnect #307

Closed chris1705 closed 5 years ago

chris1705 commented 5 years ago

Hello Silvio,

I've just started using your library after implementation of automatic reconnect handling.

In my use case the connection is recreated successfully but messages, which were send while there was no connection to MQTT broker, are not resend. I've already tried different configurations (cleanSession = false/true) but it doesn't work. Do I have to set up a specific flag to use this feature? Can you provide an example?

My current code to setup client looks like this:

final Mqtt3AsyncClient mqttAsyncClient = MqttClient.builder()
                                                    .identifier("java-sample-" + Instant.now().toEpochMilli())
                                                    .serverHost(broker)
                                                    .automaticReconnectWithDefaultConfig()
                                                    .useMqttVersion3()
                                                    .simpleAuth().username(username)
                                                                .password(password.getBytes())
                                                                .applySimpleAuth()
                                                    .buildAsync();
mqttAsyncClient.connectWith().cleanSession(false).send();

Regards Christian

SgtSilvio commented 5 years ago

Hi Christian, You do not have to set a flag for this, but a session is required so cleanSession(false) is right. How are you observing that the messages are not resent? Could you add a listener to your publish calls and log the throwable like so:

client.publishWith().topic("test").send()
        .whenComplete((publish, throwable) -> {
            if (throwable != null) {
                throwable.printStackTrace();
            }
        });

Maybe your broker clears the session. Could you add a ConnectedListener that prints the ConnAck message:

final Mqtt3AsyncClient client = Mqtt3Client.builder()
        ...
        .addConnectedListener(context -> TypeSwitch.when(context)
                .is(Mqtt3ClientConnectedContext.class, context3 -> System.out.println(context3.getConnAck())))
        .buildAsync();
SgtSilvio commented 5 years ago

@chris1705 do you still have this problem. If so, could you provide more information? If not please comment and close this issue.

SgtSilvio commented 5 years ago

I will close this issue now. Feel free to comment and reopen if you still face this issue.

nitin-vavdiya commented 4 years ago

How can we set a clean session flag in Mqtt5AsyncClient I can not find method with cleanSession There is one method cleanStart is it the same?

SgtSilvio commented 4 years ago

@nitinsmartsense Only MQTT 3 has cleanSession. MQTT 5 replaced it with cleanStart + sessionExpiryInterval. You can find more information in this blog post: https://www.hivemq.com/blog/mqtt5-essentials-part4-session-and-message-expiry/

Please open a separate issue in the future or ask questions in our community forum.