通过jedis客户端连接redis写入报"JedisConnectionException: java.net.SocketException:

通过jedis客户端连接redis写入报"JedisConnectionException: java.net.SocketException: Software caused connection abort: socket write error"异常

一、异常描述

通过客户端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

[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 .... 

    at redis.clients.jedis.Protocol.sendCommand(Protocol.java:94)

    at redis.clients.jedis.Protocol.sendCommand(Protocol.java:74)

    at redis.clients.jedis.Connection.sendCommand(Connection.java:80)

    at redis.clients.jedis.BinaryClient.set(BinaryClient.java:92)

    at redis.clients.jedis.Client.set(Client.java:24)

    at redis.clients.jedis.Jedis.set(Jedis.java:62)

    at com.xwood.publisher.util.JedisUtil.set(JedisUtil.java:35)

    at com.xwood.publisher.master.PublishEntityDataHandler.pushRemoteRedis(PublishEntityDataHandler.java:43)

    at com.xwood.publisher.master.PublishEntityDataHandler.run(PublishEntityDataHandler.java:36)

    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)

    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)

    at java.util.concurrent.FutureTask.run(FutureTask.java:138)

    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)

    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)

    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)

    at java.lang.Thread.run(Thread.java:662)

[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 .... 

Caused by: java.net.SocketException: Software caused connection abort: socket write error

    at java.net.SocketOutputStream.socketWrite0(Native Method)

    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)

    at java.net.SocketOutputStream.write(SocketOutputStream.java:136)

    at redis.clients.util.RedisOutputStream.flushBuffer(RedisOutputStream.java:31)

    at redis.clients.util.RedisOutputStream.write(RedisOutputStream.java:38)

    at redis.clients.jedis.Protocol.sendCommand(Protocol.java:80)

二、解决方法

之前通过jedis单例方式创建客户端会话,具体代码如下

public class JedisUtil{ 

     private static Jedis client = new Jedis(j_ip,j_port,60000);  

    public static Jedis getclient(){

        return client;

    }     

    public static void  setcache(String key,String value){

        JedisUtil.getclient().set(key, value);

     

    ... 

}

改为单例+多例结合的方式解决问题,在单例异常的情况下,再调用多例的方式重新赋值,结果问题解决了,具体代码如下

public class JedisUtil{

    private static BlockingQueue pools=new LinkedBlockingQueue();      

    private static Jedis client = new Jedis(j_ip,j_port,60000);    

    public static Jedis getclient(){

        return client;

    }         

    public  static  Jedis  getFactoryBean() throws  Exception{

       if(pools.size()<10){

            Jedis add_client = new Jedis(j_ip,j_port,60000);

            pools.add(add_client);

        }

        return pools.take();

    }                

    public static void setcache(String key,String value){

        try{

            if(value!=null&&!"".equals(value)&&key!=null&&!"".equals(key)){

                JedisUtil.getclient().set(key, value); 

            }else{

                logger.error(" 【JedisUtil save NULL】 key-value:"+key+"@value:"+value);

            }                 

        }catch(Exception e){

            logger.error(" 【JedisUtil save exception】 key:"+key+"@value:"+value);

            try{

                    getFactoryBean().set(key, value); 

                catch (Exception e1) {

                    e1.printStackTrace();

                    logger.error(" 【JedisUtil getFactoryBean exception】    key:"+key+"@value:"+value);

                }                 

        }       

    }

}

转:http://www.xwood.net/_site_domain_/_root/5870/5874/t_c266986.html

你可能感兴趣的:(redis)