Search in sources :

Example 11 with JedisSentinelPool

use of redis.clients.jedis.JedisSentinelPool 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 12 with JedisSentinelPool

use of redis.clients.jedis.JedisSentinelPool in project cachecloud by sohutv.

the class JedisSentinelPoolTest method initializeWithNotMonitoredMasterNameShouldThrowException.

@Test(expected = JedisException.class)
public void initializeWithNotMonitoredMasterNameShouldThrowException() {
    final String wrongMasterName = "wrongMasterName";
    JedisSentinelPool pool = new JedisSentinelPool(wrongMasterName, sentinels);
    pool.destroy();
}
Also used : JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) Test(org.junit.Test)

Example 13 with JedisSentinelPool

use of redis.clients.jedis.JedisSentinelPool in project cachecloud by sohutv.

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)

Example 14 with JedisSentinelPool

use of redis.clients.jedis.JedisSentinelPool in project cachecloud by sohutv.

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 15 with JedisSentinelPool

use of redis.clients.jedis.JedisSentinelPool in project jedis by xetorthio.

the class JedisSentinelPoolTest method initializeWithNotAvailableSentinelsShouldThrowException.

@Test(expected = JedisConnectionException.class)
public void initializeWithNotAvailableSentinelsShouldThrowException() {
    Set<String> wrongSentinels = new HashSet<String>();
    wrongSentinels.add(new HostAndPort("localhost", 65432).toString());
    wrongSentinels.add(new HostAndPort("localhost", 65431).toString());
    JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, wrongSentinels);
    pool.destroy();
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)19 Test (org.junit.Test)17 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)12 Jedis (redis.clients.jedis.Jedis)11 HashSet (java.util.HashSet)4 HostAndPort (redis.clients.jedis.HostAndPort)3 BaseTest (com.sohu.tv.test.base.BaseTest)2 Transaction (redis.clients.jedis.Transaction)2 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)2 LoadingCacheTest (com.alicp.jetcache.LoadingCacheTest)1 RefreshCacheTest (com.alicp.jetcache.RefreshCacheTest)1 AbstractExternalCacheTest (com.alicp.jetcache.test.external.AbstractExternalCacheTest)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AppDesc (com.sohu.cache.entity.AppDesc)1 InstanceInfo (com.sohu.cache.entity.InstanceInfo)1 Random (java.util.Random)1