标签:style io ar os sp java on 问题 cti
在用jedis客户端对redis进行压力测试时,经常会出现下述错误:
Exception in thread "Thread-565" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
at redis.clients.jedis.Protocol.process(Protocol.java:79)
at redis.clients.jedis.Protocol.read(Protocol.java:131)
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:162)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:76)
at redis.clients.jedis.Connection.sendCommand(Connection.java:79)
at redis.clients.jedis.BinaryClient.select(BinaryClient.java:148)
at redis.clients.jedis.Jedis.select(Jedis.java:328)
at RedisThread.run(RedisThread.java:15)
at java.lang.Thread.run(Thread.java:679)
Caused by: java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:146)
at java.net.SocketInputStream.read(SocketInputStream.java:107)
at redis.clients.util.RedisInputStream.fill(RedisInputStream.java:109)
at redis.clients.util.RedisInputStream.readByte(RedisInputStream.java:45)
at redis.clients.jedis.Protocol.process(Protocol.java:64)
... 8 more
从中可以看出,出错的原因是线程等待超时,断开连接,所以可以通过手动设置超时时间来避免这个错误。查询Jedis构造函数,可以发现其中一个构造函数如下,可以在其中指出客户端等待的超时时间:
public Jedis(final String host, final int port, final int timeout) {
super(host, port, timeout);
}
在Jedis对象创建时,在构造函数中按照实际需求设置timeout的大小可以避免上述错误的出现。
标签:style io ar os sp java on 问题 cti
原文地址:http://my.oschina.net/u/1169607/blog/348687