Java Code Examples for org.apache.commons.pool.impl.GenericObjectPool#Config

The following examples show how to use org.apache.commons.pool.impl.GenericObjectPool#Config . 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: PooledConfiguration.java    From navi-pbrpc with Apache License 2.0 6 votes vote down vote up
/**
 * 获取对象池配置
 * 
 * @return
 */
public GenericObjectPool.Config getPoolConfig() {
    GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
    // maxIdle为负数时,不对pool size大小做限制,此处做限制,防止保持过多空闲redis连接
    if (this.maxIdle >= 0) {
        poolConfig.maxIdle = this.maxIdle;
    }
    poolConfig.maxWait = this.maxWait;
    if (this.whenExhaustedAction >= 0 && this.whenExhaustedAction < 3) {
        poolConfig.whenExhaustedAction = this.whenExhaustedAction;
    }
    poolConfig.testOnBorrow = this.testOnBorrow;
    poolConfig.minIdle = this.minIdle;
    poolConfig.maxActive = this.maxActive;
    poolConfig.testOnReturn = this.testOnReturn;
    poolConfig.testWhileIdle = this.testWhileIdle;
    poolConfig.timeBetweenEvictionRunsMillis = this.timeBetweenEvictionRunsMillis;
    poolConfig.numTestsPerEvictionRun = this.numTestsPerEvictionRun;
    poolConfig.minEvictableIdleTimeMillis = this.minEvictableIdleTimeMillis;
    poolConfig.softMinEvictableIdleTimeMillis = this.softMinEvictableIdleTimeMillis;
    poolConfig.lifo = this.lifo;
    return poolConfig;
}
 
Example 2
Source File: RedisClient.java    From apollo with GNU General Public License v2.0 6 votes vote down vote up
private GenericObjectPool.Config getPoolConfig() {
    GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
    // maxIdle为负数时,不对pool size大小做限制,此处做限制,防止保持过多空闲redis连接
    if (this.maxIdle >= 0) {
        poolConfig.maxIdle = this.maxIdle;
    }
    poolConfig.maxWait = this.maxWait;
    if (this.whenExhaustedAction >= 0 && this.whenExhaustedAction < 3) {
        poolConfig.whenExhaustedAction = this.whenExhaustedAction;
    }
    poolConfig.testOnBorrow = this.testOnBorrow;
    poolConfig.minIdle = this.minIdle;
    poolConfig.maxActive = this.maxActive;
    poolConfig.testOnReturn = this.testOnReturn;
    poolConfig.testWhileIdle = this.testWhileIdle;
    poolConfig.timeBetweenEvictionRunsMillis = this.timeBetweenEvictionRunsMillis;
    poolConfig.numTestsPerEvictionRun = this.numTestsPerEvictionRun;
    poolConfig.minEvictableIdleTimeMillis = this.minEvictableIdleTimeMillis;
    poolConfig.softMinEvictableIdleTimeMillis = this.softMinEvictableIdleTimeMillis;
    poolConfig.lifo = this.lifo;
    return poolConfig;
}
 
Example 3
Source File: QueryIterator.java    From datawave with Apache License 2.0 5 votes vote down vote up
private GenericObjectPool.Config createIvaratorSourcePoolConfig(int maxIvaratorSources) {
    GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
    poolConfig.maxActive = maxIvaratorSources;
    poolConfig.maxIdle = maxIvaratorSources;
    poolConfig.minIdle = 0;
    poolConfig.whenExhaustedAction = WHEN_EXHAUSTED_BLOCK;
    return poolConfig;
}
 
Example 4
Source File: IvaratorReloadTest.java    From datawave with Apache License 2.0 5 votes vote down vote up
public static GenericObjectPool.Config createIvaratorSourcePoolConfig(int maxIvaratorSources) {
    GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
    poolConfig.maxActive = maxIvaratorSources;
    poolConfig.maxIdle = maxIvaratorSources;
    poolConfig.minIdle = 0;
    poolConfig.whenExhaustedAction = WHEN_EXHAUSTED_BLOCK;
    return poolConfig;
}
 
