I have a java application which has below properties:
- Wrapped as a window service by Apache Commons Deamon
- Has a MQTT client listening message from broker
This my MQTT client code. The setAutomaticReconnect option is set to true
public boolean start() {
logger.info(String.format("MQTT client [%s] is connecting...", clientId));
if (isRunning)
return true;
isRunning = true;
try {
client = new MqttAsyncClient("tcp://" + host + ":" + port, clientId, new MemoryPersistence());
client.setCallback(new MqttSubscriber.Callback());
// Set options
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName(username);
connOpts.setPassword(password.toCharArray());
connOpts.setAutomaticReconnect(true);
connOpts.setMaxReconnectDelay(maxReconnectInterval);
// Connect
IMqttToken connectToken = client.connect(connOpts);
connectToken.waitForCompletion(maxConnectTimeOut);
return true;
} catch (MqttException ex) {
logger.error("MQTT client connected failed", ex);
closeClient();
isRunning = false;
return false;
}
}
With the above code, the app is working fine, and the MQTT client always reconnects if the connection is closed or disconnected. However, sometimes, after when machine slept (about 30 minutes), and this issue occurs on certain machines, the connection does not automatically reconnect. As a result, I have to restart my app to re-establish the connection. This app is designed to run for a long time without any issues, so this problem is quite annoying for the user.
Can someone help me identify what's wrong with my app?