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 set MQTT5 WILL_DELAY_INTERVAL property? #586

Closed andsel closed 12 months ago

andsel commented 12 months ago

Checklist

❓ Question

How I can set the MQTT5 WILL_DELAY_INTERVAL Connect's property?

📎 Additional context

I'm trying to connect with a custom will delay interval. Looking at the code the property in the connect message is encoded: https://github.com/hivemq/hivemq-mqtt-client/blob/338f33877d72dc71d0e9c528c7f80ba15096b32c/src/main/java/com/hivemq/client/internal/mqtt/codec/encoder/mqtt5/Mqtt5ConnectEncoder.java#L306-L316

and the willPublish instance comes from the MqttConnect message: https://github.com/hivemq/hivemq-mqtt-client/blob/338f33877d72dc71d0e9c528c7f80ba15096b32c/src/main/java/com/hivemq/client/internal/mqtt/message/connect/MqttConnect.java#L119-L121

which accepts it as its parameters in the constructor https://github.com/hivemq/hivemq-mqtt-client/blob/338f33877d72dc71d0e9c528c7f80ba15096b32c/src/main/java/com/hivemq/client/internal/mqtt/message/connect/MqttConnect.java#L73

https://github.com/hivemq/hivemq-mqtt-client/blob/338f33877d72dc71d0e9c528c7f80ba15096b32c/src/main/java/com/hivemq/client/internal/mqtt/message/connect/MqttConnectBuilder.java#L135-L138

the willPublish comes from the MqttConnect which is built by the Mqtt5Connect.builder(). That builder provides a willPublish method but the Mqtt5WillPublishBuilder.Nested doesn't provide any method to set the will delay interval property.

Please could you provide a sample, or code snippet to do this?

My sample test code:

final Mqtt5BlockingClient publisher = MqttClient.builder()
            .useMqttVersion5()
            .identifier("simple_client")
            .serverHost("localhost")
            .serverPort(1883)
            .buildBlocking();
Mqtt5Connect connectMessage = Mqtt5Connect.builder()
            .keepAlive(10)
            .willPublish()
            // here I miss the ability
           .build();
Mqtt5ConnAck connectAck = publisher.connect(connectMessage);                       

Thank's a lot :-)

andsel commented 12 months ago

I'm using client version 1.3.1 but same with 1.3.0

SgtSilvio commented 12 months ago

Hi @andsel

The option is only present after you specified the mandatory topic:

Mqtt5Connect connectMessage = Mqtt5Connect.builder()
        .keepAlive(10)
        .willPublish()
            .topic("test")
            .delayInterval(123)
            .applyWillPublish()
        .build();
andsel commented 12 months ago

Thank's @SgtSilvio, I missed that builder changed after topic invocation :+1:

gelbertgel commented 12 months ago

How can it stay connected in the background all the time? After a while, I get a disconnect error.