java redis 重连机制_redis在java中的客户端连接
單例模式下創建連接池,包括斷線重連等機制。
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;
}
}
標簽:
總結
以上是生活随笔為你收集整理的java redis 重连机制_redis在java中的客户端连接的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot 页面跳转后css和
- 下一篇: memset 结构体内指针_数据结构之线