Search in sources :

Example 1 with ManagedDataSourceSupplier

use of io.bootique.jdbc.managed.ManagedDataSourceSupplier in project bootique-jdbc by bootique.

the class HikariCPManagedDataSourceFactory method create.

@Override
public Optional<ManagedDataSourceSupplier> create(String dataSourceName, Injector injector) {
    if (jdbcUrl == null) {
        return Optional.empty();
    }
    Supplier<DataSource> startup = () -> {
        validate();
        HikariConfig hikariConfig = toConfiguration();
        return new HikariDataSource(hikariConfig);
    };
    Consumer<DataSource> shutdown = ds -> ((HikariDataSource) ds).close();
    return Optional.of(new ManagedDataSourceSupplier(getJdbcUrl(), startup, shutdown));
}
Also used : Properties(java.util.Properties) MINUTES(java.util.concurrent.TimeUnit.MINUTES) Supplier(java.util.function.Supplier) Injector(com.google.inject.Injector) Objects(java.util.Objects) Consumer(java.util.function.Consumer) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) BQConfigProperty(io.bootique.annotation.BQConfigProperty) HikariConfig(com.zaxxer.hikari.HikariConfig) ManagedDataSourceFactory(io.bootique.jdbc.managed.ManagedDataSourceFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource) ManagedDataSourceSupplier(io.bootique.jdbc.managed.ManagedDataSourceSupplier) Optional(java.util.Optional) ThreadFactory(java.util.concurrent.ThreadFactory) SECONDS(java.util.concurrent.TimeUnit.SECONDS) BQConfig(io.bootique.annotation.BQConfig) ManagedDataSourceSupplier(io.bootique.jdbc.managed.ManagedDataSourceSupplier) HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariConfig(com.zaxxer.hikari.HikariConfig) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource)

Example 2 with ManagedDataSourceSupplier

use of io.bootique.jdbc.managed.ManagedDataSourceSupplier in project bootique-jdbc by bootique.

the class HikariCPInstrumentedDataSourceFactory method create.

@Override
public Optional<ManagedDataSourceSupplier> create(String dataSourceName, Injector injector) {
    if (getJdbcUrl() == null) {
        return Optional.empty();
    }
    Supplier<DataSource> startup = () -> {
        validate();
        HikariConfig hikariConfig = toConfiguration();
        HikariDataSource ds = new HikariDataSource(hikariConfig);
        this.addMetrics(ds, injector);
        this.addHealthChecks(ds, dataSourceName, injector);
        return ds;
    };
    Consumer<DataSource> shutdown = ds -> ((HikariDataSource) ds).close();
    return Optional.of(new ManagedDataSourceSupplier(getJdbcUrl(), startup, shutdown));
}
Also used : HikariCPHealthCheckGroup(io.bootique.jdbc.instrumented.hikaricp.healthcheck.HikariCPHealthCheckGroup) MetricRegistry(com.codahale.metrics.MetricRegistry) CachedGauge(com.codahale.metrics.CachedGauge) Supplier(java.util.function.Supplier) Injector(com.google.inject.Injector) Consumer(java.util.function.Consumer) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) BQConfigProperty(io.bootique.annotation.BQConfigProperty) HikariConfig(com.zaxxer.hikari.HikariConfig) HikariCPManagedDataSourceFactory(io.bootique.jdbc.hikaricp.HikariCPManagedDataSourceFactory) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource) ManagedDataSourceSupplier(io.bootique.jdbc.managed.ManagedDataSourceSupplier) Optional(java.util.Optional) Gauge(com.codahale.metrics.Gauge) HikariCPHealthCheckGroupFactory(io.bootique.jdbc.instrumented.hikaricp.healthcheck.HikariCPHealthCheckGroupFactory) BQConfig(io.bootique.annotation.BQConfig) ManagedDataSourceSupplier(io.bootique.jdbc.managed.ManagedDataSourceSupplier) HikariDataSource(com.zaxxer.hikari.HikariDataSource) HikariConfig(com.zaxxer.hikari.HikariConfig) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource)

Example 3 with ManagedDataSourceSupplier

use of io.bootique.jdbc.managed.ManagedDataSourceSupplier in project bootique-jdbc by bootique.

the class TomcatManagedDataSourceFactory method create.

