Search in sources :

Example 16 with GenericObjectPoolConfig

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

the class JedisSentinelPoolTest method returnResourceShouldResetState.

@Test
public void returnResourceShouldResetState() {
    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();
    Jedis jedis2 = null;
    try {
        jedis.set("hello", "jedis");
        Transaction t = jedis.multi();
        t.set("hello", "world");
        jedis.close();
        jedis2 = pool.getResource();
        assertTrue(jedis == jedis2);
        assertEquals("jedis", jedis2.get("hello"));
    } catch (JedisConnectionException e) {
        if (jedis2 != null) {
            jedis2 = null;
        }
    } finally {
        jedis2.close();
        pool.destroy();
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) Test(org.junit.Test)

Example 17 with GenericObjectPoolConfig

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

the class MessageSender method start.

public void start() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(32);
    config.setMaxIdle(8);
    config.setMinIdle(1);
    config.setTestOnBorrow(true);
    config.setTestOnReturn(true);
    config.setTestWhileIdle(true);
    config.setNumTestsPerEvictionRun(12);
    config.setMaxWaitMillis(5000);
    config.setTimeBetweenEvictionRunsMillis(60000);
    config.setBlockWhenExhausted(true);
    // Set the name of this client to be able to distinguish when doing
    // CLIENT LIST on redis-cli
    redisPool = new JedisPool(config, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, "BbbRed5AppsPub");
    log.info("Redis message publisher starting!");
    try {
        sendMessage = true;
        Runnable messageSender = new Runnable() {

            public void run() {
                while (sendMessage) {
                    try {
                        MessageToSend msg = messages.take();
                        publish(msg.getChannel(), msg.getMessage());
                    } catch (InterruptedException e) {
                        log.warn("Failed to get message from queue.");
                    }
                }
            }
        };
        msgSenderExec.execute(messageSender);
    } catch (Exception e) {
        log.error("Error subscribing to channels: " + e.getMessage());
    }
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool)

Example 18 with GenericObjectPoolConfig

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

the class MessageSender method start.

public void start() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(32);
    config.setMaxIdle(8);
    config.setMinIdle(1);
    config.setTestOnBorrow(true);
    config.setTestOnReturn(true);
    config.setTestWhileIdle(true);
    config.setNumTestsPerEvictionRun(12);
    config.setMaxWaitMillis(5000);
    config.setTimeBetweenEvictionRunsMillis(60000);
    config.setBlockWhenExhausted(true);
    // Set the name of this client to be able to distinguish when doing
    // CLIENT LIST on redis-cli
    redisPool = new JedisPool(config, host, port, Protocol.DEFAULT_TIMEOUT, null, Protocol.DEFAULT_DATABASE, "BbbRed5AppsPub");
    log.info("Redis message publisher starting!");
    try {
        sendMessage = true;
        Runnable messageSender = new Runnable() {

            public void run() {
                while (sendMessage) {
                    try {
                        MessageToSend msg = messages.take();
                        publish(msg.getChannel(), msg.getMessage());
                    } catch (InterruptedException e) {
                        log.warn("Failed to get message from queue.");
                    }
                }
            }
        };
        msgSenderExec.execute(messageSender);
    } catch (Exception e) {
        log.error("Error subscribing to channels: " + e.getMessage());
    }
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool)

Example 19 with GenericObjectPoolConfig

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

the class JedisPoolTest method returnResourceDestroysResourceOnException.

@Test
public void returnResourceDestroysResourceOnException() {
    class CrashingJedis extends Jedis {

        @Override
        public void resetState() {
            throw new RuntimeException();
        }
    }
    final AtomicInteger destroyed = new AtomicInteger(0);
    class CrashingJedisPooledObjectFactory implements PooledObjectFactory<Jedis> {

        @Override
        public PooledObject<Jedis> makeObject() throws Exception {
            return new DefaultPooledObject<Jedis>(new CrashingJedis());
        }

        @Override
        public void destroyObject(PooledObject<Jedis> p) throws Exception {
            destroyed.incrementAndGet();
        }

        @Override
        public boolean validateObject(PooledObject<Jedis> p) {
            return true;
        }

        @Override
        public void activateObject(PooledObject<Jedis> p) throws Exception {
        }

        @Override
        public void passivateObject(PooledObject<Jedis> p) throws Exception {
        }
    }
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
    pool.initPool(config, new CrashingJedisPooledObjectFactory());
    Jedis crashingJedis = pool.getResource();
    try {
        crashingJedis.close();
    } catch (Exception ignored) {
    }
    assertEquals(destroyed.get(), 1);
}
Also used : Jedis(redis.clients.jedis.Jedis) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) PooledObjectFactory(org.apache.commons.pool2.PooledObjectFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PooledObject(org.apache.commons.pool2.PooledObject) DefaultPooledObject(org.apache.commons.pool2.impl.DefaultPooledObject) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) URISyntaxException(java.net.URISyntaxException) JedisException(redis.clients.jedis.exceptions.JedisException) InvalidURIException(redis.clients.jedis.exceptions.InvalidURIException) JedisExhaustedPoolException(redis.clients.jedis.exceptions.JedisExhaustedPoolException) Test(org.junit.Test)

Example 20 with GenericObjectPoolConfig

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

the class JedisPoolTest method checkResourceIsCloseable.

@Test
public void checkResourceIsCloseable() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
    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) JedisPool(redis.clients.jedis.JedisPool) 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