• java连接Redis


    package bean;
    import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.Pipeline;
    import java.util.List;
    /**
     * Created by Administrator on 2017/5/8 0008.
     */
    public class RedisPool {
        //连接池配置
        static GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        //初始化redis连接池,默认8个最大连接数
        static JedisPool jedisPool=null;
        static {
            //设置最大连接数为默认的5倍
            //poolConfig.setMaxTotal(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL*5);
            //设置最大空闲连接数为默认的3倍
            poolConfig.setMaxIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE*3);
            //设置最小空闲连接数为默认的2倍
            poolConfig.setMinIdle(GenericObjectPoolConfig.DEFAULT_MIN_IDLE*2);
            //设置开启jmx功能
            poolConfig.setJmxEnabled(true);
            //设置连接池没有连接后客户端的最大等待时间,毫秒
            poolConfig.setMaxWaitMillis(5000);
            jedisPool=new JedisPool(poolConfig,"127.0.0.1",6379);
        }
        public static Jedis getRedisFormPool() {
    //        Jedis jedis=new Jedis("127.0.0.1",6387);//生成Jedis对象
            Jedis jedis = null;
            try {
                jedis = jedisPool.getResource();
            } catch (Exception e) {
                System.err.println(e.getMessage());
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
            return jedis;
        }
        /**
         *  利用管道流删除,节省请求时间,非原子性
         * @param jedis
         * @param keys
         */
        public static void mdel(Jedis jedis,List<String> keys){
            Pipeline pipeline = jedis.pipelined();
            for(String key:keys){
                pipeline.del(key);
            }
            //执行命令
            pipeline.sync();
            //可返回执行结果
    //        pipeline.syncAndReturnAll();
        }
        public static void main(String[] args) {
            //System.out.println(getRedisFormPool().lrange("a",0,-1));
            for(int i=0;i<9;i++){
                //不执行redis.close(),可观察到因为连接数到达上限,先阻塞5s,然后报错
                System.out.println("ping..."+i);
                getRedisFormPool().ping();
            }
        }
    }
    
    

    客户端异常

    1.无法从连接池获得连接。
    默认8个连接对象,在没关闭的情况继续去获得,等待maxWaitMillis(>0)后,抛出异常。
    //设置连接池没有连接后客户端的最大等待时间,毫秒

            poolConfig.setMaxWaitMillis(5000);
        public static Jedis getRedisFormPool() {
            Jedis jedis = null;
            try {
                jedis = jedisPool.getResource();
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }/* finally {
                if (jedis != null) {
                    jedis.close();
                }
            }*/
            return jedis;
        }

    2.读写超时后,抛出SocketTImeoutException:Read timed out
    3.连接超时后 ,抛出SocketTImeoutException:Connection timed out

  • 相关阅读:
    Getting Started with MongoDB (MongoDB Shell Edition)
    Ioc
    BsonDocument
    Find or Query Data with C# Driver
    Insert Data with C# Driver
    Connect to MongoDB
    What's the difference between returning void and returning a Task?
    Import Example Dataset
    jQuery来源学习笔记:整体结构
    Word文件交换的电脑打开字体、排版变化的原因和解决方法!
  • 原文地址:https://www.cnblogs.com/thewindkee/p/12873218.html
Copyright © 2020-2023  润新知