@Override
public Optional<ManagedDataSourceSupplier> create(String dataSourceName, Injector injector) {
    // Once we stop supporting vars based on naming conventions, we can replace Optional<T> with just T
    if (url == null) {
        return Optional.empty();
    }
    Supplier<javax.sql.DataSource> startup = () -> {
        validate();
        PoolConfiguration poolConfig = toConfiguration();
        org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(poolConfig);
        try {
            dataSource.createPool();
        } catch (Exception e) {
            throw new RuntimeException("Error creating DataSource", e);
        }
        return dataSource;
    };
    Consumer<javax.sql.DataSource> shutdown = ds -> ((org.apache.tomcat.jdbc.pool.DataSource) ds).close();
    return Optional.of(new ManagedDataSourceSupplier(getUrl(), startup, shutdown));
}
Also used : Connection(java.sql.Connection) Properties(java.util.Properties) PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) ObjectName(javax.management.ObjectName) Supplier(java.util.function.Supplier) PoolProperties(org.apache.tomcat.jdbc.pool.PoolProperties) Injector(com.google.inject.Injector) Objects(java.util.Objects) Consumer(java.util.function.Consumer) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) BQConfigProperty(io.bootique.annotation.BQConfigProperty) ManagedDataSourceFactory(io.bootique.jdbc.managed.ManagedDataSourceFactory) ManagedDataSourceSupplier(io.bootique.jdbc.managed.ManagedDataSourceSupplier) Optional(java.util.Optional) DataSourceFactory(org.apache.tomcat.jdbc.pool.DataSourceFactory) BQConfig(io.bootique.annotation.BQConfig) PoolConfiguration(org.apache.tomcat.jdbc.pool.PoolConfiguration) ManagedDataSourceSupplier(io.bootique.jdbc.managed.ManagedDataSourceSupplier)

Example 4 with ManagedDataSourceSupplier

use of io.bootique.jdbc.managed.ManagedDataSourceSupplier in project bootique-jdbc by bootique.

the class LazyDataSourceFactory method createManagedDataSource.

protected ManagedDataSource createManagedDataSource(String name) {
    ManagedDataSourceSupplier factory = dataSourceFactories.get(name);
    if (factory == null) {
        throw new IllegalStateException("No configuration present for DataSource named '" + name + "'");
    }
    String url = factory.getUrl();
    listeners.forEach(listener -> listener.beforeStartup(name, url));
    ManagedDataSource dataSource = factory.start();
    listeners.forEach(listener -> listener.afterStartup(name, url, dataSource.getDataSource()));
    return dataSource;
}
Also used : ManagedDataSourceSupplier(io.bootique.jdbc.managed.ManagedDataSourceSupplier) ManagedDataSource(io.bootique.jdbc.managed.ManagedDataSource)

Example 5 with ManagedDataSourceSupplier

use of io.bootique.jdbc.managed.ManagedDataSourceSupplier in project bootique-jdbc by bootique.

the class LazyDataSourceFactoryTest method testAllNames.

@Test
public void testAllNames() {
    LazyDataSourceFactory f1 = new LazyDataSourceFactory(Collections.emptyMap(), Collections.emptySet());
    assertEquals(0, f1.allNames().size());
    ManagedDataSourceSupplier factory = mock(ManagedDataSourceSupplier.class);
    LazyDataSourceFactory f2 = new LazyDataSourceFactory(Collections.singletonMap("a", factory), Collections.emptySet());
    assertEquals(1, f2.allNames().size());
    assertTrue(f2.allNames().contains("a"));
}
Also used : ManagedDataSourceSupplier(io.bootique.jdbc.managed.ManagedDataSourceSupplier) Test(org.junit.Test)

Aggregations

ManagedDataSourceSupplier (io.bootique.jdbc.managed.ManagedDataSourceSupplier)5 JsonTypeName (com.fasterxml.jackson.annotation.JsonTypeName)3 Injector (com.google.inject.Injector)3 BQConfig (io.bootique.annotation.BQConfig)3 BQConfigProperty (io.bootique.annotation.BQConfigProperty)3 Optional (java.util.Optional)3 Consumer (java.util.function.Consumer)3 Supplier (java.util.function.Supplier)3 HikariConfig (com.zaxxer.hikari.HikariConfig)2 HikariDataSource (com.zaxxer.hikari.HikariDataSource)2 ManagedDataSourceFactory (io.bootique.jdbc.managed.ManagedDataSourceFactory)2 Objects (java.util.Objects)2 Properties (java.util.Properties)2 DataSource (javax.sql.DataSource)2 CachedGauge (com.codahale.metrics.CachedGauge)1 Gauge (com.codahale.metrics.Gauge)1 MetricRegistry (com.codahale.metrics.MetricRegistry)1 HikariCPManagedDataSourceFactory (io.bootique.jdbc.hikaricp.HikariCPManagedDataSourceFactory)1 HikariCPHealthCheckGroup (io.bootique.jdbc.instrumented.hikaricp.healthcheck.HikariCPHealthCheckGroup)1 HikariCPHealthCheckGroupFactory (io.bootique.jdbc.instrumented.hikaricp.healthcheck.HikariCPHealthCheckGroupFactory)1