jedis 中出现ArrayIndexOutOfBoundsException异常的解决方法

在使用jedis连接redis并调用publish方法发布消息时,出现了ArrayIndexOutOfBoundsException异常,当时使用的是jedis2.1.0版本,查看源代码发现是write方法中通过递增count,向缓存字节数组中写入数据时出现的ArrayIndexOutOfBoundsException,而且该异常是偶发的,并不是必现的,因此原因也不好追查。
之前在别的模块也遇到过jedis连接redis时出现的各种问题,例如还有下面的异常:
org.jivesoftware.openfire.RedisConnection - Subscribing failed with exception:
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe
        at redis.clients.jedis.Connection.flush(Connection.java:66)
        at redis.clients.jedis.JedisPubSub.proceed(JedisPubSub.java:82)
        at redis.clients.jedis.Jedis.subscribe(Jedis.java:1971)
        at org.jivesoftware.openfire.RedisConnection$1.run(RedisConnection.java:116)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Broken pipe
        at java.net.SocketOutputStream.socketWrite0(Native Method)
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
        at redis.clients.util.RedisOutputStream.flushBuffer(RedisOutputStream.java:29)
        at redis.clients.util.RedisOutputStream.flush(RedisOutputStream.java:227)
        at redis.clients.jedis.Connection.flush(Connection.java:64)
        ... 4 more
后来解决的方式就是替换了一个新版本的jedis的jar包——jedis2.5.1,需要提醒的是,这个版本中使用的commons-pool2的是commons-pool2-2.2.jar,需要Java 6.0+。
如果项目中同时用到了commons DBCP,那么需要注意,如果你使用的是commons-dbcp-1.4,那还需要保留原有的commons-pool1.x(Java 6.0+),否则需要将commons-dbcp升级到2.x版本,而且commons-dbcp2-2.0.1需要Java 7.0+。
处理替换jar,在释放redis连接方面也做了部分改动,会重试几次连接和释放。

猜你喜欢

转载自bsr1983.iteye.com/blog/2114630