com.vladmihalcea.flexypool.config.Configuration Java Examples

The following examples show how to use com.vladmihalcea.flexypool.config.Configuration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FlexyPoolDataSource.java    From flexy-pool with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize <code>FlexyPoolDataSource</code> from {@link Configuration} and the associated list of {@link ConnectionAcquiringStrategyFactory}
 *
 * @param configuration                        configuration
 * @param connectionAcquiringStrategyFactories list of {@link ConnectionAcquiringStrategyFactory}
 */
private FlexyPoolDataSource(final Configuration<T> configuration,
                            List<ConnectionAcquiringStrategyFactory<? extends ConnectionAcquiringStrategy, T>> connectionAcquiringStrategyFactories) {
    this.uniqueName = configuration.getUniqueName();
    this.poolAdapter = configuration.getPoolAdapter();
    this.targetDataSource = poolAdapter.getTargetDataSource();
    this.metrics = configuration.getMetrics();
    this.connectionAcquireTotalTimer = metrics.timer(OVERALL_CONNECTION_ACQUIRE_MILLIS);
    this.concurrentConnectionCountHistogram = metrics.histogram(CONCURRENT_CONNECTIONS_HISTOGRAM);
    this.concurrentConnectionRequestCountHistogram = metrics.histogram(CONCURRENT_CONNECTION_REQUESTS_HISTOGRAM);
    this.connectionLeaseTimer = metrics.timer(CONNECTION_LEASE_MILLIS);
    this.connectionProxyFactory = configuration.getConnectionProxyFactory();
    if (connectionAcquiringStrategyFactories.isEmpty()) {
        LOGGER.info("FlexyPool is not using any strategy!");
    }
    for (ConnectionAcquiringStrategyFactory<? extends ConnectionAcquiringStrategy, T>
            connectionAcquiringStrategyFactory : connectionAcquiringStrategyFactories) {
        connectionAcquiringStrategies.add(connectionAcquiringStrategyFactory.newInstance(configuration));
    }
    eventPublisher = configuration.getEventPublisher();
    connectionAcquireTimeThresholdMillis = configuration.getConnectionAcquireTimeThresholdMillis();
    connectionLeaseTimeThresholdMillis = configuration.getConnectionLeaseTimeThresholdMillis();
}
 
Example #2
Source File: CodahaleMetricsFactoryServiceTest.java    From flexy-pool with Apache License 2.0 6 votes vote down vote up
@Test
public void testExistingCodahaleMetricsFactory() {
    DataSource dataSource = Mockito.mock(DataSource.class);
    PoolAdapterFactory<DataSource> poolAdapterFactory = Mockito.mock(PoolAdapterFactory.class);

    PoolAdapter poolAdapter = Mockito.mock(PoolAdapter.class);
    when(poolAdapterFactory.newInstance(any(ConfigurationProperties.class))).thenReturn(poolAdapter);

    ReservoirFactory reservoirFactory = Mockito.mock(ReservoirFactory.class);

    Configuration<DataSource> configuration = new Configuration.Builder<DataSource>(
            "unique", dataSource, poolAdapterFactory)
            .setMetricsFactory(new CodahaleMetrics.ReservoirMetricsFactory(reservoirFactory))
            .build();

    assertEquals(CodahaleHistogram.class, configuration.getMetrics().histogram("test").getClass());
}
 
Example #3
Source File: ConfigurationTest.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuilder() {
    final MeterRegistry meterRegistry = Mockito.mock(MeterRegistry.class);

    DataSource dataSource = Mockito.mock(DataSource.class);
    PoolAdapterFactory<DataSource> poolAdapterFactory = Mockito.mock(PoolAdapterFactory.class);
    ConnectionProxyFactory connectionProxyFactory = Mockito.mock(ConnectionProxyFactory.class);
    Metrics metrics = Mockito.mock(Metrics.class);
    PoolAdapter poolAdapter = Mockito.mock(PoolAdapter.class);
    when(poolAdapterFactory.newInstance(any(ConfigurationProperties.class))).thenReturn(poolAdapter);
    Configuration<DataSource> configuration = new Configuration.Builder<DataSource>(
            "unique", dataSource, poolAdapterFactory)
            .setConnectionProxyFactory(connectionProxyFactory)
            .setJmxAutoStart(true)
            .setJmxEnabled(true)
            .setMetricLogReporterMillis(120)
            .setMetricsFactory(new MetricsFactory() {
                @Override public Metrics newInstance(ConfigurationProperties configurationProperties) {
                    return new MicrometerMetrics(configurationProperties, meterRegistry);
                }
            })
            .build();
    assertSame("unique", configuration.getUniqueName());
    assertSame(connectionProxyFactory, configuration.getConnectionProxyFactory());
    assertTrue(configuration.isJmxAutoStart());
    assertTrue(configuration.isJmxEnabled());
    assertEquals(120, configuration.getMetricLogReporterMillis());
    assertSame(poolAdapter, configuration.getPoolAdapter());
    assertSame(dataSource, configuration.getTargetDataSource());
}
 
