Hi
I am using Paho(Java client) in auto re-connect mode. I am unable to properly shut down the client when connection to broker is lost.
Client hangs forever and java application does not terminate.
Code snippet -
private void runClient() throws MqttException, InterruptedException {
………… …………. …………. ………….
………… …………. …………. ………….
// Connect to Broker
final IMqttToken connect = gcdClient.connect( connOpt );
connect.waitForCompletion();
Thread.sleep( 10_000 );
// SCENARIO - If connection to broker is lost at this point. May be broker is down or other reason.
// Trying to shutdown the connection
log.info( "Trying to shutdown the client" );
try {
gcdClient.disconnect().waitForCompletion();
} catch ( MqttException ex ) {
log.error( "Exception while disconnecting", ex );
} finally {
log.info( "Close the client" );
gcdClient.close();
}
}
Attaching the java file. Here is the stack trace –
11:50:29.459 [main] INFO TestMqtt - Connecting to MQTT Broker
11:50:29.489 [MQTT Call: clientID123] INFO TestMqtt - connectComplete - MQTT Publisher connected to MQTT Broker : tcp://localhost:1883 reconnect=false
11:50:30.989 [MQTT Rec: clientID123] ERROR TestMqtt - connectionLost - MQTT Publisher disconnected from MQTT Broker
11:50:39.498 [main] INFO TestMqtt - Trying to shutdown the client
11:50:39.498 [main] ERROR TestMqtt - Exception while disconnecting
org.eclipse.paho.client.mqttv3.MqttException: Client is disconnected
at org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:31)
at org.eclipse.paho.client.mqttv3.internal.ClientComms.disconnect(ClientComms.java:451)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.disconnect(MqttAsyncClient.java:632)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.disconnect(MqttAsyncClient.java:601)
at org.eclipse.paho.client.mqttv3.MqttAsyncClient.disconnect(MqttAsyncClient.java:608)
at TestMqtt.runClient(TestMqtt.java:47)
at TestMqtt.main(TestMqtt.java:22)
11:50:39.498 [main] INFO TestMqtt - Close the client