Skip to content

Commit

Permalink
Merge pull request #122 from NitorCreations/registerHikariPoolToMetri…
Browse files Browse the repository at this point in the history
…cRegistry

Register hikari pool to MetricRegistry if MetricRegistry is on classpath
  • Loading branch information
efonsell committed Feb 10, 2015
2 parents 161ce33 + 6547bb0 commit 5662fd4
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.Environment;
Expand All @@ -29,7 +31,7 @@ protected DatabaseConfiguration(String dbType) {

@Bean
@NFlow
public DataSource nflowDatasource(Environment env) {
public DataSource nflowDatasource(Environment env, BeanFactory appCtx) {
String url = property(env, "url");
logger.info("Database connection to {} using {}", dbType, url);
HikariConfig config = new HikariConfig();
Expand All @@ -41,9 +43,22 @@ public DataSource nflowDatasource(Environment env) {
config.setMaximumPoolSize(property(env, "max_pool_size", Integer.class));
config.setIdleTimeout(property(env, "idle_timeout_seconds", Integer.class) * 1000);
config.setAutoCommit(true);
setMetricRegistryIfBeanFoundOnClassPath(config, appCtx);
return new HikariDataSource(config);
}

private void setMetricRegistryIfBeanFoundOnClassPath(HikariConfig config, BeanFactory appCtx) {
try {
Class<?> metricClass = Class.forName("com.codahale.metrics.MetricRegistry");
Object metricRegistry = appCtx.getBean(metricClass);
if (metricRegistry != null) {
config.setMetricRegistry(metricRegistry);
}
} catch (ClassNotFoundException | NoSuchBeanDefinitionException e) {
// ignored - metrics is an optional dependency
}
}

@Bean
@NFlow
@Scope(SCOPE_PROTOTYPE)
Expand Down

0 comments on commit 5662fd4

Please sign in to comment.