Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

MQTT connection robustness improvements

Rich Turner3de9c695d539
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
index e7eb65860..221c77c75 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttBrokerService.java
@@ -78,7 +78,9 @@ public class MqttBrokerService extends RouteBuilder implements ContainerService,
protected MessageBrokerService messageBrokerService;
protected ScheduledExecutorService executorService;
protected TimerService timerService;
- protected final Map<String, MqttConnection> clientIdConnectionMap = new HashMap<>();
+ // We store connections that get 'pushed' out by a re-connect which can occur before we are notified of the disconnect
+ private final Map<String, MqttConnection> obsoleteConnectionMap = new HashMap<>();
+ private final Map<String, MqttConnection> clientIdConnectionMap = new HashMap<>();
protected List<MQTTHandler> customHandlers = new ArrayList<>();
protected boolean active;
@@ -261,7 +263,16 @@ public class MqttBrokerService extends RouteBuilder implements ContainerService,
}
synchronized (clientIdConnectionMap) {
- MqttConnection connection = clientIdConnectionMap.remove(clientId);
+ MqttConnection connection = obsoleteConnectionMap.remove(clientId);
+
+ if (connection == null) {
+ connection = clientIdConnectionMap.remove(clientId);
+ }
+
+ if (connection != null && LOG.isLoggable(Level.FINEST)) {
+ LOG.finest("Removing connection: " + connection);
+ }
+
if (connection != null && !connection.isCleanSession()) {
try {
SessionRegistry sessionRegistry = getSessionRegistry();
@@ -353,6 +364,14 @@ public class MqttBrokerService extends RouteBuilder implements ContainerService,
public void addConnection(String clientId, MqttConnection connection) {
synchronized (clientIdConnectionMap) {
+ MqttConnection obsoleteConnection = clientIdConnectionMap.remove(clientId);
+ if (obsoleteConnection != null) {
+ obsoleteConnection.setObsolete();
+ obsoleteConnectionMap.put(clientId, obsoleteConnection);
+ }
+ if (LOG.isLoggable(Level.FINEST)) {
+ LOG.finest("Adding connection: " + connection);
+ }
clientIdConnectionMap.put(clientId, connection);
}
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
index e2eebe066..031f01e65 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/MqttConnection.java
@@ -32,6 +32,7 @@ public class MqttConnection {
protected Supplier<String> tokenSupplier;
protected ManagerKeycloakIdentityProvider identityProvider;
protected long connectionTime;
+ protected boolean obsolete;
public MqttConnection(ManagerKeycloakIdentityProvider identityProvider, String clientId, String realm, String username, String password, boolean cleanSession, long connectionTime) {
this.cleanSession = cleanSession;
@@ -118,6 +119,10 @@ public class MqttConnection {
}
}
+ protected void setObsolete() {
+ this.obsolete = true;
+ }
+
@Override
public String toString() {
return this.getClass().getSimpleName() + "{" +
@@ -125,6 +130,7 @@ public class MqttConnection {
", realm='" + realm + '\'' +
", username='" + username + '\'' +
", clientId='" + clientId + '\'' +
+ ", obsolete='" + obsolete + '\'' +
'}';
}
diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java
index 1ee482077..94f89ce3f 100644
--- a/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java
+++ b/manager/src/main/java/org/openremote/manager/mqtt/ORAuthorizatorPolicy.java
@@ -56,13 +56,13 @@ public class ORAuthorizatorPolicy implements IAuthorizatorPolicy {
@SuppressWarnings({"rawtypes", "unchecked"})
protected boolean verifyRights(Topic topic, String clientId, String realm, String username, boolean isWrite) {
- MqttConnection connection = brokerService.clientIdConnectionMap.get(clientId);
+ MqttConnection connection = brokerService.getConnection(clientId);
int i=0;
try {
while (connection == null && i<10) {
// The connection handler gets called after this quite often so try and wait until the connection is registered
- connection = brokerService.clientIdConnectionMap.get(clientId);
+ connection = brokerService.getConnection(clientId);
i++;
Thread.sleep(100);
}
diff --git a/manager/src/main/resources/logging-dev.properties b/manager/src/main/resources/logging-dev.properties
index fd07ce348..c73680609 100644
--- a/manager/src/main/resources/logging-dev.properties
+++ b/manager/src/main/resources/logging-dev.properties
@@ -18,7 +18,7 @@ handlers=java.util.logging.ConsoleHandler, \
###############################################################################
# Set to FINEST to get all enabled log categories on console handler
-java.util.logging.ConsoleHandler.level=FINEST
+java.util.logging.ConsoleHandler.level=FINER
# Console format, you can colorize this in IntelliJ IDEA "Run Application " console
# with the GrepConsole plugin and after copying the configuration file of your
@@ -34,7 +34,7 @@ java.util.logging.ConsoleHandler.formatter=org.openremote.container.util.LogForm
###############################################################################
# Set to FINEST to get all enabled log categories on file handler
-java.util.logging.FileLogger.level=FINEST
+java.util.logging.FileHandler.level=FINER
# Output file and format
java.util.logging.FileHandler.pattern=/deployment/openremote.log
@@ -96,7 +96,10 @@ org.hibernate.cfg.beanvalidation.TypeSafeActivator.level=SEVERE
com.zaxxer.hikari.level=WARNING
javax.management.level=WARNING
org.xnio.level=WARNING
+org.apache.camel.level=WARNING
org.apache.camel.management.level=WARNING
org.apache.camel.impl.converter.DefaultTypeConverter.level=WARNING
org.apache.camel.impl.DefaultShutdownStrategy.level=SEVERE
org.jeasy.rules.level=WARNING
+org.postgresql.jdbc.level=INFO
+io.moquette.broker.level=INFO
diff --git a/manager/src/main/resources/logging.properties b/manager/src/main/resources/logging.properties
index 073de6d4d..0f014f49b 100644
--- a/manager/src/main/resources/logging.properties
+++ b/manager/src/main/resources/logging.properties
@@ -34,7 +34,7 @@ java.util.logging.ConsoleHandler.formatter=org.openremote.container.util.LogForm
###############################################################################
# Set to FINEST to get all enabled log categories on file handler
-java.util.logging.FileLogger.level=FINEST
+java.util.logging.FileHandler.level=INFO
# Output file and format
java.util.logging.FileHandler.pattern=/deployment/openremote.log
@@ -95,8 +95,10 @@ org.hibernate.cfg.beanvalidation.TypeSafeActivator.level=SEVERE
com.zaxxer.hikari.level=WARNING
javax.management.level=WARNING
org.xnio.level=WARNING
+org.apache.camel.level=INFO
org.apache.camel.management.level=WARNING
org.apache.camel.impl.converter.DefaultTypeConverter.level=WARNING
org.apache.camel.impl.DefaultShutdownStrategy.level=SEVERE
org.jeasy.rules.level=WARNING
+org.postgresql.jdbc.level=INFO
io.moquette.broker.level=WARNING