Search in sources :

Example 66 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.pool2.impl.GenericObjectPoolConfig in project jedis by xetorthio.

the class ShardedJedisPoolTest method startWithUrl.

@Test
public void startWithUrl() throws URISyntaxException {
    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(new URI("redis://:foobared@localhost:6380")));
    shards.add(new JedisShardInfo(new URI("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) URI(java.net.URI) Test(org.junit.Test)

Example 67 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.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 68 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.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 69 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.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 70 with GenericObjectPoolConfig

use of org.datanucleus.store.rdbms.datasource.dbcp2.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)

Aggregations

GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)127 Test (org.junit.Test)77 Jedis (redis.clients.jedis.Jedis)41 ShardedJedis (redis.clients.jedis.ShardedJedis)40 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)40 JedisPool (redis.clients.jedis.JedisPool)31 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)19 ArrayList (java.util.ArrayList)15 JedisShardInfo (redis.clients.jedis.JedisShardInfo)15 URI (java.net.URI)11 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)9 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 BaseTest (com.sohu.tv.test.base.BaseTest)6 IOException (java.io.IOException)6 Transaction (redis.clients.jedis.Transaction)6 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)5 PooledObject (org.apache.commons.pool2.PooledObject)5 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)5 Test (org.testng.annotations.Test)5