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

Mqtt5UserProperty is empty when reconnected. #530

Closed ljwhx2002 closed 1 year ago

ljwhx2002 commented 2 years ago

here is my code:

Mqtt5Client client=Mqtt5Client.builder()
                .identifier("clientId") // use a unique identifier
                .serverHost("127.0.0.1") // use the HiveMQ broker
                .serverPort(1883)
                .automaticReconnectWithDefaultConfig() // the client automatically reconnects
                .build();

        client.toBlocking().connectWith()
                .cleanStart(false)
                .willPublish(null)
                .userProperties(Mqtt5UserProperties.of(Mqtt5UserProperty.of("k1","v1")))
                .sessionExpiryInterval(TimeUnit.HOURS.toSeconds(1)) // buffer messages
                .send();

the server can print Mqtt5UserProperty of k1 v1 well at the first time, but empty when reconnected.

pglombardo commented 1 year ago

Hi @ljwhx2002 - to resend UserProperties you can do so through a DisconnectedListener:

.addDisconnectedListener(context ->
    TypeSwitch.when(context.getReconnector())
        .is(Mqtt5ClientReconnector.class, mqtt5ClientReconnector -> {
             final var builder = mqtt5ClientReconnector.connectWith();
             builder.userProperties(connectProperties);
             builder.applyConnect();
}))

Let me know if this helps!

pglombardo commented 1 year ago

Hi @ljwhx2002 - since I haven't heard back I'll close out this issue. If the issue still exists, please let us know. We'd be happy to help out. Feel free to re-open or file another issue if that's the case.