Example usage for org.apache.commons.pool2.impl GenericObjectPool setAbandonedConfig

List of usage examples for org.apache.commons.pool2.impl GenericObjectPool setAbandonedConfig

Introduction

In this page you can find the example usage for org.apache.commons.pool2.impl GenericObjectPool setAbandonedConfig.

Prototype

public void setAbandonedConfig(AbandonedConfig abandonedConfig) throws IllegalArgumentException 

Source Link

Document

Sets the abandoned object removal configuration.

Usage

From source file:com.heliosapm.streams.collector.ds.pool.PoolConfig.java

/**
 * Creates and deploys an ObjectPool based on the passed config props
 * @param p The configuration properties
 * @return the created GenericObjectPool
 *//*w w w. j  av a  2 s  .  co m*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private static GenericObjectPool<?> deployPool(final Properties p) {
    final String factoryName = (String) p.remove(POOLED_OBJECT_FACTORY_KEY);
    final String poolName = p.getProperty(NAME.name().toLowerCase());
    if (poolName == null || poolName.trim().isEmpty())
        throw new RuntimeException("Pool was not assigned a name");
    if (factoryName == null || factoryName.trim().isEmpty())
        throw new RuntimeException("No pooled object factory defined");
    final GenericObjectPoolConfig cfg = DEFAULT_CONFIG.clone();
    try {
        final Class<PooledObjectFactoryBuilder<?>> clazz = loadFactoryClass(factoryName);
        final Constructor<PooledObjectFactoryBuilder<?>> ctor = clazz.getDeclaredConstructor(Properties.class);
        final PooledObjectFactoryBuilder<?> factory = ctor.newInstance(p);
        for (final String key : p.stringPropertyNames()) {
            if (isPoolConfig(key)) {
                final PoolConfig pc = decode(key);
                pc.apply(cfg, p.get(key));
            }
        }
        final PooledObjectFactory<?> pooledObjectFactory = factory.factory();
        GenericObjectPool<?> pool = new GenericObjectPool(pooledObjectFactory, cfg);
        pool.setSwallowedExceptionListener(EX_LISTENER);
        if (factory instanceof PoolAwareFactory) {
            ((PoolAwareFactory) factory).setPool(pool);
        }
        GlobalCacheService.getInstance().put("pool/" + poolName, pool);
        pool.setAbandonedConfig(Abandoned.create(p));
        pool.preparePool();
        return pool;
    } catch (Exception ex) {
        throw new RuntimeException("Failed to create GenericObjectPool from properties [" + p + "]", ex);
    }
}

From source file:io.seldon.dbcp.DbcpFactory.java

private void createDbcp(DbcpConfig conf) {
    if (!dataSources.containsKey(conf.name)) {
        try {/* w ww  . j  a va  2s.co  m*/

            Class.forName(conf.driverClassName);

            DriverManagerConnectionFactory cf = new DriverManagerConnectionFactory(conf.jdbc, conf.user,
                    conf.password);

            PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, null);
            pcf.setValidationQuery(conf.validationQuery);
            //, pool, null, conf.validationQuery, false, true,abandondedConfig);

            logger.info("Creating pool " + conf.toString());
            // create a generic pool
            GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(pcf);
            pool.setMaxTotal(conf.maxTotal);
            pool.setMaxIdle(conf.maxIdle);
            pool.setMinIdle(conf.minIdle);
            pool.setMaxWaitMillis(conf.maxWait);
            pool.setTimeBetweenEvictionRunsMillis(conf.timeBetweenEvictionRunsMillis);
            pool.setMinEvictableIdleTimeMillis(conf.minEvictableIdleTimeMillis);
            pool.setTestWhileIdle(conf.testWhileIdle);
            pool.setTestOnBorrow(conf.testOnBorrow);

            AbandonedConfig abandonedConfig = new AbandonedConfig();
            abandonedConfig.setRemoveAbandonedOnMaintenance(conf.removeAbanadoned);
            abandonedConfig.setRemoveAbandonedTimeout(conf.removeAbandonedTimeout);
            abandonedConfig.setLogAbandoned(conf.logAbandonded);

            pool.setAbandonedConfig(abandonedConfig);

            pcf.setPool(pool);
            DataSource ds = new PoolingDataSource(pool);
            dataSources.put(conf.name, ds);

        } catch (ClassNotFoundException e) {
            logger.error(
                    "Failed to create datasource for " + conf.name + " with class " + conf.driverClassName);
        }

    } else {
        logger.error("Pool " + conf.name + " already exists. Can't change existing datasource at present.");
    }
}