一、异常描述
通过客户端jedis连接redis服务器并发写入数据时报出“redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Software caused connection abort: socket write error...Caused by: java.net.SocketException: Software caused connection abort: socket write error”等异常日志,详情如下所示
redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Software caused connection abort: socket write error@b@[xwood-pms-publish] [pool-2-thread-1] 2017-06-05 13:31:08,246 - com.xwood.publisher.master.PublishEntityDataHandler -61998 [pool-2-thread-1] INFO com.xwood.publisher.master.PublishEntityDataHandler - 【 extract - PublishEntityDataHandler:1】 s1-2@ entityNo:447@349779 .... @b@ at redis.clients.jedis.Protocol.sendCommand(Protocol.java:94)@b@ at redis.clients.jedis.Protocol.sendCommand(Protocol.java:74)@b@ at redis.clients.jedis.Connection.sendCommand(Connection.java:80)@b@ at redis.clients.jedis.BinaryClient.set(BinaryClient.java:92)@b@ at redis.clients.jedis.Client.set(Client.java:24)@b@ at redis.clients.jedis.Jedis.set(Jedis.java:62)@b@ at com.xwood.publisher.util.JedisUtil.set(JedisUtil.java:35)@b@ at com.xwood.publisher.master.PublishEntityDataHandler.pushRemoteRedis(PublishEntityDataHandler.java:43)@b@ at com.xwood.publisher.master.PublishEntityDataHandler.run(PublishEntityDataHandler.java:36)@b@ at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)@b@ at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)@b@ at java.util.concurrent.FutureTask.run(FutureTask.java:138)@b@ at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)@b@ at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)@b@ at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)@b@ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)@b@ at java.lang.Thread.run(Thread.java:662)@b@[xwood-pms-publish] [pool-2-thread-1] 2017-06-05 13:31:08,247 - com.xwood.publisher.master.PublishEntityDataHandler -61999 [pool-2-thread-1] INFO com.xwood.publisher.master.PublishEntityDataHandler - 【extract - PublishEntityDataHandler:1】 s1-3@ entityNo:447@349779 .... @b@Caused by: java.net.SocketException: Software caused connection abort: socket write error@b@ at java.net.SocketOutputStream.socketWrite0(Native Method)@b@ at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)@b@ at java.net.SocketOutputStream.write(SocketOutputStream.java:136)@b@ at redis.clients.util.RedisOutputStream.flushBuffer(RedisOutputStream.java:31)@b@ at redis.clients.util.RedisOutputStream.write(RedisOutputStream.java:38)@b@ at redis.clients.jedis.Protocol.sendCommand(Protocol.java:80)@b@ ... 16 more
二、解决方法
之前通过jedis单例方式创建客户端会话,具体代码如下
public class JedisUtil{ @b@ @b@ private static Jedis client = new Jedis(j_ip,j_port,60000);@b@ @b@ public static Jedis getclient(){@b@ return client;@b@ } @b@ @b@ public static void setcache(String key,String value){@b@ JedisUtil.getclient().set(key, value);@b@ } @b@ @b@ ...@b@ @b@}
改为单例+多例结合的方式解决问题,在单例异常的情况下,再调用多例的方式重新赋值,结果问题解决了,具体代码如下
public class JedisUtil{@b@ @b@ private static BlockingQueue<Jedis> pools=new LinkedBlockingQueue<Jedis>();@b@ @b@ private static Jedis client = new Jedis(j_ip,j_port,60000); @b@ @b@ public static Jedis getclient(){@b@ return client;@b@ }@b@ @b@ public static Jedis getFactoryBean() throws Exception{@b@ if(pools.size()<10){@b@ Jedis add_client = new Jedis(j_ip,j_port,60000);@b@ pools.add(add_client);@b@ }@b@ return pools.take();@b@ }@b@ @b@ @b@ public static void setcache(String key,String value){@b@ try{@b@ if(value!=null&&!"".equals(value)&&key!=null&&!"".equals(key)){@b@ JedisUtil.getclient().set(key, value); @b@ }else{@b@ logger.error(" 【JedisUtil save NULL】 key-value:"+key+"@value:"+value);@b@ }@b@ @b@ }catch(Exception e){@b@ logger.error(" 【JedisUtil save exception】 key:"+key+"@value:"+value);@b@ try{@b@ getFactoryBean().set(key, value); @b@ } catch (Exception e1) {@b@ e1.printStackTrace();@b@ logger.error(" 【JedisUtil getFactoryBean exception】 key:"+key+"@value:"+value);@b@ }@b@ @b@ }@b@ @b@ }@b@}