Example 5
Source File: DataSourceProviderDbcpImpl.java    From ralasafe with MIT License 5 votes vote down vote up
public void setup( Properties prop ) {
	this.prop=prop;

       GenericObjectPool.Config conPoolCfg = new GenericObjectPool.Config();
       conPoolCfg.maxActive = Integer.parseInt( prop.getProperty( "connectionPoolMaxSize", "15" ) );
       conPoolCfg.maxIdle = Integer.parseInt( prop.getProperty( "connectionPoolMaxIdle", "8" ) );
       conPoolCfg.maxWait = Integer.parseInt( prop.getProperty( "connectionPoolMaxWait", "60000" ) );
       conPoolCfg.minIdle = Integer.parseInt( prop.getProperty( "connectionPoolMinSize", "2" ) );


       ObjectPool connectionPool = new GenericObjectPool( null, conPoolCfg );
       try {
		Class.forName( prop.getProperty( "jdbcDriver" ) );
	} catch( ClassNotFoundException e ) {
		e.printStackTrace();
		throw new RuntimeException();
	}

       ConnectionFactory connectionFactory = new
           DriverManagerConnectionFactory( prop.getProperty( "jdbcUrl" ),
                                          prop.getProperty( "jdbcUser" ), 
                                          prop.getProperty( "jdbcPassword" ) );


       new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);

       PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
       
       ds = dataSource;
}
 
Example 6
Source File: RedisRegistry.java    From dubbox-hystrix with Apache License 2.0 4 votes vote down vote up
public RedisRegistry(URL url) {
    super(url);
    if (url.isAnyHost()) {
        throw new IllegalStateException("registry address == null");
    }
    GenericObjectPool.Config config = new GenericObjectPool.Config();
    config.testOnBorrow = url.getParameter("test.on.borrow", true);
    config.testOnReturn = url.getParameter("test.on.return", false);
    config.testWhileIdle = url.getParameter("test.while.idle", false);
    if (url.getParameter("max.idle", 0) > 0)
        config.maxIdle = url.getParameter("max.idle", 0);
    if (url.getParameter("min.idle", 0) > 0)
        config.minIdle = url.getParameter("min.idle", 0);
    if (url.getParameter("max.active", 0) > 0)
        config.maxActive = url.getParameter("max.active", 0);
    if (url.getParameter("max.wait", url.getParameter("timeout", 0)) > 0)
        config.maxWait = url.getParameter("max.wait", url.getParameter("timeout", 0));
    if (url.getParameter("num.tests.per.eviction.run", 0) > 0)
        config.numTestsPerEvictionRun = url.getParameter("num.tests.per.eviction.run", 0);
    if (url.getParameter("time.between.eviction.runs.millis", 0) > 0)
        config.timeBetweenEvictionRunsMillis = url.getParameter("time.between.eviction.runs.millis", 0);
    if (url.getParameter("min.evictable.idle.time.millis", 0) > 0)
        config.minEvictableIdleTimeMillis = url.getParameter("min.evictable.idle.time.millis", 0);

    String cluster = url.getParameter("cluster", "failover");
    if (! "failover".equals(cluster) && ! "replicate".equals(cluster)) {
        throw new IllegalArgumentException("Unsupported redis cluster: " + cluster + ". The redis cluster only supported failover or replicate.");
    }
    replicate = "replicate".equals(cluster);

    List<String> addresses = new ArrayList<String>();
    addresses.add(url.getAddress());
    String[] backups = url.getParameter(Constants.BACKUP_KEY, new String[0]);
    if (backups != null && backups.length > 0) {
        addresses.addAll(Arrays.asList(backups));
    }

    // 增加Redis密码支持
    String password = url.getPassword();
    for (String address : addresses) {
        int i = address.indexOf(':');
        String host;
        int port;
        if (i > 0) {
            host = address.substring(0, i);
            port = Integer.parseInt(address.substring(i + 1));
        } else {
            host = address;
            port = DEFAULT_REDIS_PORT;
        }
        if (StringUtils.isEmpty(password)) {
            this.jedisPools.put(address, new JedisPool(config, host, port,
                    url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)));
        } else {
            // 使用密码连接。  此处要求备用redis与主要redis使用相同的密码
            this.jedisPools.put(address, new JedisPool(config, host, port,
                    url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT), password));
        }
    }

    this.reconnectPeriod = url.getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD);
    String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
    if (! group.startsWith(Constants.PATH_SEPARATOR)) {
        group = Constants.PATH_SEPARATOR + group;
    }
    if (! group.endsWith(Constants.PATH_SEPARATOR)) {
        group = group + Constants.PATH_SEPARATOR;
    }
    this.root = group;

    this.expirePeriod = url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_SESSION_TIMEOUT);
    this.expireFuture = expireExecutor.scheduleWithFixedDelay(new Runnable() {
        public void run() {
            try {
                deferExpired(); // 延长过期时间
            } catch (Throwable t) { // 防御性容错
                logger.error("Unexpected exception occur at defer expire time, cause: " + t.getMessage(), t);
            }
        }
    }, expirePeriod / 2, expirePeriod / 2, TimeUnit.MILLISECONDS);
}
 
