vert-x3 / vertx-mqtt

Vert.x MQTT
Apache License 2.0
184 stars 86 forks source link

Create an MQTTClientOption from a JsonObject #240

Closed polperez closed 1 year ago

polperez commented 1 year ago

Hello All I create a new MqttClientOption from a JsonObject

public static void main (String args[]) {
        try {
            MqttClientOptions options02 = new MqttClientOptions (new MqttClientOptions ().toJson()) ;             
        } catch (Exception ex) { 
            ex.printStackTrace();
        }        
} 

Then I get an exception.

    at io.vertx.mqtt.MqttClientOptions.setKeepAliveInterval(MqttClientOptions.java:316)
    at io.vertx.mqtt.MqttClientOptions.setIdleTimeout(MqttClientOptions.java:472)
    at io.vertx.mqtt.MqttClientOptions.setIdleTimeout(MqttClientOptions.java:33)
    at io.vertx.core.net.TCPSSLOptionsConverter.fromJson(TCPSSLOptionsConverter.java:59)
    at io.vertx.core.net.TCPSSLOptions.<init>(TCPSSLOptions.java:172)
    at io.vertx.core.net.ClientOptionsBase.<init>(ClientOptionsBase.java:82)
    at io.vertx.core.net.NetClientOptions.<init>(NetClientOptions.java:96)
    at io.vertx.mqtt.MqttClientOptions.<init>(MqttClientOptions.java:94)
    at io.ourexchanges.testCluster.TestMQClientConfig.main(TestMQClientConfig.java:42)

The issue comes with version 4.4.4 in the MqttClientOptions class in the method public MqttClientOptions setKeepAliveInterval(int keepAliveInterval) when it is called by the deprecated method public MqttClientOptions setIdleTimeout(int idleTimeout) Which is called by the ancestor of MqttClientOptions.

The constructor new MqttClientOptions (JsonObject) invokes the method setKeepAliveInterval (int) with a value = 0. This bug blocks me to create MQTT Client configuration from predefined Json configuration.

In my workaround, I check the value of the int parameter. If it is less than one, I replace it with MqttClientOptions.DEFAULT_KEEP_ALIVE_INTERVAL;

public MqttClientOptions setKeepAliveInterval(int keepAliveInterval) {
        if (keepAliveInterval < 1) {
            this.keepAliveInterval = MqttClientOptions.DEFAULT_KEEP_ALIVE_INTERVAL;
            // throw new IllegalArgumentException("Invalid keep alive interval " + keepAliveInterval);
        } else {
            this.keepAliveInterval = keepAliveInterval;
        }
        return this;
    }

Question: Is there a reason for that behaviour or it is a bug?

Thank you for your answer Paul

Windows 10 with Java: 20.0.1; OpenJDK 64-Bit Server VM 20.0.1+9

vietj commented 1 year ago

that's an issue fixed in master and 4.x

polperez commented 1 year ago

Thank you for this prompt fix. regards Paul