Permalink
Browse files

Added Hikari connection pool health status provider

  • Loading branch information...
richturner committed Sep 5, 2018
1 parent 69291b6 commit 7f03f560cf74f9cbac4e8f7a783bb06ef3fd9999
@@ -37,18 +37,18 @@
*/
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() {
@@ -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;
}
}
@@ -0,0 +1 @@
org.openremote.container.persistence.PersistenceHealthStatusProvider

0 comments on commit 7f03f56

Please sign in to comment.