MqttClient status remain "connecting" after connection timeout.
Closed this issue · 4 comments
Questions
When opening a connection fails, e.g. ConnectionTimeout due to a network issue, the status of the MqttClient does not change to closed. When try to connect again (network is there again at this time), an exception is thrown with the message "Client connecting".
Version
4.0.3
Context
As soon as the MqttClient tries to connect again after first attempt failed, this exception is thrown.
java.lang.IllegalStateException: Client connecting
at io.vertx.mqtt.impl.MqttClientImpl.doConnect(MqttClientImpl.java:220)
at io.vertx.mqtt.impl.MqttClientImpl.connect(MqttClientImpl.java:176)
at io.vertx.mqtt.impl.MqttClientImpl.connect(MqttClientImpl.java:185)
can you give steps to precisely reproduce or a reproducer ?
by connection timeout, do you mean a socket that would connect but the server would not send any data back ? or timeout to establish the socket ?
By connection timeout, I mean a timeout to establish the socket, e.g. when try to connect to a server during network cable is plugged out.
Reproduce
I wrote quick and dirty a test, that simulate that behaviour. The MQTT server is not reachable. The first connection attempt will fail after some time with the message, that the server is not reachable, which is ok. But the second attempt immediately fails with an IllegalStateException and the message "Client connecting". I would expect at second attempt the same error as in first attmept. Now it is only possible, to automatically reconnect a broken connection, when the reconnect is successful at first attempt.
@Test
void mqttClientReconnect() throws InterruptedException {
MqttClientOptions options = new MqttClientOptions().setSsl(true).setTrustAll(true);
LOGGER.info("Start first attempt.");
MqttClient mqttClient = MqttClient.create(Vertx.vertx(), options);
mqttClient.connect(1883, "192.168.100.100", ar -> {
if (ar.succeeded()) {
LOGGER.info("Successfully connected to MQTT broker.");
} else {
LOGGER.error("Unable to connect to MQTT broker. Try again...", ar.cause());
}
});
Thread.sleep(30000);
if(!mqttClient.isConnected()) {
LOGGER.info("Start second attempt.");
mqttClient.connect(1883, "192.168.100.100", ar -> {
if (ar.succeeded()) {
LOGGER.info("Successfully connected to MQTT broker at second attempt.");
} else {
LOGGER.error("Again unable to connect to MQTT broker at second attempt.", ar.cause());
}
});
}
Thread.sleep(5000);
}
thanks