Search in sources :

Example 76 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project new-cloud by xie-summer.

the class RedisStandaloneTest method testStandaloneExample.

@Test
public void testStandaloneExample() {
    long appId = 10122;
    JedisPool jedisPool = null;
    // 使用默认配置
    // jedisPool = ClientBuilder.redisStandalone(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);
    jedisPool = ClientBuilder.redisStandalone(appId).setPoolConfig(poolConfig).setTimeout(2000).build("");
    Jedis jedis = jedisPool.getResource();
    jedis.setnx("key2", "5");
    assertEquals("10", jedis.incrBy("key2", 5));
    jedis.close();
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) BaseTest(com.sohu.tv.test.base.BaseTest) Test(org.junit.Test)

Example 77 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project new-cloud by xie-summer.

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 78 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project new-cloud by xie-summer.

the class JedisClusterTest method testIfPoolConfigAppliesToClusterPools.

@Test(expected = JedisConnectionException.class)
public void testIfPoolConfigAppliesToClusterPools() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(0);
    config.setMaxWaitMillis(2000);
    Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
    jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379));
    JedisCluster jc = new JedisCluster(jedisClusterNode, config);
    jc.set("52", "poolTestValue");
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig)

Example 79 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project new-cloud by xie-summer.

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 80 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project new-cloud by xie-summer.

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)

Aggregations

GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)222 Test (org.junit.Test)82 Jedis (redis.clients.jedis.Jedis)44 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)43 ShardedJedis (redis.clients.jedis.ShardedJedis)41 JedisPool (redis.clients.jedis.JedisPool)37 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)32 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)21 Bean (org.springframework.context.annotation.Bean)20 ArrayList (java.util.ArrayList)17 JedisShardInfo (redis.clients.jedis.JedisShardInfo)17 Test (org.junit.jupiter.api.Test)15 URI (java.net.URI)12 LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)12 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)12 RedisStandaloneConfiguration (org.springframework.data.redis.connection.RedisStandaloneConfiguration)11 IOException (java.io.IOException)10 Test (org.testng.annotations.Test)10 PooledObject (org.apache.commons.pool2.PooledObject)9 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)9