package cn.td.utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
/**
* JedisPool工具類
* 加載配置文件,配置連接池參數
* 提供獲取連接的方法
*/
public class JedisPoolUtils {
private static JedisPool jedisPool;
static {
Properties properties = new Properties();
InputStream resourceAsStream = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.parseInt(properties.getProperty("maxTotal")));
config.setMaxIdle(Integer.parseInt(properties.getProperty("maxIdle")));
config.setMinIdle(Integer.parseInt(properties.getProperty("minIdle")));
config.setMaxWaitMillis(Integer.parseInt(properties.getProperty("maxWaitMillis")));
config.setTestOnBorrow(Boolean.parseBoolean(properties.getProperty("testOnBorrow")));
config.setTestOnReturn(Boolean.parseBoolean(properties.getProperty("testOnReturn")));
config.setTimeBetweenEvictionRunsMillis(Integer.parseInt(properties.getProperty("timeBetweenEvictionRunsMillis")));
config.setTestWhileIdle(Boolean.parseBoolean(properties.getProperty("testWhileIdle")));
config.setNumTestsPerEvictionRun(Integer.parseInt(properties.getProperty("numTestsPerEvictionRun")));
//初始化JedisPool
jedisPool = new JedisPool(
config,
properties.getProperty("host"),
Integer.parseInt(properties.getProperty("port"))
);
}
/**
* 獲取Jedis連接
* @return
*/
public static Jedis getJedis(){
return jedisPool.getResource();
}
}
JedisPool工具類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.