On disconnection remove element from the iterator to avoid concurrent modification exception
diff --git a/platform/southbound/mqtt/mqtt-device/src/main/java/org/eclipse/sensinact/gateway/sthbnd/mqtt/api/MqttBroker.java b/platform/southbound/mqtt/mqtt-device/src/main/java/org/eclipse/sensinact/gateway/sthbnd/mqtt/api/MqttBroker.java
index 7292c17..f59f710 100644
--- a/platform/southbound/mqtt/mqtt-device/src/main/java/org/eclipse/sensinact/gateway/sthbnd/mqtt/api/MqttBroker.java
+++ b/platform/southbound/mqtt/mqtt-device/src/main/java/org/eclipse/sensinact/gateway/sthbnd/mqtt/api/MqttBroker.java
@@ -168,12 +168,14 @@
}
}
- public void disconnect() throws MqttException {
+ public synchronized void disconnect() throws MqttException {
if (client.isConnected()) {
- for (MqttTopic topic : topics) {
+ for (java.util.Iterator it = topics.iterator(); it.hasNext();) {
+ MqttTopic topic=null;
try {
+ topic=(MqttTopic) it.next();
client.unsubscribe(topic.getTopic());
- topics.remove(topic);
+ it.remove();
LOG.info("Unsubscription to the topic {} done", topic.getTopic());
} catch (MqttException e) {
LOG.error("Unable to unsubscribe from the topic {}", topic.getTopic());