DataSourceMetadata -> DataSourcePoolMetadata
Rename DataSourceMetadata to DataSourcePoolMetadata
This commit is contained in:
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.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceMetadataProvider;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceMetadataProvidersConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourcePoolMetadataProvider;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourcePoolMetadataProvidersConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
|
|
@ -38,11 +38,11 @@ import org.springframework.context.annotation.Import;
|
|||
*/
|
||||
@ConditionalOnBean(DataSource.class)
|
||||
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
|
||||
@Import(DataSourceMetadataProvidersConfiguration.class)
|
||||
@Import(DataSourcePoolMetadataProvidersConfiguration.class)
|
||||
public class DataSourceMetricsAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(DataSourceMetadataProvider.class)
|
||||
@ConditionalOnBean(DataSourcePoolMetadataProvider.class)
|
||||
@ConditionalOnMissingBean
|
||||
public DataSourcePublicMetrics 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.beans.factory.annotation.Autowired;
|
||||
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.DataSourceHealthIndicator;
|
||||
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.RedisHealthIndicator;
|
||||
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.AutoConfigureBefore;
|
||||
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.ConditionalOnExpression;
|
||||
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.DataSourceMetadata;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceMetadataProvider;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourcePoolMetadataProvider;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourcePoolMetadata;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration;
|
||||
|
|
@ -103,33 +103,36 @@ public class HealthIndicatorAutoConfiguration {
|
|||
private Map<String, DataSource> dataSources;
|
||||
|
||||
@Autowired(required = false)
|
||||
private Collection<DataSourceMetadataProvider> metadataProviders = Collections.emptyList();
|
||||
private Collection<DataSourcePoolMetadataProvider> metadataProviders = Collections
|
||||
.emptyList();
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(name = "dbHealthIndicator")
|
||||
public HealthIndicator dbHealthIndicator() {
|
||||
DataSourceMetadataProvider metadataProvider =
|
||||
new CompositeDataSourceMetadataProvider(this.metadataProviders);
|
||||
DataSourcePoolMetadataProvider metadataProvider = new CompositeDataSourcePoolMetadataProvider(
|
||||
this.metadataProviders);
|
||||
if (this.dataSources.size() == 1) {
|
||||
return createDataSourceHealthIndicator(metadataProvider,
|
||||
this.dataSources.values().iterator().next());
|
||||
return createDataSourceHealthIndicator(metadataProvider, this.dataSources
|
||||
.values().iterator().next());
|
||||
}
|
||||
CompositeHealthIndicator composite = new CompositeHealthIndicator(
|
||||
this.healthAggregator);
|
||||
for (Map.Entry<String, DataSource> entry : this.dataSources.entrySet()) {
|
||||
composite.addHealthIndicator(entry.getKey(),
|
||||
createDataSourceHealthIndicator(metadataProvider, entry.getValue()));
|
||||
composite.addHealthIndicator(
|
||||
entry.getKey(),
|
||||
createDataSourceHealthIndicator(metadataProvider,
|
||||
entry.getValue()));
|
||||
}
|
||||
return composite;
|
||||
}
|
||||
|
||||
private DataSourceHealthIndicator createDataSourceHealthIndicator(DataSourceMetadataProvider provider,
|
||||
DataSource dataSource) {
|
||||
|
||||
private DataSourceHealthIndicator createDataSourceHealthIndicator(
|
||||
DataSourcePoolMetadataProvider provider, DataSource dataSource) {
|
||||
String validationQuery = null;
|
||||
DataSourceMetadata dataSourceMetadata = provider.getDataSourceMetadata(dataSource);
|
||||
if (dataSourceMetadata != null) {
|
||||
validationQuery = dataSourceMetadata.getValidationQuery();
|
||||
DataSourcePoolMetadata poolMetadata = provider
|
||||
.getDataSourcePoolMetadata(dataSource);
|
||||
if (poolMetadata != null) {
|
||||
validationQuery = poolMetadata.getValidationQuery();
|
||||
}
|
||||
return new DataSourceHealthIndicator(dataSource, validationQuery);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,9 +28,9 @@ import javax.sql.DataSource;
|
|||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.metrics.Metric;
|
||||
import org.springframework.boot.autoconfigure.jdbc.CompositeDataSourceMetadataProvider;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceMetadata;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceMetadataProvider;
|
||||
import org.springframework.boot.autoconfigure.jdbc.CompositeDataSourcePoolMetadataProvider;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourcePoolMetadata;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourcePoolMetadataProvider;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
|
|
@ -48,23 +48,23 @@ public class DataSourcePublicMetrics implements PublicMetrics {
|
|||
private ApplicationContext applicationContext;
|
||||
|
||||
@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
|
||||
public void initialize() {
|
||||
DataSource primaryDataSource = getPrimaryDataSource();
|
||||
DataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider(
|
||||
DataSourcePoolMetadataProvider provider = new CompositeDataSourcePoolMetadataProvider(
|
||||
this.providers);
|
||||
for (Map.Entry<String, DataSource> entry : this.applicationContext
|
||||
.getBeansOfType(DataSource.class).entrySet()) {
|
||||
String beanName = entry.getKey();
|
||||
DataSource bean = entry.getValue();
|
||||
String prefix = createPrefix(beanName, bean, bean.equals(primaryDataSource));
|
||||
DataSourceMetadata dataSourceMetadata = provider.getDataSourceMetadata(bean);
|
||||
if (dataSourceMetadata != null) {
|
||||
this.metadataByPrefix.put(prefix, dataSourceMetadata);
|
||||
DataSourcePoolMetadata poolMetadata = provider.getDataSourcePoolMetadata(bean);
|
||||
if (poolMetadata != null) {
|
||||
this.metadataByPrefix.put(prefix, poolMetadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -72,13 +72,13 @@ public class DataSourcePublicMetrics implements PublicMetrics {
|
|||
@Override
|
||||
public Collection<Metric<?>> metrics() {
|
||||
Set<Metric<?>> metrics = new LinkedHashSet<Metric<?>>();
|
||||
for (Map.Entry<String, DataSourceMetadata> entry : this.metadataByPrefix
|
||||
for (Map.Entry<String, DataSourcePoolMetadata> entry : this.metadataByPrefix
|
||||
.entrySet()) {
|
||||
String prefix = entry.getKey();
|
||||
prefix = (prefix.endsWith(".") ? prefix : prefix + ".");
|
||||
DataSourceMetadata dataSourceMetadata = entry.getValue();
|
||||
addMetric(metrics, prefix + "active", dataSourceMetadata.getPoolSize());
|
||||
addMetric(metrics, prefix + "usage", dataSourceMetadata.getPoolUsage());
|
||||
DataSourcePoolMetadata dataSourceMetadata = entry.getValue();
|
||||
addMetric(metrics, prefix + "active", dataSourceMetadata.getActive());
|
||||
addMetric(metrics, prefix + "usage", dataSourceMetadata.getUsage());
|
||||
}
|
||||
return metrics;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
|
|||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration;
|
||||
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.EmbeddedDataSourceConfiguration;
|
||||
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
|
||||
|
|
@ -167,7 +167,7 @@ public class HealthIndicatorAutoConfigurationTests {
|
|||
public void dataSourceHealthIndicatorWithCustomValidationQuery() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.register(PropertyPlaceholderAutoConfiguration.class, DataSourceProperties.class,
|
||||
DataSourceConfig.class, DataSourceMetadataProvidersConfiguration.class,
|
||||
DataSourceConfig.class, DataSourcePoolMetadataProvidersConfiguration.class,
|
||||
HealthIndicatorAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.validation-query:SELECT from FOOBAR");
|
||||
this.context.refresh();
|
||||
|
|
|
|||
|
|
@ -19,27 +19,27 @@ package org.springframework.boot.autoconfigure.jdbc;
|
|||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* A base {@link DataSourceMetadata} implementation.
|
||||
* A base {@link DataSourcePoolMetadata} implementation.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public abstract class AbstractDataSourceMetadata<T extends DataSource> implements
|
||||
DataSourceMetadata {
|
||||
public abstract class AbstractDataSourcePoolMetadata<T extends DataSource> implements
|
||||
DataSourcePoolMetadata {
|
||||
|
||||
private final T dataSource;
|
||||
|
||||
/**
|
||||
* Create an instance with the data source to use.
|
||||
*/
|
||||
protected AbstractDataSourceMetadata(T dataSource) {
|
||||
protected AbstractDataSourcePoolMetadata(T dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getPoolUsage() {
|
||||
Integer maxSize = getMaxPoolSize();
|
||||
Integer currentSize = getPoolSize();
|
||||
public Float getUsage() {
|
||||
Integer maxSize = getMax();
|
||||
Integer currentSize = getActive();
|
||||
if (maxSize == null || currentSize == null) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -21,30 +21,30 @@ import javax.sql.DataSource;
|
|||
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
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public class CommonsDbcpDataSourceMetadata extends
|
||||
AbstractDataSourceMetadata<BasicDataSource> {
|
||||
public class CommonsDbcpDataSourcePoolMetadata extends
|
||||
AbstractDataSourcePoolMetadata<BasicDataSource> {
|
||||
|
||||
public CommonsDbcpDataSourceMetadata(BasicDataSource dataSource) {
|
||||
public CommonsDbcpDataSourcePoolMetadata(BasicDataSource dataSource) {
|
||||
super(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPoolSize() {
|
||||
public Integer getActive() {
|
||||
return getDataSource().getNumActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getMaxPoolSize() {
|
||||
public Integer getMax() {
|
||||
return getDataSource().getMaxActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getMinPoolSize() {
|
||||
public Integer getMin() {
|
||||
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;
|
||||
|
||||
/**
|
||||
* Provides access meta-data that is commonly available from most {@link DataSource}
|
||||
* implementations.
|
||||
* Provides access meta-data that is commonly available from most polled
|
||||
* {@link DataSource} implementations.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @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
|
||||
|
|
@ -38,26 +38,26 @@ public interface DataSourceMetadata {
|
|||
* This may also return {@code null} if the data source does not provide the necessary
|
||||
* information to compute the poll usage.
|
||||
*/
|
||||
Float getPoolUsage();
|
||||
Float getUsage();
|
||||
|
||||
/**
|
||||
* Return the current number of active connections that have been allocated from the
|
||||
* 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
|
||||
* time or {@code -1} if there is no limit. Can also return {@code null} if that
|
||||
* information is not available.
|
||||
*/
|
||||
Integer getMaxPoolSize();
|
||||
Integer getMax();
|
||||
|
||||
/**
|
||||
* Return the minimum number of idle connections in the pool or {@code null} if that
|
||||
* information is not available.
|
||||
*/
|
||||
Integer getMinPoolSize();
|
||||
Integer getMin();
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Provide a {@link DataSourceMetadata} based on a {@link DataSource}.
|
||||
* Provide a {@link DataSourcePoolMetadata} based on a {@link DataSource}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @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.
|
||||
*/
|
||||
DataSourceMetadata getDataSourceMetadata(DataSource dataSource);
|
||||
DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource);
|
||||
|
||||
}
|
||||
|
|
@ -26,26 +26,27 @@ import org.springframework.context.annotation.Configuration;
|
|||
import com.zaxxer.hikari.HikariDataSource;
|
||||
|
||||
/**
|
||||
* Register the {@link DataSourceMetadataProvider} instances for the supported data
|
||||
* Register the {@link DataSourcePoolMetadataProvider} instances for the supported data
|
||||
* sources.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Configuration
|
||||
public class DataSourceMetadataProvidersConfiguration {
|
||||
public class DataSourcePoolMetadataProvidersConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnClass(org.apache.tomcat.jdbc.pool.DataSource.class)
|
||||
static class TomcatDataSourceProviderConfiguration {
|
||||
static class TomcatDataSourcePoolMetadataProviderConfiguration {
|
||||
|
||||
@Bean
|
||||
public DataSourceMetadataProvider tomcatDataSourceProvider() {
|
||||
return new DataSourceMetadataProvider() {
|
||||
public DataSourcePoolMetadataProvider tomcatPoolDataSourceMetadataProvider() {
|
||||
return new DataSourcePoolMetadataProvider() {
|
||||
@Override
|
||||
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) {
|
||||
public DataSourcePoolMetadata getDataSourcePoolMetadata(
|
||||
DataSource dataSource) {
|
||||
if (dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource) {
|
||||
return new TomcatDataSourceMetadata(
|
||||
return new TomcatDataSourcePoolMetadata(
|
||||
(org.apache.tomcat.jdbc.pool.DataSource) dataSource);
|
||||
}
|
||||
return null;
|
||||
|
|
@ -57,15 +58,17 @@ public class DataSourceMetadataProvidersConfiguration {
|
|||
|
||||
@Configuration
|
||||
@ConditionalOnClass(HikariDataSource.class)
|
||||
static class HikariDataSourceProviderConfiguration {
|
||||
static class HikariPoolDataSourceMetadataProviderConfiguration {
|
||||
|
||||
@Bean
|
||||
public DataSourceMetadataProvider hikariDataSourceProvider() {
|
||||
return new DataSourceMetadataProvider() {
|
||||
public DataSourcePoolMetadataProvider hikariPoolDataSourceMetadataProvider() {
|
||||
return new DataSourcePoolMetadataProvider() {
|
||||
@Override
|
||||
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) {
|
||||
public DataSourcePoolMetadata getDataSourcePoolMetadata(
|
||||
DataSource dataSource) {
|
||||
if (dataSource instanceof HikariDataSource) {
|
||||
return new HikariDataSourceMetadata((HikariDataSource) dataSource);
|
||||
return new HikariDataSourcePoolMetadata(
|
||||
(HikariDataSource) dataSource);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -76,15 +79,16 @@ public class DataSourceMetadataProvidersConfiguration {
|
|||
|
||||
@Configuration
|
||||
@ConditionalOnClass(BasicDataSource.class)
|
||||
static class CommonsDbcpDataSourceProviderConfiguration {
|
||||
static class CommonsDbcpPoolDataSourceMetadataProviderConfiguration {
|
||||
|
||||
@Bean
|
||||
public DataSourceMetadataProvider commonsDbcpDataSourceProvider() {
|
||||
return new DataSourceMetadataProvider() {
|
||||
public DataSourcePoolMetadataProvider commonsDbcpPoolDataSourceMetadataProvider() {
|
||||
return new DataSourcePoolMetadataProvider() {
|
||||
@Override
|
||||
public DataSourceMetadata getDataSourceMetadata(DataSource dataSource) {
|
||||
public DataSourcePoolMetadata getDataSourcePoolMetadata(
|
||||
DataSource dataSource) {
|
||||
if (dataSource instanceof BasicDataSource) {
|
||||
return new CommonsDbcpDataSourceMetadata(
|
||||
return new CommonsDbcpDataSourcePoolMetadata(
|
||||
(BasicDataSource) dataSource);
|
||||
}
|
||||
return null;
|
||||
|
|
@ -24,20 +24,20 @@ import com.zaxxer.hikari.HikariDataSource;
|
|||
import com.zaxxer.hikari.pool.HikariPool;
|
||||
|
||||
/**
|
||||
* {@link DataSourceMetadata} for a Hikari {@link DataSource}.
|
||||
* {@link DataSourcePoolMetadata} for a Hikari {@link DataSource}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public class HikariDataSourceMetadata extends
|
||||
AbstractDataSourceMetadata<HikariDataSource> {
|
||||
public class HikariDataSourcePoolMetadata extends
|
||||
AbstractDataSourcePoolMetadata<HikariDataSource> {
|
||||
|
||||
public HikariDataSourceMetadata(HikariDataSource dataSource) {
|
||||
public HikariDataSourcePoolMetadata(HikariDataSource dataSource) {
|
||||
super(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPoolSize() {
|
||||
public Integer getActive() {
|
||||
try {
|
||||
return getHikariPool().getActiveConnections();
|
||||
}
|
||||
|
|
@ -52,12 +52,12 @@ public class HikariDataSourceMetadata extends
|
|||
}
|
||||
|
||||
@Override
|
||||
public Integer getMaxPoolSize() {
|
||||
public Integer getMax() {
|
||||
return getDataSource().getMaximumPoolSize();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getMinPoolSize() {
|
||||
public Integer getMin() {
|
||||
return getDataSource().getMinimumIdle();
|
||||
}
|
||||
|
||||
|
|
@ -20,29 +20,29 @@ import org.apache.tomcat.jdbc.pool.ConnectionPool;
|
|||
import org.apache.tomcat.jdbc.pool.DataSource;
|
||||
|
||||
/**
|
||||
* {@link DataSourceMetadata} for a Tomcat {@link DataSource}.
|
||||
* {@link DataSourcePoolMetadata} for a Tomcat {@link DataSource}.
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPoolSize() {
|
||||
public Integer getActive() {
|
||||
ConnectionPool pool = getDataSource().getPool();
|
||||
return (pool == null ? 0 : pool.getActive());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getMaxPoolSize() {
|
||||
public Integer getMax() {
|
||||
return getDataSource().getMaxActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getMinPoolSize() {
|
||||
public Integer getMin() {
|
||||
return getDataSource().getMinIdle();
|
||||
}
|
||||
|
||||
|
|
@ -27,11 +27,11 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Abstract base class for {@link DataSourceMetadata} tests.
|
||||
* Abstract base class for {@link DataSourcePoolMetadata} tests.
|
||||
*
|
||||
* @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.
|
||||
|
|
@ -40,12 +40,12 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
|
|||
|
||||
@Test
|
||||
public void getMaxPoolSize() {
|
||||
assertEquals(Integer.valueOf(2), getDataSourceMetadata().getMaxPoolSize());
|
||||
assertEquals(Integer.valueOf(2), getDataSourceMetadata().getMax());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMinPoolSize() {
|
||||
assertEquals(Integer.valueOf(0), getDataSourceMetadata().getMinPoolSize());
|
||||
assertEquals(Integer.valueOf(0), getDataSourceMetadata().getMin());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -60,8 +60,8 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
|
|||
return null;
|
||||
}
|
||||
});
|
||||
assertEquals(Integer.valueOf(0), getDataSourceMetadata().getPoolSize());
|
||||
assertEquals(Float.valueOf(0), getDataSourceMetadata().getPoolUsage());
|
||||
assertEquals(Integer.valueOf(0), getDataSourceMetadata().getActive());
|
||||
assertEquals(Float.valueOf(0), getDataSourceMetadata().getUsage());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -72,8 +72,8 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
|
|||
@Override
|
||||
public Void doInConnection(Connection connection) throws SQLException,
|
||||
DataAccessException {
|
||||
assertEquals(Integer.valueOf(1), getDataSourceMetadata().getPoolSize());
|
||||
assertEquals(Float.valueOf(0.5F), getDataSourceMetadata().getPoolUsage());
|
||||
assertEquals(Integer.valueOf(1), getDataSourceMetadata().getActive());
|
||||
assertEquals(Float.valueOf(0.5F), getDataSourceMetadata().getUsage());
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
|
@ -92,9 +92,9 @@ public abstract class AbstractDataSourceMetadataTests<D extends AbstractDataSour
|
|||
public Void doInConnection(Connection connection)
|
||||
throws SQLException, DataAccessException {
|
||||
assertEquals(Integer.valueOf(2), getDataSourceMetadata()
|
||||
.getPoolSize());
|
||||
.getActive());
|
||||
assertEquals(Float.valueOf(1F), getDataSourceMetadata()
|
||||
.getPoolUsage());
|
||||
.getUsage());
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
|
@ -24,14 +24,14 @@ import static org.junit.Assert.assertEquals;
|
|||
import static org.junit.Assert.assertNull;
|
||||
|
||||
/**
|
||||
* Tests for {@link CommonsDbcpDataSourceMetadata}.
|
||||
* Tests for {@link CommonsDbcpDataSourcePoolMetadata}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class CommonsDbcpDataSourceMetadataTests extends
|
||||
AbstractDataSourceMetadataTests<CommonsDbcpDataSourceMetadata> {
|
||||
public class CommonsDbcpDataSourcePoolMetadataTests extends
|
||||
AbstractDataSourcePoolMetadataTests<CommonsDbcpDataSourcePoolMetadata> {
|
||||
|
||||
private CommonsDbcpDataSourceMetadata dataSourceMetadata;
|
||||
private CommonsDbcpDataSourcePoolMetadata dataSourceMetadata;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
|
@ -39,38 +39,38 @@ public class CommonsDbcpDataSourceMetadataTests extends
|
|||
}
|
||||
|
||||
@Override
|
||||
protected CommonsDbcpDataSourceMetadata getDataSourceMetadata() {
|
||||
protected CommonsDbcpDataSourcePoolMetadata getDataSourceMetadata() {
|
||||
return this.dataSourceMetadata;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPoolUsageWithNoCurrent() {
|
||||
CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata(
|
||||
CommonsDbcpDataSourcePoolMetadata dsm = new CommonsDbcpDataSourcePoolMetadata(
|
||||
createDataSource()) {
|
||||
@Override
|
||||
public Integer getPoolSize() {
|
||||
public Integer getActive() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
assertNull(dsm.getPoolUsage());
|
||||
assertNull(dsm.getUsage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPoolUsageWithNoMax() {
|
||||
CommonsDbcpDataSourceMetadata dsm = new CommonsDbcpDataSourceMetadata(
|
||||
CommonsDbcpDataSourcePoolMetadata dsm = new CommonsDbcpDataSourcePoolMetadata(
|
||||
createDataSource()) {
|
||||
@Override
|
||||
public Integer getMaxPoolSize() {
|
||||
public Integer getMax() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
assertNull(dsm.getPoolUsage());
|
||||
assertNull(dsm.getUsage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPoolUsageWithUnlimitedPool() {
|
||||
DataSourceMetadata unlimitedDataSource = createDataSourceMetadata(0, -1);
|
||||
assertEquals(Float.valueOf(-1F), unlimitedDataSource.getPoolUsage());
|
||||
DataSourcePoolMetadata unlimitedDataSource = createDataSourceMetadata(0, -1);
|
||||
assertEquals(Float.valueOf(-1F), unlimitedDataSource.getUsage());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -78,15 +78,15 @@ public class CommonsDbcpDataSourceMetadataTests extends
|
|||
BasicDataSource dataSource = createDataSource();
|
||||
dataSource.setValidationQuery("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) {
|
||||
BasicDataSource dataSource = createDataSource();
|
||||
dataSource.setMinIdle(minSize);
|
||||
dataSource.setMaxActive(maxSize);
|
||||
return new CommonsDbcpDataSourceMetadata(dataSource);
|
||||
return new CommonsDbcpDataSourcePoolMetadata(dataSource);
|
||||
}
|
||||
|
||||
private BasicDataSource createDataSource() {
|
||||
|
|
@ -30,26 +30,26 @@ import static org.junit.Assert.assertSame;
|
|||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
/**
|
||||
* Tests for {@link CompositeDataSourceMetadataProvider}.
|
||||
* Tests for {@link CompositeDataSourcePoolMetadataProvider}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class CompositeDataSourceMetadataProviderTests {
|
||||
|
||||
@Mock
|
||||
private DataSourceMetadataProvider firstProvider;
|
||||
private DataSourcePoolMetadataProvider firstProvider;
|
||||
|
||||
@Mock
|
||||
private DataSourceMetadata first;
|
||||
private DataSourcePoolMetadata first;
|
||||
|
||||
@Mock
|
||||
private DataSource firstDataSource;
|
||||
|
||||
@Mock
|
||||
private DataSourceMetadataProvider secondProvider;
|
||||
private DataSourcePoolMetadataProvider secondProvider;
|
||||
|
||||
@Mock
|
||||
private DataSourceMetadata second;
|
||||
private DataSourcePoolMetadata second;
|
||||
|
||||
@Mock
|
||||
private DataSource secondDataSource;
|
||||
|
|
@ -60,27 +60,27 @@ public class CompositeDataSourceMetadataProviderTests {
|
|||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
given(this.firstProvider.getDataSourceMetadata(this.firstDataSource)).willReturn(
|
||||
given(this.firstProvider.getDataSourcePoolMetadata(this.firstDataSource)).willReturn(
|
||||
this.first);
|
||||
given(this.firstProvider.getDataSourceMetadata(this.secondDataSource))
|
||||
given(this.firstProvider.getDataSourcePoolMetadata(this.secondDataSource))
|
||||
.willReturn(this.second);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createWithProviders() {
|
||||
CompositeDataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider(
|
||||
CompositeDataSourcePoolMetadataProvider provider = new CompositeDataSourcePoolMetadataProvider(
|
||||
Arrays.asList(this.firstProvider, this.secondProvider));
|
||||
assertSame(this.first, provider.getDataSourceMetadata(this.firstDataSource));
|
||||
assertSame(this.second, provider.getDataSourceMetadata(this.secondDataSource));
|
||||
assertNull(provider.getDataSourceMetadata(this.unknownDataSource));
|
||||
assertSame(this.first, provider.getDataSourcePoolMetadata(this.firstDataSource));
|
||||
assertSame(this.second, provider.getDataSourcePoolMetadata(this.secondDataSource));
|
||||
assertNull(provider.getDataSourcePoolMetadata(this.unknownDataSource));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addProvider() {
|
||||
CompositeDataSourceMetadataProvider provider = new CompositeDataSourceMetadataProvider();
|
||||
assertNull(provider.getDataSourceMetadata(this.firstDataSource));
|
||||
CompositeDataSourcePoolMetadataProvider provider = new CompositeDataSourcePoolMetadataProvider();
|
||||
assertNull(provider.getDataSourcePoolMetadata(this.firstDataSource));
|
||||
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;
|
||||
|
||||
/**
|
||||
* Tests for {@link HikariDataSourceMetadata}.
|
||||
* Tests for {@link HikariDataSourcePoolMetadata}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class HikariDataSourceMetadataTests extends
|
||||
AbstractDataSourceMetadataTests<HikariDataSourceMetadata> {
|
||||
public class HikariDataSourcePoolMetadataTests extends
|
||||
AbstractDataSourcePoolMetadataTests<HikariDataSourcePoolMetadata> {
|
||||
|
||||
private HikariDataSourceMetadata dataSourceMetadata;
|
||||
private HikariDataSourcePoolMetadata dataSourceMetadata;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.dataSourceMetadata = new HikariDataSourceMetadata(createDataSource(0, 2));
|
||||
this.dataSourceMetadata = new HikariDataSourcePoolMetadata(createDataSource(0, 2));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HikariDataSourceMetadata getDataSourceMetadata() {
|
||||
protected HikariDataSourcePoolMetadata getDataSourceMetadata() {
|
||||
return this.dataSourceMetadata;
|
||||
}
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ public class HikariDataSourceMetadataTests extends
|
|||
HikariDataSource dataSource = createDataSource(0, 4);
|
||||
dataSource.setConnectionTestQuery("SELECT FROM FOO");
|
||||
assertEquals("SELECT FROM FOO",
|
||||
new HikariDataSourceMetadata(dataSource).getValidationQuery());
|
||||
new HikariDataSourcePoolMetadata(dataSource).getValidationQuery());
|
||||
}
|
||||
|
||||
private HikariDataSource createDataSource(int minSize, int maxSize) {
|
||||
|
|
@ -22,22 +22,22 @@ import org.junit.Before;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Tests for {@link TomcatDataSourceMetadata}.
|
||||
* Tests for {@link TomcatDataSourcePoolMetadata}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class TomcatDataSourceMetadataTests extends
|
||||
AbstractDataSourceMetadataTests<TomcatDataSourceMetadata> {
|
||||
public class TomcatDataSourcePoolMetadataTests extends
|
||||
AbstractDataSourcePoolMetadataTests<TomcatDataSourcePoolMetadata> {
|
||||
|
||||
private TomcatDataSourceMetadata dataSourceMetadata;
|
||||
private TomcatDataSourcePoolMetadata dataSourceMetadata;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.dataSourceMetadata = new TomcatDataSourceMetadata(createDataSource(0, 2));
|
||||
this.dataSourceMetadata = new TomcatDataSourcePoolMetadata(createDataSource(0, 2));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected TomcatDataSourceMetadata getDataSourceMetadata() {
|
||||
protected TomcatDataSourcePoolMetadata getDataSourceMetadata() {
|
||||
return this.dataSourceMetadata;
|
||||
}
|
||||
|
||||
|
|
@ -46,7 +46,7 @@ public class TomcatDataSourceMetadataTests extends
|
|||
DataSource dataSource = createDataSource(0, 4);
|
||||
dataSource.setValidationQuery("SELECT FROM FOO");
|
||||
assertEquals("SELECT FROM FOO",
|
||||
new TomcatDataSourceMetadata(dataSource).getValidationQuery());
|
||||
new TomcatDataSourcePoolMetadata(dataSource).getValidationQuery());
|
||||
}
|
||||
|
||||
private DataSource createDataSource(int minSize, int maxSize) {
|
||||
Loading…
Reference in New Issue