Configure Jedis timeout

前端 未结 3 2030
失恋的感觉
失恋的感觉 2020-12-31 12:50

I\'m having problems completing an .hgetall(), here\'s what I\'ve tried:

Jedis jedis = new Jedis(REDIS_MASTER_NODE);
jedis.connect();
jedis.conf         


        
相关标签:
3条回答
  • 2020-12-31 13:24

    Few things to consider:

    1. For both Jedis and JedisPool classes, timeout is in miliseconds. Default timeout, at least in 2.5.1, as I see, is 2000 (milisec): int redis.clients.jedis.Protocol.DEFAULT_TIMEOUT = 2000 [0x7d0]

    2. As per this documentation, Redis 2.6 or higher does not close connection, even if the client is idle. I have not verified this yet, and I will try to update the post when I do.

    0 讨论(0)
  • 2020-12-31 13:30

    This is a bit of an extension to xetorthio's answer, but here is similar approach for use with a JedisPool. (Caveat: this is based on my understanding from looking at the Jedis version 2.6.2 code directly and has not been tested in a live use case.)

        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxWaitMillis(writeTimeout);
        JedisPool pool = new JedisPool(jedisPoolConfig, redisHost, port, readTimeout);
    

    The writeTimeout is max time for a Jedis resource from the pool to wait for a write operation.

    The readTimeout specified for the pool constructor is the wait time for a socket read, see java.net.Socket.setSoTimeout for more specific details.

    0 讨论(0)
  • 2020-12-31 13:41

    If what you want to do is set Jedis connection timeout, you should do it using the special constructor made for that:

    public Jedis(final String host, final int port, final int timeout)
    

    What you are doing is setting the timeout on redis settings from jedis. Doing CONFIG SET timeout 60, means that redis will close idle client connections after 60 seconds. That's why you get the exception in Jedis.

    0 讨论(0)
提交回复
热议问题