Search in sources :

Example 36 with GenericObjectPoolConfig

use of org.apache.tomcat.dbcp.pool2.impl.GenericObjectPoolConfig in project athenz by yahoo.

the class DataSourceFactory method setupPoolConfig.

public static GenericObjectPoolConfig setupPoolConfig() {
    // setup config vars for the object pool
    // ie. min and max idle instances, and max total instances of arbitrary objects
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    // The maximum number of active connections that can be allocated from
    // this pool at the same time, or negative for no limit. Default: 8
    config.setMaxTotal(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_TOTAL, GenericObjectPoolConfig.DEFAULT_MAX_TOTAL));
    if (config.getMaxTotal() == 0) {
        // -1 means no limit
        config.setMaxTotal(-1);
    }
    // The maximum number of connections that can remain idle in the pool,
    // without extra ones being released, or negative for no limit. Default 8
    config.setMaxIdle(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_IDLE, GenericObjectPoolConfig.DEFAULT_MAX_IDLE));
    if (config.getMaxIdle() == 0) {
        // -1 means no limit
        config.setMaxIdle(-1);
    }
    // The minimum number of connections that can remain idle in the pool,
    // without extra ones being created, or zero to create none. Default 0
    config.setMinIdle(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MIN_IDLE, GenericObjectPoolConfig.DEFAULT_MIN_IDLE));
    // The maximum number of milliseconds that the pool will wait (when
    // there are no available connections) for a connection to be returned
    // before throwing an exception, or -1 to wait indefinitely. Default -1
    config.setMaxWaitMillis(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_MAX_WAIT, GenericObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS));
    // setup the configuration to cleanup idle connections
    // 
    // Minimum time an object can be idle in the pool before being eligible
    // for eviction by the idle object evictor.
    // The default value is 30 minutes (1000 * 60 * 30).
    config.setMinEvictableIdleTimeMillis(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_EVICT_IDLE_TIMEOUT, BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS));
    // Number of milliseconds to sleep between runs of idle object evictor thread.
    // Not using DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS since it is -1
    // meaning it will not run the evictor thread and instead we're using
    // the default min value for evictable idle connections (Default 30 minutes)
    config.setTimeBetweenEvictionRunsMillis(retrieveConfigSetting(ATHENZ_PROP_DBPOOL_EVICT_IDLE_INTERVAL, BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS));
    if (LOG.isDebugEnabled()) {
        LOG.debug("Config settings for idle object eviction: " + "time interval between eviction thread runs (" + config.getTimeBetweenEvictionRunsMillis() + " millis): minimum timeout for idle objects (" + config.getMinEvictableIdleTimeMillis() + " millis)");
    }
    // Validate objects by the idle object evictor. If invalid, gets dropped
    // from the pool.
    config.setTestWhileIdle(true);
    // Validate object before borrowing from pool. If invalid, gets dropped
    // from the pool and an attempt to borrow another one will occur.
    config.setTestOnBorrow(true);
    return config;
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig)

Example 37 with GenericObjectPoolConfig

use of org.apache.tomcat.dbcp.pool2.impl.GenericObjectPoolConfig in project gitblit by gitblit.

the class RedisTicketService method createPool.

private JedisPool createPool(String url) {
    JedisPool pool = null;
    if (!StringUtils.isEmpty(url)) {
        try {
            URI uri = URI.create(url);
            if (uri.getScheme() != null && uri.getScheme().equalsIgnoreCase("redis")) {
                int database = Protocol.DEFAULT_DATABASE;
                String password = null;
                if (uri.getUserInfo() != null) {
                    password = uri.getUserInfo().split(":", 2)[1];
                }
                if (uri.getPath().indexOf('/') > -1) {
                    database = Integer.parseInt(uri.getPath().split("/", 2)[1]);
                }
                pool = new JedisPool(new GenericObjectPoolConfig(), uri.getHost(), uri.getPort(), Protocol.DEFAULT_TIMEOUT, password, database);
            } else {
                pool = new JedisPool(url);
            }
        } catch (JedisException e) {
            log.error("failed to create a Redis pool!", e);
        }
    }
    return pool;
}
Also used : JedisException(redis.clients.jedis.exceptions.JedisException) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) URI(java.net.URI)

