hivemq/hivemq-mqtt-client

How to set MQTT5 WILL_DELAY_INTERVAL property?

andsel opened this issue ยท 4 comments

andsel commented

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:

private void encodeWillPublish(
final @NotNull MqttConnect message, final @NotNull ByteBuf out, final int willPropertyLength) {
final MqttWillPublish willPublish = message.getRawWillPublish();
if (willPublish != null) {
MqttVariableByteInteger.encode(willPropertyLength, out);
publishEncoder.encodeFixedProperties(willPublish, out);
willPublish.getUserProperties().encode(out);
encodeIntProperty(MqttWillPublishProperty.WILL_DELAY_INTERVAL, willPublish.getDelayInterval(),
MqttWillPublish.DEFAULT_DELAY_INTERVAL, out);

and the willPublish instance comes from the MqttConnect message:

public @Nullable MqttWillPublish getRawWillPublish() {
return willPublish;
}

which accepts it as its parameters in the constructor

public @NotNull MqttConnect build() {
return new MqttConnect(keepAlive, cleanStart, sessionExpiryInterval, restrictions, simpleAuth,
enhancedAuthMechanism, willPublish, userProperties);
}

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

I'm using client version 1.3.1 but same with 1.3.0

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

Thank's @SgtSilvio, I missed that builder changed after topic invocation ๐Ÿ‘

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