Search in sources :

Example 71 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project core-util by WSO2Telco.

the class RedisUtil method getInstance.

public static JedisPool getInstance() {
    if (pool == null) {
        synchronized (RedisUtil.class) {
            if (pool == null) {
                Map<Object, Object> redis = (Map<Object, Object>) YamlReader.getConfiguration().getRedis();
                String redisHost = (String) redis.get("host");
                int redisPort = (int) redis.get("port");
                String password = (String) redis.get("password");
                int timeout = (int) redis.get("timeout");
                int jedisPoolSize = (int) redis.get("poolsize");
                JedisPoolConfig poolConfig = new JedisPoolConfig();
                poolConfig.setMaxTotal(jedisPoolSize);
                pool = new JedisPool(new GenericObjectPoolConfig(), redisHost, redisPort, timeout, password);
            }
        }
    }
    return pool;
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) Map(java.util.Map) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig)

Example 72 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project Hotchpotch by carryxyh.

the class RedisSentinelService method init.

public void init() {
    Set<String> sentinelset = new HashSet<>(Arrays.asList(sentinels.split(",")));
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxIdle(this.getMaxIdle());
    config.setMinIdle(this.getMinIdle());
    config.setMaxTotal(this.getMaxTotal());
    // 表示当borrow一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException
    config.setMaxWaitMillis(this.getMaxWaitMillis());
    // 获取连接时关闭触发ping
    config.setTestOnBorrow(this.isTestOnBorrow());
    // 释放连接时关闭触发ping
    config.setTestOnReturn(this.isTestOnReturn());
    // 表示有一个idle object evitor线程对idle object进行扫描,如果validate失败,此object会被从pool中drop掉
    config.setTestWhileIdle(this.isTestWhileIdle());
    // 每隔30秒定期检查空闲连接
    config.setTimeBetweenEvictionRunsMillis(this.getTimeBetweenEvictionRunsMillis());
    // 空闲连接扫描时,每次最多扫描的连接数, -1 全部扫描
    config.setNumTestsPerEvictionRun(this.getNumTestsPerEvictionRun());
    // 连接在池中保持空闲而不被空闲连接回收器线程回收的最小时间值
    config.setMinEvictableIdleTimeMillis(this.getMinEvictableIdleTimeMillis());
    pool = new JedisSentinelPool(masterName, sentinelset, config, Protocol.DEFAULT_TIMEOUT, null, database);
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            pool.destroy();
        }
    });
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) HashSet(java.util.HashSet)

Example 73 with GenericObjectPoolConfig

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

the class JedisPoolTest method checkPoolOverflow.

@Test(expected = JedisConnectionException.class)
public void checkPoolOverflow() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort());
    Jedis jedis = pool.getResource();
    jedis.auth("foobared");
    jedis.set("foo", "0");
    Jedis newJedis = pool.getResource();
    newJedis.auth("foobared");
    newJedis.incr("foo");
}
Also used : Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) Test(org.junit.Test)

Example 74 with GenericObjectPoolConfig

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

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)

Example 75 with GenericObjectPoolConfig

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

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) 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