Example #4
Source File: ConfigurationTest.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuilder() {
    final MetricRegistry metricRegistry = Mockito.mock(MetricRegistry.class);

    DataSource dataSource = Mockito.mock(DataSource.class);
    PoolAdapterFactory<DataSource> poolAdapterFactory = Mockito.mock(PoolAdapterFactory.class);
    ConnectionProxyFactory connectionProxyFactory = Mockito.mock(ConnectionProxyFactory.class);
    Metrics metrics = Mockito.mock(Metrics.class);
    PoolAdapter poolAdapter = Mockito.mock(PoolAdapter.class);
    when(poolAdapterFactory.newInstance(any(ConfigurationProperties.class))).thenReturn(poolAdapter);
    Configuration<DataSource> configuration = new Configuration.Builder<DataSource>(
            "unique", dataSource, poolAdapterFactory)
            .setConnectionProxyFactory(connectionProxyFactory)
            .setJmxAutoStart(true)
            .setJmxEnabled(true)
            .setMetricLogReporterMillis(120)
            .setMetricsFactory(new MetricsFactory() {
                @Override
                public Metrics newInstance(ConfigurationProperties configurationProperties) {
                    return new DropwizardMetrics(configurationProperties, metricRegistry, new ReservoirFactory() {
                        @Override
                        public Reservoir newInstance(Class<? extends Metric> metricClass, String metricName) {
                            return new ExponentiallyDecayingReservoir();
                        }
                    });
                }
            })
            .build();
    assertSame("unique", configuration.getUniqueName());
    assertSame(connectionProxyFactory, configuration.getConnectionProxyFactory());
    assertTrue(configuration.isJmxAutoStart());
    assertTrue(configuration.isJmxEnabled());
    assertEquals(120, configuration.getMetricLogReporterMillis());
    assertSame(poolAdapter, configuration.getPoolAdapter());
    assertSame(dataSource, configuration.getTargetDataSource());
}
 
Example #5
Source File: ConfigurationTest.java    From flexy-pool with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuilder() {
    final MetricRegistry metricRegistry = Mockito.mock(MetricRegistry.class);

    DataSource dataSource = Mockito.mock(DataSource.class);
    PoolAdapterFactory<DataSource> poolAdapterFactory = Mockito.mock(PoolAdapterFactory.class);
    ConnectionProxyFactory connectionProxyFactory = Mockito.mock(ConnectionProxyFactory.class);
    Metrics metrics = Mockito.mock(Metrics.class);
    PoolAdapter poolAdapter = Mockito.mock(PoolAdapter.class);
    when(poolAdapterFactory.newInstance(any(ConfigurationProperties.class))).thenReturn(poolAdapter);
    Configuration<DataSource> configuration = new Configuration.Builder<DataSource>(
            "unique", dataSource, poolAdapterFactory)
            .setConnectionProxyFactory(connectionProxyFactory)
            .setJmxAutoStart(true)
            .setJmxEnabled(true)
            .setMetricLogReporterMillis(120)
            .setMetricsFactory(new MetricsFactory() {
                @Override
                public Metrics newInstance(ConfigurationProperties configurationProperties) {
                    return new CodahaleMetrics(configurationProperties, metricRegistry, new ReservoirFactory() {
                        @Override
                        public Reservoir newInstance(Class<? extends Metric> metricClass, String metricName) {
                            return new ExponentiallyDecayingReservoir();
                        }
                    });
                }
            })
            .build();
    assertSame("unique", configuration.getUniqueName());
    assertSame(connectionProxyFactory, configuration.getConnectionProxyFactory());
    assertTrue(configuration.isJmxAutoStart());
    assertTrue(configuration.isJmxEnabled());
    assertEquals(120, configuration.getMetricLogReporterMillis());
    assertSame(poolAdapter, configuration.getPoolAdapter());
    assertSame(dataSource, configuration.getTargetDataSource());
}
 
Example #6
Source File: FlexyPoolWrapper.java    From hammock with Apache License 2.0 5 votes vote down vote up
private DataSource wrapInternal(String name, HikariDataSource dataSource) {
    Configuration<HikariDataSource> configuration = createConfiguration(name, config, dataSource);
    int maxOverflow = config.getOptionalValue(format(MAX_OVERFLOW_PROPERTY_FORMAT, name), Integer.class).orElse(5);
    int retry = config.getOptionalValue(format(RETRY_PROPERTY_FORMAT, name), Integer.class).orElse(2);
    return new FlexyPoolDataSource<HikariDataSource>(configuration,
            new IncrementPoolOnTimeoutConnectionAcquiringStrategy.Factory(maxOverflow),
            new RetryConnectionAcquiringStrategy.Factory(retry)
    );
}
 
Example #7
Source File: FlexyPoolWrapper.java    From hammock with Apache License 2.0 5 votes vote down vote up
private Configuration<HikariDataSource> createConfiguration(String name, Config config, HikariDataSource dataSource) {
    String uniqueId = config.getOptionalValue(format(UNIQUE_ID_PROPERTY_FORMAT, name), String.class).orElse(name);
    Configuration.Builder<HikariDataSource> builder = new Configuration.Builder<>(uniqueId, dataSource, HikariCPPoolAdapter.FACTORY);
    if(metricsFactoryInstance.isResolvable()) {
        MetricsFactory metricsFactory = metricsFactoryInstance.get();
        builder.setMetricsFactory(metricsFactory);
    }
    return builder.build();
}
 
