Hello,
I’m trying to set a connection timeout for a client(blocking/async), to put simply, i just want client throw an exception when it can not connect in a certain amount of time. I’ve tried two approaches so far:
Async client
Mqtt5AsyncClient client = MqttClient.builder()
.useMqttVersion5()
.serverHost(hivemqHost)
.serverPort(hivemqPort)
.identifier(clientIdMqtt5)
.automaticReconnect(MqttClientAutoReconnectImpl.DEFAULT)
.transportConfig()
.mqttConnectTimeout(5, TimeUnit.SECONDS)
.socketConnectTimeout(5,TimeUnit.SECONDS)
.applyTransportConfig()
.buildAsync();
log.info("Built mqtt v5 client..");
client.connectWith()
.cleanStart(false)
.noSessionExpiry()
.send()
.whenComplete((connAck, throwable) -> {
if (connAck != null) {
log.info("whenComplete: MQTT connect success with code {}", connAck.getReasonCode());
} else {
if (throwable instanceof Mqtt5ConnAckException) {
log.info("whenComplete: Connect failed because of Negative CONNACK with code {} ",
((Mqtt5ConnAckException) throwable).getMqttMessage().getReasonCode());
} else {
log.info("whenComplete: MQTT connect failed because of {}", throwable.getMessage());
}
}
});
When hivemq broker is not running this whenComplete() callback is never called. I expect it should be called in connect timeout seconds(5s here).
Hi Daria,
Meanwhile I’ve also discovered those connect/disconnect listeners.They are handy.
Also tried the autoReconnect option, which allows frequent checking if the server is back online.
It didn’t trigger whenComplete() callback, i expected it should… Why wasn’t it triggered?
After HiveMQ broker went online, client auto reconnected and sent those messages to broker.
So hivemq-mqtt-client must be buffering those messages locally. Is this correct? If so what is limit of this behaviour(i.e. how much data can be buffered?)
P.S.
And also thank you for pointing out additional example api usages.
True, these two methods aren’t in the MQTT CLI. This means that MQTT CLI is reusing whatever is set in the MQTT Client library, take a look here:
private int socketConnectTimeoutMs = MqttClientTransportConfigImpl.DEFAULT_SOCKET_CONNECT_TIMEOUT_MS;
private int mqttConnectTimeoutMs = MqttClientTransportConfigImpl.DEFAULT_MQTT_CONNECT_TIMEOUT_MS;