jedis.properties配置文件
host=192.168.119.166
port=6379
maxTotal=50
maxIdle=10
JedisPoolUtils工具类
public class JedisPoolUtils {
private static JedisPool jedisPool;
static {
//读取配置文件
InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
Properties properties = new Properties();
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//获取数据,设置到JedisPoolConfig中
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.parseInt(properties.getProperty("maxTotal"))); config.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle")));
//初始JedisPool
jedisPool = new JedisPool(config,properties.getProperty("host"),Integer.parseInt(properties.getProperty("port")));
}
/**
* 获取连接方法
*/
public static Jedis getJedis(){
return jedisPool.getResource();
}
}
测试
@Test
//jedis连接池
public void test7(){
Jedis jedis = JedisPoolUtils.getJedis();
jedis.set("cc","jj");
String cc = jedis.get("cc");
System.out.println(cc);
//关闭 归还到连接池中
jedis.close();
}