Search in sources :

Example 36 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project cas by apereo.

the class MemcachedPooledClientConnectionFactory method getObjectPool.

/**
 * Gets object pool.
 *
 * @return the object pool
 */
public ObjectPool<MemcachedClientIF> getObjectPool() {
    val pool = new GenericObjectPool<>(this);
    pool.setMaxIdle(memcachedProperties.getMaxIdle());
    pool.setMinIdle(memcachedProperties.getMinIdle());
    pool.setMaxTotal(memcachedProperties.getMaxTotal());
    return pool;
}
Also used : lombok.val(lombok.val) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Example 37 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project cas by apereo.

the class MemcachedMonitorConfiguration method memcachedHealthClientPool.

@Bean
@RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)
@ConditionalOnMissingBean(name = "memcachedHealthClientPool")
public ObjectPool<MemcachedClientIF> memcachedHealthClientPool(@Qualifier("memcachedMonitorTranscoder") final Transcoder memcachedMonitorTranscoder, final CasConfigurationProperties casProperties) {
    val memcached = casProperties.getMonitor().getMemcached();
    val factory = new MemcachedPooledClientConnectionFactory(memcached, memcachedMonitorTranscoder);
    return new GenericObjectPool<>(factory);
}
Also used : lombok.val(lombok.val) MemcachedPooledClientConnectionFactory(org.apereo.cas.memcached.MemcachedPooledClientConnectionFactory) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 38 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project ddf by codice.

the class SslLdapLoginModule method installLdapConnectionPool.

private void installLdapConnectionPool(String connectionPoolId) {
    BundleContext bundleContext = getContext();
    if (bundleContext != null) {
        try {
            Collection<ServiceReference<GenericObjectPool>> serviceReferences = bundleContext.getServiceReferences(GenericObjectPool.class, String.format("(id=%s)", connectionPoolId));
            ServiceReference<GenericObjectPool> serviceReference = serviceReferences.stream().findFirst().orElseThrow(() -> new IllegalStateException("No LDAPConnectionPool service found with id:" + connectionPoolId));
            ldapConnectionPool = bundleContext.getService(serviceReference);
        } catch (InvalidSyntaxException | IllegalStateException e) {
            LOGGER.error("Unable to get LDAP Connection pool. LDAP log in will not be possible.", e);
        }
    }
}
Also used : InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 39 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project kork by spinnaker.

the class JedisHealthIndicatorFactory method build.

