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;
}
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();
}
});
}
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");
}
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();
}
}
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);
}
Aggregations