DataSourceMetadata -> DataSourcePoolMetadata

Rename DataSourceMetadata to DataSourcePoolMetadata
pull/1487/head
Phillip Webb 10 years ago
parent e17769fc2f
commit e56be6cf3d

@ -24,8 +24,8 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceMetadataProvider; import org.springframework.boot.autoconfigure.jdbc.DataSourcePoolMetadataProvider;
import org.springframework.boot.autoconfigure.jdbc.DataSourceMetadataProvidersConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourcePoolMetadataProvidersConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
@ -38,11 +38,11 @@ import org.springframework.context.annotation.Import;
*/ */
@ConditionalOnBean(DataSource.class) @ConditionalOnBean(DataSource.class)
@AutoConfigureAfter(DataSourceAutoConfiguration.class) @AutoConfigureAfter(DataSourceAutoConfiguration.class)
@Import(DataSourceMetadataProvidersConfiguration.class) @Import(DataSourcePoolMetadataProvidersConfiguration.class)
public class DataSourceMetricsAutoConfiguration { public class DataSourceMetricsAutoConfiguration {
@Bean @Bean
@ConditionalOnBean(DataSourceMetadataProvider.class) @ConditionalOnBean(DataSourcePoolMetadataProvider.class)
@ConditionalOnMissingBean @ConditionalOnMissingBean
public DataSourcePublicMetrics dataSourcePublicMetrics() { public DataSourcePublicMetrics dataSourcePublicMetrics() {
return new DataSourcePublicMetrics(); return new DataSourcePublicMetrics();

@ -27,6 +27,7 @@ import org.apache.solr.client.solrj.SolrServer;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
import org.springframework.boot.actuate.health.CompositeHealthIndicator; import org.springframework.boot.actuate.health.CompositeHealthIndicator;
import org.springframework.boot.actuate.health.DataSourceHealthIndicator; import org.springframework.boot.actuate.health.DataSourceHealthIndicator;
import org.springframework.boot.actuate.health.HealthAggregator; import org.springframework.boot.actuate.health.HealthAggregator;
@ -36,7 +37,6 @@ import org.springframework.boot.actuate.health.OrderedHealthAggregator;
import org.springframework.boot.actuate.health.RabbitHealthIndicator; import org.springframework.boot.actuate.health.RabbitHealthIndicator;
import org.springframework.boot.actuate.health.RedisHealthIndicator; import org.springframework.boot.actuate.health.RedisHealthIndicator;
import org.springframework.boot.actuate.health.SolrHealthIndicator; import org.springframework.boot.actuate.health.SolrHealthIndicator;
import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -44,10 +44,10 @@ import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.CompositeDataSourceMetadataProvider; import org.springframework.boot.autoconfigure.jdbc.CompositeDataSourcePoolMetadataProvider;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceMetadata; import org.springframework.boot.autoconfigure.jdbc.DataSourcePoolMetadataProvider;
import org.springframework.boot.autoconfigure.jdbc.DataSourceMetadataProvider; import org.springframework.boot.autoconfigure.jdbc.DataSourcePoolMetadata;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration; import org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration;
@ -103,33 +103,36 @@ public class HealthIndicatorAutoConfiguration {
private Map<String, DataSource> dataSources; private Map<String, DataSource> dataSources;
@Autowired(required = false) @Autowired(required = false)
private Collection<DataSourceMetadataProvider> metadataProviders = Collections.emptyList(); private Collection<DataSourcePoolMetadataProvider> metadataProviders = Collections
.emptyList();
@Bean @Bean
@ConditionalOnMissingBean(name = "dbHealthIndicator") @ConditionalOnMissingBean(name = "dbHealthIndicator")
public HealthIndicator dbHealthIndicator() { public HealthIndicator dbHealthIndicator() {
DataSourceMetadataProvider metadataProvider = DataSourcePoolMetadataProvider metadataProvider = new CompositeDataSourcePoolMetadataProvider(
new CompositeDataSourceMetadataProvider(this.metadataProviders); this.metadataProviders);
if (this.dataSources.size() == 1) { if (this.dataSources.size() == 1) {
return createDataSourceHealthIndicator(metadataProvider, return createDataSourceHealthIndicator(metadataProvider, this.dataSources
this.dataSources.values().iterator().next()); .values().iterator().next());
} }
CompositeHealthIndicator composite = new CompositeHealthIndicator( CompositeHealthIndicator composite = new CompositeHealthIndicator(
this.healthAggregator); this.healthAggregator);
for (Map.Entry<String, DataSource> entry : this.dataSources.entrySet()) { for (Map.Entry<String, DataSource> entry : this.dataSources.entrySet()) {
composite.addHealthIndicator(entry.getKey(), composite.addHealthIndicator(
createDataSourceHealthIndicator(metadataProvider, entry.getValue())); entry.getKey(),
createDataSourceHealthIndicator(metadataProvider,
entry.getValue()));
} }
return composite; return composite;
} }
private DataSourceHealthIndicator createDataSourceHealthIndicator(DataSourceMetadataProvider provider, private DataSourceHealthIndicator createDataSourceHealthIndicator(
DataSource dataSource) { DataSourcePoolMetadataProvider provider, DataSource dataSource) {
String validationQuery = null; String validationQuery = null;
DataSourceMetadata dataSourceMetadata = provider.getDataSourceMetadata(dataSource); DataSourcePoolMetadata poolMetadata = provider
if (dataSourceMetadata != null) { .getDataSourcePoolMetadata(dataSource);
validationQuery = dataSourceMetadata.getValidationQuery(); if (poolMetadata != null) {
validationQuery = poolMetadata.getValidationQuery();
} }
return new DataSourceHealthIndicator(dataSource, validationQuery); return new DataSourceHealthIndicator(dataSource, validationQuery);
} }

@ -28,9 +28,9 @@ import javax.sql.DataSource;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.Metric; import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.autoconfigure.jdbc.CompositeDataSourceMetadataProvider; import org.springframework.boot.autoconfigure.jdbc.CompositeDataSourcePoolMetadataProvider;
import org.springframework.boot.autoconfigure.jdbc.DataSourceMetadata; import org.springframework.boot.autoconfigure.jdbc.DataSourcePoolMetadata;
import org.springframework.boot.autoconfigure.jdbc.DataSourceMetadataProvider; import org.springframework.boot.autoconfigure.jdbc.DataSourcePoolMetadataProvider;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
@ -48,23 +48,23 @@ public class DataSourcePublicMetrics implements PublicMetrics {
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@Autowired @Autowired
private Collection<DataSourceMetadataProvider> providers; private Collection<DataSourcePoolMetadataProvider> providers;
private final Map<String, DataSourceMetadata> metadataByPrefix = new HashMap<String, DataSourceMetadata>(); private final Map<String, DataSourcePoolMetadata> metadataByPrefix = new HashMap<String, DataSourcePoolMetadata>();
@PostConstruct @PostConstruct
public void initialize() { public void initialize() {
DataSource primaryDataSource = getPrimaryDataSource(); DataSource primaryDataSource = getPrimaryDataSource();
DataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider( DataSourcePoolMetadataProvider provider = new CompositeDataSourcePoolMetadataProvider(
this.providers); this.providers);
for (Map.Entry<String, DataSource> entry : this.applicationContext for (Map.Entry<String, DataSource> entry : this.applicationContext
.getBeansOfType(DataSource.class).entrySet()) { .getBeansOfType(DataSource.class).entrySet()) {
String beanName = entry.getKey(); String beanName = entry.getKey();
DataSource bean = entry.getValue(); DataSource bean = entry.getValue();
String prefix = createPrefix(beanName, bean, bean.equals(primaryDataSource)); String prefix = createPrefix(beanName, bean, bean.equals(primaryDataSource));
DataSourceMetadata dataSourceMetadata = provider.getDataSourceMetadata(bean); DataSourcePoolMetadata poolMetadata = provider.getDataSourcePoolMetadata(bean);
if (dataSourceMetadata != null) { if (poolMetadata != null) {
this.metadataByPrefix.put(prefix, dataSourceMetadata); this.metadataByPrefix.put(prefix, poolMetadata);
} }
} }
} }
@ -72,13 +72,13 @@ public class DataSourcePublicMetrics implements PublicMetrics {
@Override @Override
public Collection<Metric<?>> metrics() { public Collection<Metric<?>> metrics() {
Set<Metric<?>> metrics = new LinkedHashSet<Metric<?>>(); Set<Metric<?>> metrics = new LinkedHashSet<Metric<?>>();
for (Map.Entry<String, DataSourceMetadata> entry : this.metadataByPrefix for (Map.Entry<String, DataSourcePoolMetadata> entry : this.metadataByPrefix
.entrySet()) { .entrySet()) {
String prefix = entry.getKey(); String prefix = entry.getKey();
prefix = (prefix.endsWith(".") ? prefix : prefix + "."); prefix = (prefix.endsWith(".") ? prefix : prefix + ".");
DataSourceMetadata dataSourceMetadata = entry.getValue(); DataSourcePoolMetadata dataSourceMetadata = entry.getValue();
addMetric(metrics, prefix + "active", dataSourceMetadata.getPoolSize()); addMetric(metrics, prefix + "active", dataSourceMetadata.getActive());
addMetric(metrics, prefix + "usage", dataSourceMetadata.getPoolUsage()); addMetric(metrics, prefix + "usage", dataSourceMetadata.getUsage());
} }
return metrics; return metrics;
} }

@ -33,7 +33,7 @@ import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration; import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.jdbc.DataSourceMetadataProvidersConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourcePoolMetadataProvidersConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
@ -167,7 +167,7 @@ public class HealthIndicatorAutoConfigurationTests {
public void dataSourceHealthIndicatorWithCustomValidationQuery() { public void dataSourceHealthIndicatorWithCustomValidationQuery() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(PropertyPlaceholderAutoConfiguration.class, DataSourceProperties.class, this.context.register(PropertyPlaceholderAutoConfiguration.class, DataSourceProperties.class,
DataSourceConfig.class, DataSourceMetadataProvidersConfiguration.class, DataSourceConfig.class, DataSourcePoolMetadataProvidersConfiguration.class,
HealthIndicatorAutoConfiguration.class); HealthIndicatorAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.validation-query:SELECT from FOOBAR"); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.validation-query:SELECT from FOOBAR");
this.context.refresh(); this.context.refresh();

@ -19,27 +19,27 @@ package org.springframework.boot.autoconfigure.jdbc;
import javax.sql.DataSource; import javax.sql.DataSource;
/** /**
* A base {@link DataSourceMetadata} implementation. * A base {@link DataSourcePoolMetadata} implementation.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
*/ */
public abstract class AbstractDataSourceMetadata<T extends DataSource> implements public abstract class AbstractDataSourcePoolMetadata<T extends DataSource> implements
DataSourceMetadata { DataSourcePoolMetadata {
private final T dataSource; private final T dataSource;
/** /**
* Create an instance with the data source to use. * Create an instance with the data source to use.
*/ */
protected AbstractDataSourceMetadata(T dataSource) { protected AbstractDataSourcePoolMetadata(T dataSource) {
this.dataSource = dataSource; this.dataSource = dataSource;
} }
@Override @Override
public Float getPoolUsage() { public Float getUsage() {
Integer maxSize = getMaxPoolSize(); Integer maxSize = getMax();
Integer currentSize = getPoolSize(); Integer currentSize = getActive();
if (maxSize == null || currentSize == null) { if (maxSize == null || currentSize == null) {
return null; return null;
} }

@ -21,30 +21,30 @@ import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSource;
/** /**
* {@link DataSourceMetadata} for a Apache Commons DBCP {@link DataSource}. * {@link DataSourcePoolMetadata} for a Apache Commons DBCP {@link DataSource}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
*/ */
public class CommonsDbcpDataSourceMetadata extends public class CommonsDbcpDataSourcePoolMetadata extends
AbstractDataSourceMetadata<BasicDataSource> { AbstractDataSourcePoolMetadata<BasicDataSource> {
public CommonsDbcpDataSourceMetadata(BasicDataSource dataSource) { public CommonsDbcpDataSourcePoolMetadata(BasicDataSource dataSource) {
super(dataSource); super(dataSource);
} }
@Override @Override
public Integer getPoolSize() { public Integer getActive() {
return getDataSource().getNumActive(); return getDataSource().getNumActive();
} }
@Override @Override
public Integer getMaxPoolSize() { public Integer getMax() {
return getDataSource().getMaxActive(); return getDataSource().getMaxActive();
} }
@Override @Override
public Integer getMinPoolSize() { public Integer getMin() {
return getDataSource().getMinIdle(); return getDataSource().getMinIdle();
} }

@ -1,70 +0,0 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.jdbc;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.sql.DataSource;
/**
* A {@link DataSourceMetadataProvider} implementation that returns the first
* {@link DataSourceMetadata} that is found by one of its delegate.
*
* @author Stephane Nicoll
* @since 1.2.0
*/
public class CompositeDataSourceMetadataProvider implements DataSourceMetadataProvider {
private final List<DataSourceMetadataProvider> providers;
/**
* Create a {@link CompositeDataSourceMetadataProvider} instance with no delegate.
*/
public CompositeDataSourceMetadataProvider() {
this(new ArrayList<DataSourceMetadataProvider>());
}
/**
* Create a {@link CompositeDataSourceMetadataProvider} instance with an initial
* collection of delegates to use.
*/
public CompositeDataSourceMetadataProvider(
Collection<? extends DataSourceMetadataProvider> providers) {
this.providers = new ArrayList<DataSourceMetadataProvider>(providers);
}
@Override
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) {
for (DataSourceMetadataProvider provider : this.providers) {
DataSourceMetadata metadata = provider.getDataSourceMetadata(dataSource);
if (metadata != null) {
return metadata;
}
}
return null;
}
/**
* Add a {@link DataSourceMetadataProvider} delegate to the list.
*/
public void addDataSourceMetadataProvider(DataSourceMetadataProvider provider) {
this.providers.add(provider);
}
}

@ -0,0 +1,70 @@
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.jdbc;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.sql.DataSource;
/**
* A {@link DataSourcePoolMetadataProvider} implementation that returns the first
* {@link DataSourcePoolMetadata} that is found by one of its delegate.
*
* @author Stephane Nicoll
* @since 1.2.0
*/
public class CompositeDataSourcePoolMetadataProvider implements DataSourcePoolMetadataProvider {
private final List<DataSourcePoolMetadataProvider> providers;
/**
* Create a {@link CompositeDataSourcePoolMetadataProvider} instance with no delegate.
*/
public CompositeDataSourcePoolMetadataProvider() {
this(new ArrayList<DataSourcePoolMetadataProvider>());
}
/**
* Create a {@link CompositeDataSourcePoolMetadataProvider} instance with an initial
* collection of delegates to use.
*/
public CompositeDataSourcePoolMetadataProvider(
Collection<? extends DataSourcePoolMetadataProvider> providers) {
this.providers = new ArrayList<DataSourcePoolMetadataProvider>(providers);
}
@Override
public DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource) {
for (DataSourcePoolMetadataProvider provider : this.providers) {
DataSourcePoolMetadata metadata = provider.getDataSourcePoolMetadata(dataSource);
if (metadata != null) {
return metadata;
}
}
return null;
}
/**
* Add a {@link DataSourcePoolMetadataProvider} delegate to the list.
*/
public void addDataSourceMetadataProvider(DataSourcePoolMetadataProvider provider) {
this.providers.add(provider);
}
}

@ -19,13 +19,13 @@ package org.springframework.boot.autoconfigure.jdbc;
import javax.sql.DataSource; import javax.sql.DataSource;
/** /**
* Provides access meta-data that is commonly available from most {@link DataSource} * Provides access meta-data that is commonly available from most polled
* implementations. * {@link DataSource} implementations.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
*/ */
public interface DataSourceMetadata { public interface DataSourcePoolMetadata {
/** /**
* Return the usage of the pool as value between 0 and 1 (or -1 if the pool is not * Return the usage of the pool as value between 0 and 1 (or -1 if the pool is not
@ -38,26 +38,26 @@ public interface DataSourceMetadata {
* This may also return {@code null} if the data source does not provide the necessary * This may also return {@code null} if the data source does not provide the necessary
* information to compute the poll usage. * information to compute the poll usage.
*/ */
Float getPoolUsage(); Float getUsage();
/** /**
* Return the current number of active connections that have been allocated from the * Return the current number of active connections that have been allocated from the
* data source or {@code null} if that information is not available. * data source or {@code null} if that information is not available.
*/ */
Integer getPoolSize(); Integer getActive();
/** /**
* Return the maximum number of active connections that can be allocated at the same * Return the maximum number of active connections that can be allocated at the same
* time or {@code -1} if there is no limit. Can also return {@code null} if that * time or {@code -1} if there is no limit. Can also return {@code null} if that
* information is not available. * information is not available.
*/ */
Integer getMaxPoolSize(); Integer getMax();
/** /**
* Return the minimum number of idle connections in the pool or {@code null} if that * Return the minimum number of idle connections in the pool or {@code null} if that
* information is not available. * information is not available.
*/ */
Integer getMinPoolSize(); Integer getMin();
/** /**
* Return the query to use to validate that a connection is valid or {@code null} if * Return the query to use to validate that a connection is valid or {@code null} if

@ -19,17 +19,17 @@ package org.springframework.boot.autoconfigure.jdbc;
import javax.sql.DataSource; import javax.sql.DataSource;
/** /**
* Provide a {@link DataSourceMetadata} based on a {@link DataSource}. * Provide a {@link DataSourcePoolMetadata} based on a {@link DataSource}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
*/ */
public interface DataSourceMetadataProvider { public interface DataSourcePoolMetadataProvider {
/** /**
* Return the {@link DataSourceMetadata} instance able to manage the specified * Return the {@link DataSourcePoolMetadata} instance able to manage the specified
* {@link DataSource} or {@code null} if the given data source could not be handled. * {@link DataSource} or {@code null} if the given data source could not be handled.
*/ */
DataSourceMetadata getDataSourceMetadata(DataSource dataSource); DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource);
} }

@ -26,26 +26,27 @@ import org.springframework.context.annotation.Configuration;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
/** /**
* Register the {@link DataSourceMetadataProvider} instances for the supported data * Register the {@link DataSourcePoolMetadataProvider} instances for the supported data
* sources. * sources.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
*/ */
@Configuration @Configuration
public class DataSourceMetadataProvidersConfiguration { public class DataSourcePoolMetadataProvidersConfiguration {
@Configuration @Configuration
@ConditionalOnClass(org.apache.tomcat.jdbc.pool.DataSource.class) @ConditionalOnClass(org.apache.tomcat.jdbc.pool.DataSource.class)
static class TomcatDataSourceProviderConfiguration { static class TomcatDataSourcePoolMetadataProviderConfiguration {
@Bean @Bean
public DataSourceMetadataProvider tomcatDataSourceProvider() { public DataSourcePoolMetadataProvider tomcatPoolDataSourceMetadataProvider() {
return new DataSourceMetadataProvider() { return new DataSourcePoolMetadataProvider() {
@Override @Override
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) { public DataSourcePoolMetadata getDataSourcePoolMetadata(
DataSource dataSource) {
if (dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource) { if (dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource) {
return new TomcatDataSourceMetadata( return new TomcatDataSourcePoolMetadata(
(org.apache.tomcat.jdbc.pool.DataSource) dataSource); (org.apache.tomcat.jdbc.pool.DataSource) dataSource);
} }
return null; return null;
@ -57,15 +58,17 @@ public class DataSourceMetadataProvidersConfiguration {
@Configuration @Configuration
@ConditionalOnClass(HikariDataSource.class) @ConditionalOnClass(HikariDataSource.class)
static class HikariDataSourceProviderConfiguration { static class HikariPoolDataSourceMetadataProviderConfiguration {
@Bean @Bean
public DataSourceMetadataProvider hikariDataSourceProvider() { public DataSourcePoolMetadataProvider hikariPoolDataSourceMetadataProvider() {
return new DataSourceMetadataProvider() { return new DataSourcePoolMetadataProvider() {
@Override @Override
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) { public DataSourcePoolMetadata getDataSourcePoolMetadata(
DataSource dataSource) {
if (dataSource instanceof HikariDataSource) { if (dataSource instanceof HikariDataSource) {
return new HikariDataSourceMetadata((HikariDataSource) dataSource); return new HikariDataSourcePoolMetadata(
(HikariDataSource) dataSource);
} }
return null; return null;
} }
@ -76,15 +79,16 @@ public class DataSourceMetadataProvidersConfiguration {
@Configuration @Configuration
@ConditionalOnClass(BasicDataSource.class) @ConditionalOnClass(BasicDataSource.class)
static class CommonsDbcpDataSourceProviderConfiguration { static class CommonsDbcpPoolDataSourceMetadataProviderConfiguration {
@Bean @Bean
public DataSourceMetadataProvider commonsDbcpDataSourceProvider() { public DataSourcePoolMetadataProvider commonsDbcpPoolDataSourceMetadataProvider() {
return new DataSourceMetadataProvider() { return new DataSourcePoolMetadataProvider() {
@Override @Override
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) { public DataSourcePoolMetadata getDataSourcePoolMetadata(
DataSource dataSource) {
if (dataSource instanceof BasicDataSource) { if (dataSource instanceof BasicDataSource) {
return new CommonsDbcpDataSourceMetadata( return new CommonsDbcpDataSourcePoolMetadata(
(BasicDataSource) dataSource); (BasicDataSource) dataSource);
} }
return null; return null;

@ -24,20 +24,20 @@ import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.HikariPool; import com.zaxxer.hikari.pool.HikariPool;
/** /**
* {@link DataSourceMetadata} for a Hikari {@link DataSource}. * {@link DataSourcePoolMetadata} for a Hikari {@link DataSource}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
*/ */
public class HikariDataSourceMetadata extends public class HikariDataSourcePoolMetadata extends
AbstractDataSourceMetadata<HikariDataSource> { AbstractDataSourcePoolMetadata<HikariDataSource> {
public HikariDataSourceMetadata(HikariDataSource dataSource) { public HikariDataSourcePoolMetadata(HikariDataSource dataSource) {
super(dataSource); super(dataSource);
} }
@Override @Override
public Integer getPoolSize() { public Integer getActive() {
try { try {
return getHikariPool().getActiveConnections(); return getHikariPool().getActiveConnections();
} }
@ -52,12 +52,12 @@ public class HikariDataSourceMetadata extends
} }
@Override @Override
public Integer getMaxPoolSize() { public Integer getMax() {
return getDataSource().getMaximumPoolSize(); return getDataSource().getMaximumPoolSize();
} }
@Override @Override
public Integer getMinPoolSize() { public Integer getMin() {
return getDataSource().getMinimumIdle(); return getDataSource().getMinimumIdle();
} }

@ -20,29 +20,29 @@ import org.apache.tomcat.jdbc.pool.ConnectionPool;
import org.apache.tomcat.jdbc.pool.DataSource; import org.apache.tomcat.jdbc.pool.DataSource;
/** /**
* {@link DataSourceMetadata} for a Tomcat {@link DataSource}. * {@link DataSourcePoolMetadata} for a Tomcat {@link DataSource}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class TomcatDataSourceMetadata extends AbstractDataSourceMetadata<DataSource> { public class TomcatDataSourcePoolMetadata extends AbstractDataSourcePoolMetadata<DataSource> {
public TomcatDataSourceMetadata(DataSource dataSource) { public TomcatDataSourcePoolMetadata(DataSource dataSource) {
super(dataSource); super(dataSource);
} }
@Override @Override
public Integer getPoolSize() { public Integer getActive() {
ConnectionPool pool = getDataSource().getPool(); ConnectionPool pool = getDataSource().getPool();
return (pool == null ? 0 : pool.getActive()); return (pool == null ? 0 : pool.getActive());
} }
@Override @Override
public Integer getMaxPoolSize() { public Integer getMax() {
return getDataSource().getMaxActive(); return getDataSource().getMaxActive();
} }
@Override @Override
public Integer getMinPoolSize() { public Integer getMin() {
return getDataSource().getMinIdle(); return getDataSource().getMinIdle();
} }

@ -27,11 +27,11 @@ import org.springframework.jdbc.core.JdbcTemplate;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/** /**
* Abstract base class for {@link DataSourceMetadata} tests. * Abstract base class for {@link DataSourcePoolMetadata} tests.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSourceMetadata<?>> { public abstract class AbstractDataSourcePoolMetadataTests<D extends AbstractDataSourcePoolMetadata<?>> {
/** /**
* Return a data source metadata instance with a min size of 0 and max size of 2. * Return a data source metadata instance with a min size of 0 and max size of 2.
@ -40,12 +40,12 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
@Test @Test
public void getMaxPoolSize() { public void getMaxPoolSize() {
assertEquals(Integer.valueOf(2), getDataSourceMetadata().getMaxPoolSize()); assertEquals(Integer.valueOf(2), getDataSourceMetadata().getMax());
} }
@Test @Test
public void getMinPoolSize() { public void getMinPoolSize() {
assertEquals(Integer.valueOf(0), getDataSourceMetadata().getMinPoolSize()); assertEquals(Integer.valueOf(0), getDataSourceMetadata().getMin());
} }
@Test @Test
@ -60,8 +60,8 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
return null; return null;
} }
}); });
assertEquals(Integer.valueOf(0), getDataSourceMetadata().getPoolSize()); assertEquals(Integer.valueOf(0), getDataSourceMetadata().getActive());
assertEquals(Float.valueOf(0), getDataSourceMetadata().getPoolUsage()); assertEquals(Float.valueOf(0), getDataSourceMetadata().getUsage());
} }
@Test @Test
@ -72,8 +72,8 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
@Override @Override
public Void doInConnection(Connection connection) throws SQLException, public Void doInConnection(Connection connection) throws SQLException,
DataAccessException { DataAccessException {
assertEquals(Integer.valueOf(1), getDataSourceMetadata().getPoolSize()); assertEquals(Integer.valueOf(1), getDataSourceMetadata().getActive());
assertEquals(Float.valueOf(0.5F), getDataSourceMetadata().getPoolUsage()); assertEquals(Float.valueOf(0.5F), getDataSourceMetadata().getUsage());
return null; return null;
} }
}); });
@ -92,9 +92,9 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
public Void doInConnection(Connection connection) public Void doInConnection(Connection connection)
throws SQLException, DataAccessException { throws SQLException, DataAccessException {
assertEquals(Integer.valueOf(2), getDataSourceMetadata() assertEquals(Integer.valueOf(2), getDataSourceMetadata()
.getPoolSize()); .getActive());
assertEquals(Float.valueOf(1F), getDataSourceMetadata() assertEquals(Float.valueOf(1F), getDataSourceMetadata()
.getPoolUsage()); .getUsage());
return null; return null;
} }
}); });

@ -24,14 +24,14 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
/** /**
* Tests for {@link CommonsDbcpDataSourceMetadata}. * Tests for {@link CommonsDbcpDataSourcePoolMetadata}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class CommonsDbcpDataSourceMetadataTests extends public class CommonsDbcpDataSourcePoolMetadataTests extends
AbstractDataSourceMetadataTests<CommonsDbcpDataSourceMetadata> { AbstractDataSourcePoolMetadataTests<CommonsDbcpDataSourcePoolMetadata> {
private CommonsDbcpDataSourceMetadata dataSourceMetadata; private CommonsDbcpDataSourcePoolMetadata dataSourceMetadata;
@Before @Before
public void setup() { public void setup() {
@ -39,38 +39,38 @@ public class CommonsDbcpDataSourceMetadataTests extends
} }
@Override @Override
protected CommonsDbcpDataSourceMetadata getDataSourceMetadata() { protected CommonsDbcpDataSourcePoolMetadata getDataSourceMetadata() {
return this.dataSourceMetadata; return this.dataSourceMetadata;
} }
@Test @Test
public void getPoolUsageWithNoCurrent() { public void getPoolUsageWithNoCurrent() {
CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata( CommonsDbcpDataSourcePoolMetadata dsm = new CommonsDbcpDataSourcePoolMetadata(
createDataSource()) { createDataSource()) {
@Override @Override
public Integer getPoolSize() { public Integer getActive() {
return null; return null;
} }
}; };
assertNull(dsm.getPoolUsage()); assertNull(dsm.getUsage());
} }
@Test @Test
public void getPoolUsageWithNoMax() { public void getPoolUsageWithNoMax() {
CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata( CommonsDbcpDataSourcePoolMetadata dsm = new CommonsDbcpDataSourcePoolMetadata(
createDataSource()) { createDataSource()) {
@Override @Override
public Integer getMaxPoolSize() { public Integer getMax() {
return null; return null;
} }
}; };
assertNull(dsm.getPoolUsage()); assertNull(dsm.getUsage());
} }
@Test @Test
public void getPoolUsageWithUnlimitedPool() { public void getPoolUsageWithUnlimitedPool() {
DataSourceMetadata unlimitedDataSource = createDataSourceMetadata(0, -1); DataSourcePoolMetadata unlimitedDataSource = createDataSourceMetadata(0, -1);
assertEquals(Float.valueOf(-1F), unlimitedDataSource.getPoolUsage()); assertEquals(Float.valueOf(-1F), unlimitedDataSource.getUsage());
} }
@Override @Override
@ -78,15 +78,15 @@ public class CommonsDbcpDataSourceMetadataTests extends
BasicDataSource dataSource = createDataSource(); BasicDataSource dataSource = createDataSource();
dataSource.setValidationQuery("SELECT FROM FOO"); dataSource.setValidationQuery("SELECT FROM FOO");
assertEquals("SELECT FROM FOO", assertEquals("SELECT FROM FOO",
new CommonsDbcpDataSourceMetadata(dataSource).getValidationQuery()); new CommonsDbcpDataSourcePoolMetadata(dataSource).getValidationQuery());
} }
private CommonsDbcpDataSourceMetadata createDataSourceMetadata(int minSize, private CommonsDbcpDataSourcePoolMetadata createDataSourceMetadata(int minSize,
int maxSize) { int maxSize) {
BasicDataSource dataSource = createDataSource(); BasicDataSource dataSource = createDataSource();
dataSource.setMinIdle(minSize); dataSource.setMinIdle(minSize);
dataSource.setMaxActive(maxSize); dataSource.setMaxActive(maxSize);
return new CommonsDbcpDataSourceMetadata(dataSource); return new CommonsDbcpDataSourcePoolMetadata(dataSource);
} }
private BasicDataSource createDataSource() { private BasicDataSource createDataSource() {

@ -30,26 +30,26 @@ import static org.junit.Assert.assertSame;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
/** /**
* Tests for {@link CompositeDataSourceMetadataProvider}. * Tests for {@link CompositeDataSourcePoolMetadataProvider}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class CompositeDataSourceMetadataProviderTests { public class CompositeDataSourceMetadataProviderTests {
@Mock @Mock
private DataSourceMetadataProvider firstProvider; private DataSourcePoolMetadataProvider firstProvider;
@Mock @Mock
private DataSourceMetadata first; private DataSourcePoolMetadata first;
@Mock @Mock
private DataSource firstDataSource; private DataSource firstDataSource;
@Mock @Mock
private DataSourceMetadataProvider secondProvider; private DataSourcePoolMetadataProvider secondProvider;
@Mock @Mock
private DataSourceMetadata second; private DataSourcePoolMetadata second;
@Mock @Mock
private DataSource secondDataSource; private DataSource secondDataSource;
@ -60,27 +60,27 @@ public class CompositeDataSourceMetadataProviderTests {
@Before @Before
public void setup() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
given(this.firstProvider.getDataSourceMetadata(this.firstDataSource)).willReturn( given(this.firstProvider.getDataSourcePoolMetadata(this.firstDataSource)).willReturn(
this.first); this.first);
given(this.firstProvider.getDataSourceMetadata(this.secondDataSource)) given(this.firstProvider.getDataSourcePoolMetadata(this.secondDataSource))
.willReturn(this.second); .willReturn(this.second);
} }
@Test @Test
public void createWithProviders() { public void createWithProviders() {
CompositeDataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider( CompositeDataSourcePoolMetadataProvider provider = new CompositeDataSourcePoolMetadataProvider(
Arrays.asList(this.firstProvider, this.secondProvider)); Arrays.asList(this.firstProvider, this.secondProvider));
assertSame(this.first, provider.getDataSourceMetadata(this.firstDataSource)); assertSame(this.first, provider.getDataSourcePoolMetadata(this.firstDataSource));
assertSame(this.second, provider.getDataSourceMetadata(this.secondDataSource)); assertSame(this.second, provider.getDataSourcePoolMetadata(this.secondDataSource));
assertNull(provider.getDataSourceMetadata(this.unknownDataSource)); assertNull(provider.getDataSourcePoolMetadata(this.unknownDataSource));
} }
@Test @Test
public void addProvider() { public void addProvider() {
CompositeDataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider(); CompositeDataSourcePoolMetadataProvider provider = new CompositeDataSourcePoolMetadataProvider();
assertNull(provider.getDataSourceMetadata(this.firstDataSource)); assertNull(provider.getDataSourcePoolMetadata(this.firstDataSource));
provider.addDataSourceMetadataProvider(this.firstProvider); provider.addDataSourceMetadataProvider(this.firstProvider);
assertSame(this.first, provider.getDataSourceMetadata(this.firstDataSource)); assertSame(this.first, provider.getDataSourcePoolMetadata(this.firstDataSource));
} }
} }

@ -23,22 +23,22 @@ import com.zaxxer.hikari.HikariDataSource;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/** /**
* Tests for {@link HikariDataSourceMetadata}. * Tests for {@link HikariDataSourcePoolMetadata}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class HikariDataSourceMetadataTests extends public class HikariDataSourcePoolMetadataTests extends
AbstractDataSourceMetadataTests<HikariDataSourceMetadata> { AbstractDataSourcePoolMetadataTests<HikariDataSourcePoolMetadata> {
private HikariDataSourceMetadata dataSourceMetadata; private HikariDataSourcePoolMetadata dataSourceMetadata;
@Before @Before
public void setup() { public void setup() {
this.dataSourceMetadata = new HikariDataSourceMetadata(createDataSource(0, 2)); this.dataSourceMetadata = new HikariDataSourcePoolMetadata(createDataSource(0, 2));
} }
@Override @Override
protected HikariDataSourceMetadata getDataSourceMetadata() { protected HikariDataSourcePoolMetadata getDataSourceMetadata() {
return this.dataSourceMetadata; return this.dataSourceMetadata;
} }
@ -47,7 +47,7 @@ public class HikariDataSourceMetadataTests extends
HikariDataSource dataSource = createDataSource(0, 4); HikariDataSource dataSource = createDataSource(0, 4);
dataSource.setConnectionTestQuery("SELECT FROM FOO"); dataSource.setConnectionTestQuery("SELECT FROM FOO");
assertEquals("SELECT FROM FOO", assertEquals("SELECT FROM FOO",
new HikariDataSourceMetadata(dataSource).getValidationQuery()); new HikariDataSourcePoolMetadata(dataSource).getValidationQuery());
} }
private HikariDataSource createDataSource(int minSize, int maxSize) { private HikariDataSource createDataSource(int minSize, int maxSize) {

@ -22,22 +22,22 @@ import org.junit.Before;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/** /**
* Tests for {@link TomcatDataSourceMetadata}. * Tests for {@link TomcatDataSourcePoolMetadata}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
public class TomcatDataSourceMetadataTests extends public class TomcatDataSourcePoolMetadataTests extends
AbstractDataSourceMetadataTests<TomcatDataSourceMetadata> { AbstractDataSourcePoolMetadataTests<TomcatDataSourcePoolMetadata> {
private TomcatDataSourceMetadata dataSourceMetadata; private TomcatDataSourcePoolMetadata dataSourceMetadata;
@Before @Before
public void setup() { public void setup() {
this.dataSourceMetadata = new TomcatDataSourceMetadata(createDataSource(0, 2)); this.dataSourceMetadata = new TomcatDataSourcePoolMetadata(createDataSource(0, 2));
} }
@Override @Override
protected TomcatDataSourceMetadata getDataSourceMetadata() { protected TomcatDataSourcePoolMetadata getDataSourceMetadata() {
return this.dataSourceMetadata; return this.dataSourceMetadata;
} }
@ -46,7 +46,7 @@ public class TomcatDataSourceMetadataTests extends
DataSource dataSource = createDataSource(0, 4); DataSource dataSource = createDataSource(0, 4);
dataSource.setValidationQuery("SELECT FROM FOO"); dataSource.setValidationQuery("SELECT FROM FOO");
assertEquals("SELECT FROM FOO", assertEquals("SELECT FROM FOO",
new TomcatDataSourceMetadata(dataSource).getValidationQuery()); new TomcatDataSourcePoolMetadata(dataSource).getValidationQuery());
} }
private DataSource createDataSource(int minSize, int maxSize) { private DataSource createDataSource(int minSize, int maxSize) {
Loading…
Cancel
Save