Search in sources :

Example 56 with GenericObjectPoolConfig

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

the class ShardedJedisPoolTest method startWithUrlString.

@Test
public void startWithUrlString() {
    Jedis j = new Jedis("localhost", 6380);
    j.auth("foobared");
    j.set("foo", "bar");
    j = new Jedis("localhost", 6379);
    j.auth("foobared");
    j.set("foo", "bar");
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add(new JedisShardInfo("redis://:foobared@localhost:6380"));
    shards.add(new JedisShardInfo("redis://:foobared@localhost:6379"));
    GenericObjectPoolConfig redisConfig = new GenericObjectPoolConfig();
    ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards);
    Jedis[] jedises = pool.getResource().getAllShards().toArray(new Jedis[2]);
    Jedis jedis = jedises[0];
    assertEquals("PONG", jedis.ping());
    assertEquals("bar", jedis.get("foo"));
    jedis = jedises[1];
    assertEquals("PONG", jedis.ping());
    assertEquals("bar", jedis.get("foo"));
}
Also used : ShardedJedis(redis.clients.jedis.ShardedJedis) Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) ArrayList(java.util.ArrayList) JedisShardInfo(redis.clients.jedis.JedisShardInfo) Test(org.junit.Test)

Example 57 with GenericObjectPoolConfig

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

the class PoolBenchmark method withPool.

private static void withPool() throws Exception {
    final JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, "foobared");
    List<Thread> tds = new ArrayList<Thread>();
    final AtomicInteger ind = new AtomicInteger();
    for (int i = 0; i < 50; i++) {
        Thread hj = new Thread(new Runnable() {

            public void run() {
                for (int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS; ) {
                    try {
                        Jedis j = pool.getResource();
                        final String key = "foo" + i;
                        j.set(key, key);
                        j.get(key);
                        j.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        tds.add(hj);
        hj.start();
    }
    for (Thread t : tds) t.join();
    pool.destroy();
}
Also used : Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) JedisPool(redis.clients.jedis.JedisPool)

Example 58 with GenericObjectPoolConfig

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

the class ShardedJedisPoolTest method checkResourceIsCloseable.

@Test
public void checkResourceIsCloseable() throws URISyntaxException {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
    shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6380")));
    shards.add(new JedisShardInfo(new URI("redis://:foobared@localhost:6379")));
    ShardedJedisPool pool = new ShardedJedisPool(config, shards);
    ShardedJedis jedis = pool.getResource();
    try {
        jedis.set("hello", "jedis");
    } finally {
        jedis.close();
    }
    ShardedJedis jedis2 = pool.getResource();
    try {
        assertEquals(jedis, jedis2);
    } finally {
        jedis2.close();
    }
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ShardedJedis(redis.clients.jedis.ShardedJedis) ShardedJedisPool(redis.clients.jedis.ShardedJedisPool) ArrayList(java.util.ArrayList) JedisShardInfo(redis.clients.jedis.JedisShardInfo) URI(java.net.URI) Test(org.junit.Test)

Example 59 with GenericObjectPoolConfig

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

the class JedisSentinelPoolTest method ensureSafeTwiceFailover.

@Test
public void ensureSafeTwiceFailover() throws InterruptedException {
    JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, new GenericObjectPoolConfig(), 1000, "foobared", 2);
    forceFailover(pool);
    // after failover sentinel needs a bit of time to stabilize before a new
    // failover
    Thread.sleep(100);
    forceFailover(pool);
// you can test failover as much as possible
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) Test(org.junit.Test)

Example 60 with GenericObjectPoolConfig

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

the class JedisSentinelPoolTest method checkResourceIsCloseable.

@Test
public void checkResourceIsCloseable() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2);
    Jedis jedis = pool.getResource();
    try {
        jedis.set("hello", "jedis");
    } finally {
        jedis.close();
    }
    Jedis jedis2 = pool.getResource();
    try {
        assertEquals(jedis, jedis2);
    } finally {
        jedis2.close();
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) 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