Example 7
Source File: RedisRegistry.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public RedisRegistry(URL url) {
    super(url);
    if (url.isAnyHost()) {
        throw new IllegalStateException("registry address == null");
    }
    GenericObjectPool.Config config = new GenericObjectPool.Config();
    config.testOnBorrow = url.getParameter("test.on.borrow", true);
    config.testOnReturn = url.getParameter("test.on.return", false);
    config.testWhileIdle = url.getParameter("test.while.idle", false);
    if (url.getParameter("max.idle", 0) > 0)
        config.maxIdle = url.getParameter("max.idle", 0);
    if (url.getParameter("min.idle", 0) > 0)
        config.minIdle = url.getParameter("min.idle", 0);
    if (url.getParameter("max.active", 0) > 0)
        config.maxActive = url.getParameter("max.active", 0);
    if (url.getParameter("max.wait", url.getParameter("timeout", 0)) > 0)
        config.maxWait = url.getParameter("max.wait", url.getParameter("timeout", 0));
    if (url.getParameter("num.tests.per.eviction.run", 0) > 0)
        config.numTestsPerEvictionRun = url.getParameter("num.tests.per.eviction.run", 0);
    if (url.getParameter("time.between.eviction.runs.millis", 0) > 0)
        config.timeBetweenEvictionRunsMillis = url.getParameter("time.between.eviction.runs.millis", 0);
    if (url.getParameter("min.evictable.idle.time.millis", 0) > 0)
        config.minEvictableIdleTimeMillis = url.getParameter("min.evictable.idle.time.millis", 0);

    String cluster = url.getParameter("cluster", "failover");
    if (! "failover".equals(cluster) && ! "replicate".equals(cluster)) {
        throw new IllegalArgumentException("Unsupported redis cluster: " + cluster + ". The redis cluster only supported failover or replicate.");
    }
    replicate = "replicate".equals(cluster);

    List<String> addresses = new ArrayList<String>();
    addresses.add(url.getAddress());
    String[] backups = url.getParameter(Constants.BACKUP_KEY, new String[0]);
    if (backups != null && backups.length > 0) {
        addresses.addAll(Arrays.asList(backups));
    }

    // 增加Redis密码支持
    String password = url.getPassword();
    for (String address : addresses) {
        int i = address.indexOf(':');
        String host;
        int port;
        if (i > 0) {
            host = address.substring(0, i);
            port = Integer.parseInt(address.substring(i + 1));
        } else {
            host = address;
            port = DEFAULT_REDIS_PORT;
        }
        if (StringUtils.isEmpty(password)) {
            this.jedisPools.put(address, new JedisPool(config, host, port,
                    url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)));
        } else {
            // 使用密码连接。  此处要求备用redis与主要redis使用相同的密码
            this.jedisPools.put(address, new JedisPool(config, host, port,
                    url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT), password));
        }
    }

    this.reconnectPeriod = url.getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD);
    String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
    if (! group.startsWith(Constants.PATH_SEPARATOR)) {
        group = Constants.PATH_SEPARATOR + group;
    }
    if (! group.endsWith(Constants.PATH_SEPARATOR)) {
        group = group + Constants.PATH_SEPARATOR;
    }
    this.root = group;

    this.expirePeriod = url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_SESSION_TIMEOUT);
    this.expireFuture = expireExecutor.scheduleWithFixedDelay(new Runnable() {
        public void run() {
            try {
                deferExpired(); // 延长过期时间
            } catch (Throwable t) { // 防御性容错
                logger.error("Unexpected exception occur at defer expire time, cause: " + t.getMessage(), t);
            }
        }
    }, expirePeriod / 2, expirePeriod / 2, TimeUnit.MILLISECONDS);
}
 
Example 8
Source File: BlockingIOPooledPbrpcClient.java    From navi-pbrpc with Apache License 2.0 4 votes vote down vote up
public void setPooledConfig(GenericObjectPool.Config pooledConfig) {
    this.pooledConfig = pooledConfig;
}
 