public static HealthIndicator build(Pool<Jedis> jedisPool) {
    try {
        final Pool<Jedis> src = jedisPool;
        final Field poolAccess = Pool.class.getDeclaredField("internalPool");
        poolAccess.setAccessible(true);
        GenericObjectPool<Jedis> internal = (GenericObjectPool<Jedis>) poolAccess.get(jedisPool);
        return () -> {
            Jedis jedis = null;
            Health.Builder health;
            try {
                jedis = src.getResource();
                if ("PONG".equals(jedis.ping())) {
                    health = Health.up();
                } else {
                    health = Health.down();
                }
            } catch (Exception ex) {
                health = Health.down(ex);
            } finally {
                if (jedis != null)
                    jedis.close();
            }
            health.withDetail("maxIdle", internal.getMaxIdle());
            health.withDetail("minIdle", internal.getMinIdle());
            health.withDetail("numActive", internal.getNumActive());
            health.withDetail("numIdle", internal.getNumIdle());
            health.withDetail("numWaiters", internal.getNumWaiters());
            return health.build();
        };
    } catch (IllegalAccessException | NoSuchFieldException e) {
        throw new BeanCreationException("Error creating Redis health indicator", e);
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Field(java.lang.reflect.Field) BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) BeanCreationException(org.springframework.beans.factory.BeanCreationException)

Example 40 with GenericObjectPool

use of com.frameworkset.commons.pool2.impl.GenericObjectPool in project atlasdb by palantir.

the class CassandraClientPoolingContainer method createClientPool.

/**
 * Pool size:
 *    Always keep {@link CassandraKeyValueServiceConfig#poolSize()} connections around, per host. Allow bursting
 *    up to {@link CassandraKeyValueServiceConfig#maxConnectionBurstSize()} connections per host under load.
 *
 * Borrowing from pool:
 *    On borrow, check if the connection is actually open. If it is not,
 *       immediately discard this connection from the pool, and try to take another.
 *    Borrow attempts against a fully in-use pool immediately throw a NoSuchElementException.
 *       {@code CassandraClientPool} when it sees this will:
 *          Follow an exponential backoff as a method of back pressure.
 *          Try 3 times against this host, and then give up and try against different hosts 3 additional times.
 *
 * In an asynchronous thread (using default values):
 *    Every 20-30 seconds, examine approximately a tenth of the connections in pool.
 *    Discard any connections in this tenth of the pool whose TCP connections are closed.
 *    Discard any connections in this tenth of the pool that have been idle for more than 10 minutes,
 *       while still keeping a minimum number of idle connections around for fast borrows.
 */
private GenericObjectPool<CassandraClient> createClientPool() {
    CassandraClientConfig clientConfig = CassandraClientConfig.of(config);
    CassandraClientFactory cassandraClientFactory = new CassandraClientFactory(metricsManager, proxy, clientConfig);
    GenericObjectPoolConfig<CassandraClient> poolConfig = new GenericObjectPoolConfig<>();
    poolConfig.setMinIdle(config.poolSize());
    poolConfig.setMaxIdle(config.maxConnectionBurstSize());
    poolConfig.setMaxTotal(config.maxConnectionBurstSize());
    // immediately throw when we try and borrow from a full pool; dealt with at higher level
    poolConfig.setBlockWhenExhausted(false);
    // this test is free/just checks a boolean and does not block; borrow is still fast
    poolConfig.setTestOnBorrow(true);
    poolConfig.setSoftMinEvictableIdleTimeMillis(TimeUnit.MILLISECONDS.convert(Duration.ofSeconds(config.idleConnectionTimeoutSeconds())));
    poolConfig.setMinEvictableIdleTimeMillis(Long.MAX_VALUE);
    // the randomness here is to prevent all of the pools for all of the hosts
    // evicting all at at once, which isn't great for C*.
    int timeBetweenEvictionsSeconds = config.timeBetweenConnectionEvictionRunsSeconds();
    int delta = ThreadLocalRandom.current().nextInt(Math.min(timeBetweenEvictionsSeconds / 2, 10));
    poolConfig.setTimeBetweenEvictionRunsMillis(TimeUnit.MILLISECONDS.convert(Duration.ofSeconds(timeBetweenEvictionsSeconds + delta)));
    poolConfig.setNumTestsPerEvictionRun(-(int) (1.0 / config.proportionConnectionsToCheckPerEvictionRun()));
    poolConfig.setTestWhileIdle(true);
    poolConfig.setJmxNamePrefix(proxy.getHostString());
    poolConfig.setEvictionPolicy(new DefaultEvictionPolicy<>());
    GenericObjectPool<CassandraClient> pool = new GenericObjectPool<>(cassandraClientFactory, poolConfig);
    pool.setSwallowedExceptionListener(exception -> log.info("Swallowed exception within object pool", exception));
    registerMetrics(pool);
    log.info("Creating a Cassandra client pool for {} with the configuration {}", SafeArg.of("cassandraHost", cassandraServer.cassandraHostName()), SafeArg.of("proxy", proxy), SafeArg.of("poolConfig", poolConfig));
    return pool;
}
Also used : CassandraClientConfig(com.palantir.atlasdb.keyvalue.cassandra.CassandraClientFactory.CassandraClientConfig) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool)

Aggregations

GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)79 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)32 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)27 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)23 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)19 DriverManagerConnectionFactory (org.apache.commons.dbcp2.DriverManagerConnectionFactory)16 Test (org.junit.jupiter.api.Test)13 Properties (java.util.Properties)11 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)9 SQLException (java.sql.SQLException)8 PoolingDriver (org.apache.commons.dbcp2.PoolingDriver)8 Connection (java.sql.Connection)7 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)5 Bean (org.springframework.context.annotation.Bean)5 ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)4 IOException (java.io.IOException)3 PooledObject (org.apache.commons.pool2.PooledObject)3 Test (org.junit.Test)3 TimeInterval (com.adaptris.util.TimeInterval)2 ThresholdedRandomCutForestMapper (com.amazon.randomcutforest.parkservices.state.ThresholdedRandomCutForestMapper)2