Search in sources :

Example 36 with GenericObjectPoolConfig

use of org.apache.commons.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 37 with GenericObjectPoolConfig

use of org.apache.commons.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)

Example 38 with GenericObjectPoolConfig

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

the class RedisStandaloneTest method testStandalone.

@Test
public void testStandalone() {
    long appId = 10121;
    JedisPool jedisPool = ClientBuilder.redisStandalone(appId).setPoolConfig(new GenericObjectPoolConfig()).setTimeout(2000).build();
    Jedis jedis = jedisPool.getResource();
    for (int i = 0; i < 10; i++) {
        jedis.zadd("key-zset", i * 0.1, "value-" + i);
    }
    Set<String> result = jedis.zrevrangeByScore("key-zset", 2, 0);
    for (String s : result) {
        logger.info("{}->", s);
    }
    jedis.close();
    jedisPool.destroy();
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) BaseTest(com.sohu.tv.test.base.BaseTest) Test(org.junit.Test)

Example 39 with GenericObjectPoolConfig

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

the class ShardedJedisPoolTest method shouldReturnActiveShardsWhenOneGoesOffline.

@Test
public void shouldReturnActiveShardsWhenOneGoesOffline() {
    GenericObjectPoolConfig redisConfig = new GenericObjectPoolConfig();
    redisConfig.setTestOnBorrow(false);
    ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards);
    ShardedJedis jedis = pool.getResource();
    // fill the shards
    for (int i = 0; i < 1000; i++) {
        jedis.set("a-test-" + i, "0");
    }
    jedis.close();
    // check quantity for each shard
    Jedis j = new Jedis(shards.get(0));
    j.connect();
    Long c1 = j.dbSize();
    j.disconnect();
    j = new Jedis(shards.get(1));
    j.connect();
    Long c2 = j.dbSize();
    j.disconnect();
    // shutdown shard 2 and check thay the pool returns an instance with c1
    // items on one shard
    // alter shard 1 and recreate pool
    pool.destroy();
    shards.set(1, new JedisShardInfo("localhost", 1234));
    pool = new ShardedJedisPool(redisConfig, shards);
    jedis = pool.getResource();
    Long actual = Long.valueOf(0);
    Long fails = Long.valueOf(0);
    for (int i = 0; i < 1000; i++) {
        try {
            jedis.get("a-test-" + i);
            actual++;
        } catch (RuntimeException e) {
            fails++;
        }
    }
    jedis.close();
    pool.destroy();
    assertEquals(actual, c1);
    assertEquals(fails, c2);
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis) Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ShardedJedis(redis.clients.jedis.ShardedJedis) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) JedisShardInfo(redis.clients.jedis.JedisShardInfo) Test(org.junit.Test)

Example 40 with GenericObjectPoolConfig

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

the class ShardedJedisPoolTest method checkPoolRepairedWhenJedisIsBroken.

@Test
public void checkPoolRepairedWhenJedisIsBroken() {
    ShardedJedisPool pool = new ShardedJedisPool(new GenericObjectPoolConfig(), shards);
    ShardedJedis jedis = pool.getResource();
    jedis.disconnect();
    jedis.close();
    jedis = pool.getResource();
    jedis.incr("foo");
    jedis.close();
    pool.destroy();
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ShardedJedis(redis.clients.jedis.ShardedJedis) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) Test(org.junit.Test)

Aggregations

GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)62 Test (org.junit.Test)50 ShardedJedis (redis.clients.jedis.ShardedJedis)26 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)26 Jedis (redis.clients.jedis.Jedis)25 JedisPool (redis.clients.jedis.JedisPool)16 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)12 ArrayList (java.util.ArrayList)10 JedisShardInfo (redis.clients.jedis.JedisShardInfo)10 URI (java.net.URI)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Transaction (redis.clients.jedis.Transaction)4 BaseTest (com.sohu.tv.test.base.BaseTest)3 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)3 JedisException (redis.clients.jedis.exceptions.JedisException)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 HashSet (java.util.HashSet)2