Skip to content

Commit

Permalink
Added MQTT Cleanup method
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Jul 8, 2024
1 parent 5a3e84e commit 105f917
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,9 @@ public void unSubscribe(MqttSubscription sub) throws MqttException {
*/
public void unSubscribeAll(String topic) throws MqttException {
mqttSubscriptions.remove(topic);
if (mqttClient == null) {
return;
}
try {
mqttClient.unsubscribe(topic);
} catch (org.eclipse.paho.client.mqttv3.MqttException ex) {
Expand Down Expand Up @@ -696,4 +699,25 @@ private void ensureMqttConfigured() throws MqttException {
}
}

/**
* Unsubscribes all topics and closes the connection.
*/
public void cleanupMqtt() {
mqttSubscriptions.keySet().forEach((topic) -> {
try {
unSubscribeAll(topic);
} catch (MqttException exc) {
LOGGER.warn("error unsubscribing from MQTT", exc);
}
});
if (mqttClient != null) {
try {
mqttClient.close(true);
} catch (org.eclipse.paho.client.mqttv3.MqttException ex) {
LOGGER.warn("error closing MQTT conection");
}
}
mqttClient = null;
}

}

0 comments on commit 105f917

Please sign in to comment.