在用jedis客戶端對redis進行壓力測試時,常常會出現下述錯誤:java
Exception in thread "Thread-565" redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed outredis
at redis.clients.jedis.Protocol.process(Protocol.java:79)socket
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)spa
at redis.clients.jedis.Connection.sendCommand(Connection.java:79).net
at redis.clients.jedis.BinaryClient.select(BinaryClient.java:148)線程
at redis.clients.jedis.Jedis.select(Jedis.java:328)對象
at RedisThread.run(RedisThread.java:15)get
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的大小能夠避免上述錯誤的出現。