Example 38 with GenericObjectPoolConfig

use of org.apache.tomcat.dbcp.pool2.impl.GenericObjectPoolConfig in project jstorm by alibaba.

the class RedisSinkBolt method prepare.

@Override
public void prepare(Map conf, TopologyContext context, OutputCollector collector) {
    this.collector = collector;
    GenericObjectPoolConfig pconf = new GenericObjectPoolConfig();
    pconf.setMaxWaitMillis(2000);
    pconf.setMaxTotal(1000);
    pconf.setTestOnBorrow(false);
    pconf.setTestOnReturn(false);
    pconf.setTestWhileIdle(true);
    pconf.setMinEvictableIdleTimeMillis(120000);
    pconf.setTimeBetweenEvictionRunsMillis(60000);
    pconf.setNumTestsPerEvictionRun(-1);
    pool = new JedisPool(pconf, redisHost, redisPort, timeout);
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool)

Example 39 with GenericObjectPoolConfig

use of org.apache.tomcat.dbcp.pool2.impl.GenericObjectPoolConfig in project cachecloud by sohutv.

the class RedisClusterTest method getPoolConfig.

private GenericObjectPoolConfig getPoolConfig() {
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxTotal(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL * 20);
    poolConfig.setMaxIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 20);
    poolConfig.setMinIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 10);
    // JedisPool.borrowObject最大等待时间
    poolConfig.setMaxWaitMillis(1000L);
    poolConfig.setJmxNamePrefix("jedis-pool");
    poolConfig.setJmxEnabled(true);
    return poolConfig;
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig)

Example 40 with GenericObjectPoolConfig

use of org.apache.tomcat.dbcp.pool2.impl.GenericObjectPoolConfig in project cachecloud by sohutv.

the class RedisSentinelTest method testSentinelExample.

@Test
public void testSentinelExample() {
    JedisSentinelPool sentinelPool = null;
    // 使用默认配置
    //        sentinelPool = ClientBuilder.redisSentinel(appId).build();
    /**
         * 自定义配置
         */
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 3);
    poolConfig.setMinIdle(GenericObjectPoolConfig.DEFAULT_MIN_IDLE * 2);
    poolConfig.setJmxEnabled(true);
    poolConfig.setMaxWaitMillis(3000);
    sentinelPool = ClientBuilder.redisSentinel(appId).setPoolConfig(poolConfig).setConnectionTimeout(2000).setSoTimeout(1000).build();
    Jedis jedis = sentinelPool.getResource();
    jedis.set("key1", "1");
    assertEquals("2", jedis.incr("key1"));
    jedis.close();
}
Also used : Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) BaseTest(com.sohu.tv.test.base.BaseTest) Test(org.junit.Test)

Aggregations

GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)69 Test (org.junit.Test)50 Jedis (redis.clients.jedis.Jedis)26 ShardedJedis (redis.clients.jedis.ShardedJedis)26 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)26 JedisPool (redis.clients.jedis.JedisPool)17 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)12 ArrayList (java.util.ArrayList)10 JedisShardInfo (redis.clients.jedis.JedisShardInfo)10 URI (java.net.URI)7 Test (org.testng.annotations.Test)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Transaction (redis.clients.jedis.Transaction)4 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)4 BaseTest (com.sohu.tv.test.base.BaseTest)3 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)3 LoadingCacheTest (com.alicp.jetcache.LoadingCacheTest)2 RefreshCacheTest (com.alicp.jetcache.RefreshCacheTest)2 AbstractExternalCacheTest (com.alicp.jetcache.test.external.AbstractExternalCacheTest)2 URISyntaxException (java.net.URISyntaxException)2