zoukankan      html  css  js  c++  java
  • Jedis连接池

    介绍完JedisPool的基本使用,我们要对JedisPool抽取一个工具类,因为我们连接池参数如果都这样指定的话,不便于修改,耦合度高些,可以把参数抽取到一个配置文件里面,读取配置文件,加载这些参数的配置更合理一些

    package util;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.JedisPoolConfig;
    
    /*
     * JedisPool工具类
     *     加载配置文件,配置连接池的参数
     *  提供获取连接的方法
     */
    public class JedisPoolUtils {
        private static JedisPool jedisPool;
        static {
            //读取配置文件
            InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
            //创建Propeteis对象
            Properties pro = new Properties();
            //关联文件
            try {
                pro.load(is);
            }catch(IOException e) {
                e.printStackTrace();
            }
            //获取数据,设置JedisPoolConfig中
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
            config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
            
            //初始化JedisPool
            jedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port")));
        }
        //获取连接方法
        public static Jedis getJedis() {
            return jedisPool.getResource();
        }
    }

                                                                                                                                                                                                           

  • 相关阅读:
    EMQ ---payload
    EMQ --集成搭建
    chome 离线安装包地址
    EMQ ---问题集
    EMQ学习---客户链接资源消耗
    EMQ学习 ---集群
    EMQ 学习---MQTT消息QoS
    EMQ 学习---订阅$SYS主题,捕获客户端上下线消息
    EMQ学习笔记---Clean Session和Retained Message
    elasticsearch学习网站
  • 原文地址:https://www.cnblogs.com/djlindex/p/11602882.html
Copyright © 2011-2022 走看看