Example #8
Source File: FlexyPoolDataSource.java    From flexy-pool with Apache License 2.0 4 votes vote down vote up
public FlexyPoolDataSourceConfiguration(
        Configuration<D> configuration,
        List<ConnectionAcquiringStrategyFactory<? extends ConnectionAcquiringStrategy, D>> connectionAcquiringStrategyFactories) {
    this.configuration = configuration;
    this.connectionAcquiringStrategyFactories = connectionAcquiringStrategyFactories;
}
 
Example #9
Source File: FlexyPoolDataSource.java    From flexy-pool with Apache License 2.0 4 votes vote down vote up
public Configuration<D> getConfiguration() {
    return configuration;
}
 
Example #10
Source File: FlexyPoolDataSource.java    From flexy-pool with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private Configuration<D> configuration(D dataSource) {
    String uniqueName = propertyLoader.getUniqueName();
    PoolAdapterFactory<D> poolAdapterFactory = propertyLoader.getPoolAdapterFactory();
    MetricsFactory metricsFactory = propertyLoader.getMetricsFactory();
    ConnectionProxyFactory connectionProxyFactory = propertyLoader.getConnectionProxyFactory();
    Integer metricLogReporterMillis = propertyLoader.getMetricLogReporterMillis();
    Boolean jmxEnabled = propertyLoader.isJmxEnabled();
    Boolean jmxAutoStart = propertyLoader.isJmxAutoStart();
    EventListenerResolver eventListenerResolver = propertyLoader.getEventListenerResolver();
    Long connectionAcquireTimeThresholdMillis = propertyLoader.getConnectionAcquireTimeThresholdMillis();
    Long connectionLeaseTimeThresholdMillis = propertyLoader.getConnectionLeaseTimeThresholdMillis();

    if (poolAdapterFactory == null) {
        poolAdapterFactory = (PoolAdapterFactory<D>) DataSourcePoolAdapter.FACTORY;
    }

    Configuration.Builder<D> configurationBuilder = new Configuration.Builder<D>(
            uniqueName, dataSource, poolAdapterFactory
    );
    if (metricsFactory != null) {
        configurationBuilder.setMetricsFactory(metricsFactory);
    }
    if (connectionProxyFactory != null) {
        configurationBuilder.setConnectionProxyFactory(connectionProxyFactory);
    }
    if (metricLogReporterMillis != null) {
        configurationBuilder.setMetricLogReporterMillis(metricLogReporterMillis);
    }
    if (jmxEnabled != null) {
        configurationBuilder.setJmxEnabled(jmxEnabled);
    }
    if (jmxAutoStart != null) {
        configurationBuilder.setJmxAutoStart(jmxAutoStart);
    }
    if (eventListenerResolver != null) {
        configurationBuilder.setEventListenerResolver(eventListenerResolver);
    }
    if (connectionAcquireTimeThresholdMillis != null) {
        configurationBuilder.setConnectionAcquireTimeThresholdMillis(connectionAcquireTimeThresholdMillis);
    }
    if (connectionLeaseTimeThresholdMillis != null) {
        configurationBuilder.setConnectionLeaseTimeThresholdMillis(connectionLeaseTimeThresholdMillis);
    }
    return configurationBuilder.build();
}
 
Example #11
Source File: DataSourcePoolAdapterTest.java    From flexy-pool with Apache License 2.0 4 votes vote down vote up
@Override
protected AbstractPoolAdapter<DataSource> newPoolAdapter(Configuration<DataSource> configuration) {
    return new DataSourcePoolAdapter(configuration);
}
 
Example #12
Source File: AbstractPoolAdapterTest.java    From flexy-pool with Apache License 2.0 4 votes vote down vote up
protected AbstractPoolAdapter<DataSource> newPoolAdapter(Configuration<DataSource> configuration) {
    return new TestPoolAdapter(configuration);
}
 
Example #13
Source File: FlexyPoolDataSource.java    From flexy-pool with Apache License 2.0 2 votes vote down vote up
/**
 * Initialize <code>FlexyPoolDataSource</code> from {@link Configuration} and the array of {@link ConnectionAcquiringStrategyFactory}
 *
 * @param configuration                        configuration
 * @param connectionAcquiringStrategyFactories array of {@link ConnectionAcquiringStrategyFactory}
 */
public FlexyPoolDataSource(final Configuration<T> configuration,
                           ConnectionAcquiringStrategyFactory<? extends ConnectionAcquiringStrategy, T>... connectionAcquiringStrategyFactories) {
    this(configuration, Arrays.asList(connectionAcquiringStrategyFactories));
}
 
Example #14
Source File: FlexyPoolConfigurationBuilderCustomizer.java    From spring-boot-data-source-decorator with Apache License 2.0 votes vote down vote up
void customize(String beanName, Configuration.Builder<?> builder, Class<?> dataSourceClass);