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