java读取redis的timeout异常

异常内容:

redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
	at redis.clients.jedis.Protocol.process(Protocol.java:79) ~[jedis-2.1.0.jar:na]
	at redis.clients.jedis.Protocol.read(Protocol.java:131) ~[jedis-2.1.0.jar:na]
	at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:182) ~[jedis-2.1.0.jar:na]
	at redis.clients.jedis.Connection.getBulkReply(Connection.java:171) ~[jedis-2.1.0.jar:na]
	at redis.clients.jedis.Jedis.lpop(Jedis.java:1090) ~[jedis-2.1.0.jar:na]
	at zju.lzm.storm.spout.RedisReadSpout.nextTuple(RedisReadSpout.java:52) ~[bin/:na]
	at backtype.storm.daemon.executor$eval5100$fn__5101$fn__5116$fn__5145.invoke(executor.clj:562) ~[na:na]
	at backtype.storm.util$async_loop$fn__390.invoke(util.clj:433) ~[na:na]
	at clojure.lang.AFn.run(AFn.java:24) [clojure-1.4.0.jar:na]
	at java.lang.Thread.run(Unknown Source) [na:1.7.0_07]
Caused by: java.net.SocketTimeoutException: Read timed out
	at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.7.0_07]
	at java.net.SocketInputStream.read(Unknown Source) ~[na:1.7.0_07]
	at java.net.SocketInputStream.read(Unknown Source) ~[na:1.7.0_07]
	at java.net.SocketInputStream.read(Unknown Source) ~[na:1.7.0_07]
	at redis.clients.util.RedisInputStream.fill(RedisInputStream.java:109) ~[jedis-2.1.0.jar:na]
	at redis.clients.util.RedisInputStream.readByte(RedisInputStream.java:45) ~[jedis-2.1.0.jar:na]
	at redis.clients.jedis.Protocol.process(Protocol.java:64) ~[jedis-2.1.0.jar:na]
	... 9 common frames omitted
209206 [Thread-27-RedisReadSpout] ERROR backtype.storm.daemon.executor - 
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: Read timed out
	at redis.clients.jedis.Protocol.process(Protocol.java:79) ~[jedis-2.1.0.jar:na]
	at redis.clients.jedis.Protocol.read(Protocol.java:131) ~[jedis-2.1.0.jar:na]
	at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:182) ~[jedis-2.1.0.jar:na]
	at redis.clients.jedis.Connection.getBulkReply(Connection.java:171) ~[jedis-2.1.0.jar:na]
	at redis.clients.jedis.Jedis.lpop(Jedis.java:1090) ~[jedis-2.1.0.jar:na]
	at zju.lzm.storm.spout.RedisReadSpout.nextTuple(RedisReadSpout.java:52) ~[bin/:na]
	at backtype.storm.daemon.executor$eval5100$fn__5101$fn__5116$fn__5145.invoke(executor.clj:562) ~[na:na]
	at backtype.storm.util$async_loop$fn__390.invoke(util.clj:433) ~[na:na]
	at clojure.lang.AFn.run(AFn.java:24) [clojure-1.4.0.jar:na]
	at java.lang.Thread.run(Unknown Source) [na:1.7.0_07]
Caused by: java.net.SocketTimeoutException: Read timed out
	at java.net.SocketInputStream.socketRead0(Native Method) ~[na:1.7.0_07]
	at java.net.SocketInputStream.read(Unknown Source) ~[na:1.7.0_07]
	at java.net.SocketInputStream.read(Unknown Source) ~[na:1.7.0_07]
	at java.net.SocketInputStream.read(Unknown Source) ~[na:1.7.0_07]
	at redis.clients.util.RedisInputStream.fill(RedisInputStream.java:109) ~[jedis-2.1.0.jar:na]
	at redis.clients.util.RedisInputStream.readByte(RedisInputStream.java:45) ~[jedis-2.1.0.jar:na]
	at redis.clients.jedis.Protocol.process(Protocol.java:64) ~[jedis-2.1.0.jar:na]
	... 9 common frames omitted

 在java中使用Jedis进行Redis数据库的操作时,出现了这个超时的问题。

 解决方法:在调用jedis的构造方法时,将超时时间设置的更大些。如:

Jedis jedis = new Jedis("127.0.0.1", 8371,100000);//将超时时间设置为100000毫秒
这样足够大,便基本不会出现超时的问题了。

问题的剖析:

在jedis的默认构造方法中,超时的时间一般被默认设置为2000毫秒,也就是2秒。当然这个时间,对于Redis这种从内存中读取数据的数据库来说应该是相当大了,但是为什么还会超时?可能的原因是,当Redis的数据量很大时,可能在Redis server可能会出现超时。因为Redis在运行时,是单线程来处理所有的客户端的连接的。当连接数非常多或者数据量很大时,这也要遵循FIFO的调度策略。因此可能会出现超时的情况。


其他的误改:

jedis.configSet("timeout", "30");
上面的方法是设置redis将会关闭超时超过30秒的空闲连接。而不是设置读取数据的超时时间。


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值