Java Jedis 池如何运作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21230503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Jedis Pool works?
提问by ipkiss
I'm using Jedis pool to manage connections to Redis server. An example code of mine as follows:
我正在使用 Jedis 池来管理与 Redis 服务器的连接。我的示例代码如下:
public Set<String> getTopArticleList(int start, int end) {
Set<String> list = null;
Jedis j = JedisFactory.getInstance().getJedisPool().getResource();
Pipeline pipe = j.pipelined();
try {
// do stuff with redis
pipe.sync();
} catch (JedisConnectionException jex) {
JedisFactory.getInstance().getJedisPool().returnBrokenResource(j);
} finally {
JedisFactory.getInstance().getJedisPool().returnResource(j);
}
return list;
}
Code to create and retrieve the Jedis pool:
创建和检索 Jedis 池的代码:
class JedisFactory {
private static JedisPool jedisPool;
public JedisFactory() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
jedisPool = new JedisPool(
poolConfig,
RedisDBConfig.HOST,
RedisDBConfig.PORT,
RedisDBConfig.TIMEOUT,
RedisDBConfig.PASSWORD
);
}
public JedisPool getJedisPool() {
return jedisPool;
}
public static JedisFactory getInstance() {
if (instance == null) {
instance = new JedisFactory();
}
return instance;
}
}
The problem is that after reaching the number of limited connections, the web cannot be accessed anymore. Am I doing something wrong?
问题是在达到限制连接数后,无法再访问网络。难道我做错了什么?
回答by Alexandre L Telles
You haven't configured the maxTotalsize of the pool, and the default value is only 8. You could change the JedisFactory constructor to:
您尚未配置池的maxTotal大小,默认值仅为 8。您可以将 JedisFactory 构造函数更改为:
public JedisFactory() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(128);
jedisPool = new JedisPool(poolConfig, RedisDBConfig.HOST, RedisDBConfig.PORT, RedisDBConfig.TIMEOUT, RedisDBConfig.PASSWORD);
}
Beware also of the default value of the WhenExhaustedAction(WHEN_EXHAUSTED_BLOCK), as it may not be your desired behavior.
还要注意WhenExhaustedAction(WHEN_EXHAUSTED_BLOCK)的默认值,因为它可能不是您想要的行为。
Jedis uses Apache Commons Pool, and you can read about it's parameters here: GenericObjectPool
Jedis 使用 Apache Commons Pool,你可以在这里阅读它的参数:GenericObjectPool