Example 9
Source File: RedisRegistry.java    From dubbox with Apache License 2.0 4 votes vote down vote up
public RedisRegistry(URL url) {
    super(url);
    if (url.isAnyHost()) {
		throw new IllegalStateException("registry address == null");
	}
    GenericObjectPool.Config config = new GenericObjectPool.Config();
    config.testOnBorrow = url.getParameter("test.on.borrow", true);
    config.testOnReturn = url.getParameter("test.on.return", false);
    config.testWhileIdle = url.getParameter("test.while.idle", false);
    if (url.getParameter("max.idle", 0) > 0)
        config.maxIdle = url.getParameter("max.idle", 0);
    if (url.getParameter("min.idle", 0) > 0)
        config.minIdle = url.getParameter("min.idle", 0);
    if (url.getParameter("max.active", 0) > 0)
        config.maxActive = url.getParameter("max.active", 0);
    if (url.getParameter("max.wait", url.getParameter("timeout", 0)) > 0)
        config.maxWait = url.getParameter("max.wait", url.getParameter("timeout", 0));
    if (url.getParameter("num.tests.per.eviction.run", 0) > 0)
        config.numTestsPerEvictionRun = url.getParameter("num.tests.per.eviction.run", 0);
    if (url.getParameter("time.between.eviction.runs.millis", 0) > 0)
        config.timeBetweenEvictionRunsMillis = url.getParameter("time.between.eviction.runs.millis", 0);
    if (url.getParameter("min.evictable.idle.time.millis", 0) > 0)
        config.minEvictableIdleTimeMillis = url.getParameter("min.evictable.idle.time.millis", 0);
    
    String cluster = url.getParameter("cluster", "failover");
    if (! "failover".equals(cluster) && ! "replicate".equals(cluster)) {
    	throw new IllegalArgumentException("Unsupported redis cluster: " + cluster + ". The redis cluster only supported failover or replicate.");
    }
    replicate = "replicate".equals(cluster);
    
    List<String> addresses = new ArrayList<String>();
    addresses.add(url.getAddress());
    String[] backups = url.getParameter(Constants.BACKUP_KEY, new String[0]);
    if (backups != null && backups.length > 0) {
        addresses.addAll(Arrays.asList(backups));
    }
    for (String address : addresses) {
        int i = address.indexOf(':');
        String host;
        int port;
        if (i > 0) {
            host = address.substring(0, i);
            port = Integer.parseInt(address.substring(i + 1));
        } else {
            host = address;
            port = DEFAULT_REDIS_PORT;
        }
        this.jedisPools.put(address, new JedisPool(config, host, port, 
                url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)));
    }
    
    this.reconnectPeriod = url.getParameter(Constants.REGISTRY_RECONNECT_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RECONNECT_PERIOD);
    String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
    if (! group.startsWith(Constants.PATH_SEPARATOR)) {
        group = Constants.PATH_SEPARATOR + group;
    }
    if (! group.endsWith(Constants.PATH_SEPARATOR)) {
        group = group + Constants.PATH_SEPARATOR;
    }
    this.root = group;
    
    this.expirePeriod = url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_SESSION_TIMEOUT);
    this.expireFuture = expireExecutor.scheduleWithFixedDelay(new Runnable() {
        public void run() {
            try {
                deferExpired(); // 延长过期时间
            } catch (Throwable t) { // 防御性容错
                logger.error("Unexpected exception occur at defer expire time, cause: " + t.getMessage(), t);
            }
        }
    }, expirePeriod / 2, expirePeriod / 2, TimeUnit.MILLISECONDS);
}
 
Example 10
Source File: GMConnectionPoolConfig.java    From gm4java with Apache License 2.0 4 votes vote down vote up
GenericObjectPool.Config getConfig() {
    return config;
}
 
Example 11
Source File: GMConnectionPool.java    From gm4java with Apache License 2.0 4 votes vote down vote up
private static GenericObjectPool.Config getParentConfig(GMConnectionPoolConfig config) {
    if (config == null) throw new NullPointerException("config");
    return config.getConfig();
}
 
Example 12
Source File: Pool.java    From navi-pbrpc with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new instance of Pool.
 * 
 * @param poolConfig
 * @param factory
 */
public Pool(final GenericObjectPool.Config poolConfig, PoolableObjectFactory factory) {
    this.internalPool = new GenericObjectPool(factory, poolConfig);
}