JedisConnectionException读取超时

时间:2018-02-01 04:21:25

标签: java redis timeout jedis

我在我的Java程序中使用jedis。它的版本是 2.9.0

         <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
            <type>jar</type>
        </dependency>

为避免命令keys(*)花费太多时间查询结果,我将密钥存储在一个集合中,当需要所有密钥时,使用以下方法从集合中查询:

public Set<String> getKeysFromDailySet(String day) {
    Jedis jedis = jedisPool.getResource();
    Set<String> keys = new HashSet<>();
    try {
        keys = jedis.smembers(day);
        jedis.close();
    } catch (Exception e) {
        logger.error("fail get keys from daily set {}", day);
    }
    return keys;
}

设置中有 800万个键,它会抛出超时超时异常:

  Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
            at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:202) ~[scoring-20180118.jar:na]
            at redis.clients.util.RedisInputStream.readByte(RedisInputStream.java:40) ~[scoring-20180118.jar:na]
            at redis.clients.jedis.Protocol.process(Protocol.java:151) ~[scoring-20180118.jar:na]
            at redis.clients.jedis.Protocol.read(Protocol.java:215) ~[scoring-20180118.jar:na]
            at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:340) ~[scoring-20180118.jar:na]
            at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:239) ~[scoring-20180118.jar:na]
            at redis.clients.jedis.BinaryJedis.select(BinaryJedis.java:523) ~[scoring-20180118.jar:na]
            at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:111) ~[scoring-20180118.jar:na]
            at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868) ~[scoring-20180118.jar:na]
            at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435) ~[scoring-20180118.jar:na]
            at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363) ~[scoring-20180118.jar:na]
            at redis.clients.util.Pool.getResource(Pool.java:49) ~[scoring-20180118.jar:na]
            ... 9 common frames omitted
    Caused by: java.net.SocketTimeoutException: Read timed out
            at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.7.0_80]
            at java.net.SocketInputStream.read(SocketInputStream.java:152) ~[na:1.7.0_80]
            at java.net.SocketInputStream.read(SocketInputStream.java:122) ~[na:1.7.0_80]
            at java.net.SocketInputStream.read(SocketInputStream.java:108) ~[na:1.7.0_80]
            at redis.clients.util.RedisInputStream.ensureFill(RedisInputStream.java:196) ~[scoring-20180118.jar:na]

这是我的JedisPool配置:

private final int HOUR_IN_SECONDS = 3600 * 10;  // 10 HOURS
private final Configuration config;
private final int INDEX;

public DatabaseConnection(String host, Configuration config) {
    this.config = config;
    this.INDEX = config.getRedisDbIndex();
    jedisPool = new JedisPool(buildJedisPoolConfiguration(), host, 6379, HOUR_IN_SECONDS, null, INDEX);
}

private JedisPoolConfig buildJedisPoolConfiguration() {
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(this.config.getNumberOfThreads());
    return jedisPoolConfig;
}

我已将超时设置为10小时,但错误发生在1小时内。我从github上读过Jedis,但没有为我的案例找到样本。有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:3)

JedisPool以超时为单位。所以你给定的超时实际上是36秒,而不是10小时。

另外请注意,您应该考虑使用批量较小的SMEMBERS而不是在一组800万成员上运行SSCAN