redis在java中的客户端连接
2016-01-18 18:05
861 查看
单例模式下创建连接池,包括断线重连等机制。
public class RedisClient {
private static JedisPool pool = null;
private RedisClient(){
initial();
}
private static RedisClient client = new RedisClient();
public static RedisClient getClient(){
return client;
}
private void initial() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxActive(30000);
jedisPoolConfig.setMaxIdle(600);
jedisPoolConfig.setMaxWait(1000);
jedisPoolConfig.setTestOnBorrow(true);
if (pool == null) {
try {
pool = new JedisPool(jedisPoolConfig, "10.0.201.140", 5379,4000000);
} catch (Exception e) {
e.printStackTrace();
pool.destroy();
initial();//断线重连
}
}
}
/**
* 获取redis连接的 Pipelined
* @return
*/
public synchronized jedis getJedis() {
Jedis jd = null;
try {
jd= pool.getResource();
} catch (Exception e) {
if(jd!=null){
pool.returnBrokenResource(jd);
}
e.printStackTrace();
jd = getJedis();//递归进行断线重连
}
return jd;
}
/**
* 释放被损坏的jedis.
*
* @param jd
*/
public synchronized void releaseBrokenJedis(Jedis jd) {
pool.returnBrokenResource(jd);
jd = null;
}
/**
* 从连接池中释放jedis
*
* @param jd
*/
public synchronized void releaseJedis(Jedis jd) {
pool.returnResource(jd);
jd = null;
}
}
public class RedisClient {
private static JedisPool pool = null;
private RedisClient(){
initial();
}
private static RedisClient client = new RedisClient();
public static RedisClient getClient(){
return client;
}
private void initial() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxActive(30000);
jedisPoolConfig.setMaxIdle(600);
jedisPoolConfig.setMaxWait(1000);
jedisPoolConfig.setTestOnBorrow(true);
if (pool == null) {
try {
pool = new JedisPool(jedisPoolConfig, "10.0.201.140", 5379,4000000);
} catch (Exception e) {
e.printStackTrace();
pool.destroy();
initial();//断线重连
}
}
}
/**
* 获取redis连接的 Pipelined
* @return
*/
public synchronized jedis getJedis() {
Jedis jd = null;
try {
jd= pool.getResource();
} catch (Exception e) {
if(jd!=null){
pool.returnBrokenResource(jd);
}
e.printStackTrace();
jd = getJedis();//递归进行断线重连
}
return jd;
}
/**
* 释放被损坏的jedis.
*
* @param jd
*/
public synchronized void releaseBrokenJedis(Jedis jd) {
pool.returnBrokenResource(jd);
jd = null;
}
/**
* 从连接池中释放jedis
*
* @param jd
*/
public synchronized void releaseJedis(Jedis jd) {
pool.returnResource(jd);
jd = null;
}
}
相关文章推荐
- windows下安装php5.5的redis扩展
- Redis总结
- redis之RDB持久化与AOF持久化
- redis中各种数据类型对应的jedis操作命令
- Redis作为缓存自我总结
- Redis 3.0的集群部署
- Infinispan-Redis配置使用
- Redis学习记录之keyspace notification简析(二十三)
- Redis 安装部署
- redis常见操作命令-set
- Redis学习记录之Server(二十二)
- 基于Redis实现分布式锁以及任务队列
- Redis持久化
- Redis持久化机制和恢复机制
- Redis 高可用性解决方案(Sentinel)
- 在CentOS6.5上部署redis3.0.6
- Redis学习记录之Connection(连接)(二十一)
- Redis学习记录之Script(二十)
- redis取zset中score最大的member
- Redis详细教程!