Skip to content

armgit / core

Public Imported from GitHub
0

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

Added Hikari connection pool health status provider

Rich Turner7f03f560cf74
diff --git a/container/src/main/java/org/openremote/container/persistence/Database.java b/container/src/main/java/org/openremote/container/persistence/Database.java
index 2a82a3ccb..b468c6787 100644
--- a/container/src/main/java/org/openremote/container/persistence/Database.java
+++ b/container/src/main/java/org/openremote/container/persistence/Database.java
@@ -37,18 +37,18 @@ public interface Database {
*/
Map<String, Object> createProperties();
- /**
- * Modify persistence properties (e.g. set datasource) used to call <code>Persistence.createEntityManagerFactory(persistenceUnitName, persistenceUnitProperties)</code>
- */
- void open(Map<String, Object> properties, String connectionUrl, String username, String password, int connectionTimeoutSeconds, int minIdle, int maxPoolSize);
+ /**
+ * Modify persistence properties (e.g. set datasource) used to call <code>Persistence.createEntityManagerFactory(persistenceUnitName, persistenceUnitProperties)</code>
+ */
+ void open(Map<String, Object> properties, String connectionUrl, String username, String password, int connectionTimeoutSeconds, int minIdle, int maxPoolSize);
- void close();
+ void close();
- enum Product implements Database {
+ enum Product implements Database {
- POSTGRES {
- protected HikariConfig hikariConfig;
- protected HikariDataSource hikariDataSource;
+ POSTGRES {
+ protected HikariConfig hikariConfig;
+ protected HikariDataSource hikariDataSource;
@Override
public Map<String, Object> createProperties() {
diff --git a/container/src/main/java/org/openremote/container/persistence/PersistenceHealthStatusProvider.java b/container/src/main/java/org/openremote/container/persistence/PersistenceHealthStatusProvider.java
new file mode 100644
index 000000000..0a16da7b7
--- /dev/null
+++ b/container/src/main/java/org/openremote/container/persistence/PersistenceHealthStatusProvider.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2017, OpenRemote Inc.
+ *
+ * See the CONTRIBUTORS.txt file in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.openremote.container.persistence;
+
+import com.zaxxer.hikari.HikariPoolMXBean;
+import org.openremote.container.Container;
+import org.openremote.container.ContainerService;
+import org.openremote.model.system.HealthStatusProvider;
+import org.openremote.model.value.ObjectValue;
+import org.openremote.model.value.Value;
+import org.openremote.model.value.Values;
+
+import javax.management.JMX;
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import java.lang.management.ManagementFactory;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import static org.openremote.container.persistence.Database.PROPERTY_POOL_NAME;
+
+public class PersistenceHealthStatusProvider implements HealthStatusProvider, ContainerService {
+
+ private static final Logger LOG = Logger.getLogger(PersistenceHealthStatusProvider.class.getName());
+ public static final String NAME = "db";
+ public static final String VERSION = "1.0";
+ protected PersistenceService persistenceService;
+
+ @Override
+ public void init(Container container) throws Exception {
+ persistenceService = container.getService(PersistenceService.class);
+ }
+
+ @Override
+ public void start(Container container) throws Exception {
+
+ }
+
+ @Override
+ public void stop(Container container) throws Exception {
+
+ }
+
+ @Override
+ public String getHealthStatusName() {
+ return NAME;
+ }
+
+ @Override
+ public String getHealthStatusVersion() {
+ return VERSION;
+ }
+
+ @Override
+ public Value getHealthStatus() {
+ if (persistenceService.persistenceUnitProperties == null) {
+ return null;
+ }
+
+ String poolNameStr = persistenceService.persistenceUnitProperties.containsKey(PROPERTY_POOL_NAME)
+ ? persistenceService.persistenceUnitProperties.get(PROPERTY_POOL_NAME).toString()
+ : "or-pool";
+
+ try {
+ MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
+ ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (" + poolNameStr + ")");
+ HikariPoolMXBean poolMBean = JMX.newMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class);
+
+ int idleConnections = poolMBean.getIdleConnections();
+ int activeConnections = poolMBean.getActiveConnections();
+ int totalConnections = poolMBean.getTotalConnections();
+ int threadsWaiting = poolMBean.getThreadsAwaitingConnection();
+
+ ObjectValue value = Values.createObject();
+ value.put("idleConnections", idleConnections);
+ value.put("activeConnections", activeConnections);
+ value.put("totalConnections", totalConnections);
+ value.put("threadsWaiting", threadsWaiting);
+
+ return value;
+ } catch (MalformedObjectNameException e) {
+ LOG.log(Level.SEVERE, "Failed to get hikari connection pool status", e);
+ }
+
+ return null;
+ }
+}
diff --git a/container/src/main/resources/META-INF/services/org.openremote.model.system.HealthStatusProvider b/container/src/main/resources/META-INF/services/org.openremote.model.system.HealthStatusProvider
new file mode 100644
index 000000000..f4315127e
--- /dev/null
+++ b/container/src/main/resources/META-INF/services/org.openremote.model.system.HealthStatusProvider
@@ -0,0 +1 @@
+org.openremote.container.persistence.PersistenceHealthStatusProvider
\ No newline at end of file