做Storm大数据开发,经常要用到redis来缓存一些东西,所以就写了个redis的连接池,jar包用的是2.8.0版本的,
经过一两个项目的检验,发现这个连接池在使用时还是挺不错的,代码如下:、
public enum RedisSingle {
INSTANCE;
private JedisPool jedisPool;
private RedisSingle() {
InputStream propInputStream = RedisSingle.class.getClassLoader().getResourceAsStream("config.properties");
Properties properties = new Properties();
try {
properties.load(propInputStream);
} catch (IOException e) {
System.err.println(e.getMessage());
}
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(Integer.parseInt(properties.get("JedisMaxActive").toString())); //100
jedisPoolConfig.setMaxIdle(Integer.parseInt(properties.get("JedisMaxIdle").toString())); //10
jedisPoolConfig.setMaxWaitMillis(Long.parseLong(properties.get("JedisMaxWait").toString())); //5000
jedisPoolConfig.setTestOnBorrow(Boolean.parseBoolean(properties.get("JedisTestOnBorrow").toString())); //false
jedisPool = new JedisPool(jedisPoolConfig, properties.get("JedisURL").toString(), Integer.parseInt(properties.get("JedisPort").toString()));
//redis服务器IP,端口6379
}
public Jedis getInstance() {
return jedisPool.getResource();
}
public void closeInstance(Jedis jedis) {
jedis.close();
}
public static void main(String[] args) {
Jedis jedis = null;
try {
jedis = RedisSingle.INSTANCE.getInstance();
System.out.println(jedis);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != jedis) {
RedisSingle.INSTANCE.closeInstance(jedis);
}
}
}
}