package cn.jztd.redis.util; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class JediPoolUtils { private static JedisPool jedisPool; //静态代码块读取配置文件 static { //读取配置文件 InputStream inp = JediPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties"); //关联文件 Properties pro=new Properties(); try { pro.load(inp); } catch (IOException e) { e.printStackTrace(); } //读取配置文件设置到JedisPoolConfig里 JedisPoolConfig jedis=new JedisPoolConfig(); jedis.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle"))); jedis.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal"))); //初始化JedisPool jedisPool=new JedisPool(jedis,pro.getProperty("host"), Integer.parseInt(pro.getProperty("port"))); } public static Jedis getJed(){ return jedisPool.getResource(); } }
配置文件
host=127.0.0.1 port=6379 maxTotal=50 maxIdle=10