java端在使用jedispool 连接redis的时候,在高并发的时候经常卡死,或报连接异常,JedisConnectionException,或者getResource 异常等各种问题
package
com.caspar.util;
import
org.apache.log4j.Logger;
import
redis.clients.jedis.Jedis;
import
redis.clients.jedis.JedisPool;
import
redis.clients.jedis.JedisPoolConfig;
/**
* Redis 工具类
* @author caspar
*/
public
class
RedisUtil {
protected
static
Logger logger = Logger.getLogger(RedisUtil.
class
);
private
static
String ADDR_ARRAY = FileUtil.getPropertyValue(
"/properties/redis.properties"
,
"server"
);
private
static
int
PORT = FileUtil.getPropertyValueInt(
"/properties/redis.properties"
,
"port"
);
private
static
int
MAX_ACTIVE = FileUtil.getPropertyValueInt(
"/properties/redis.properties"
,
"max_active"
);;
private
static
int
MAX_IDLE = FileUtil.getPropertyValueInt(
"/properties/redis.properties"
,
"max_idle"
);;
private
static
int
MAX_WAIT = FileUtil.getPropertyValueInt(
"/properties/redis.properties"
,
"max_wait"
);;
private
static
int
TIMEOUT = FileUtil.getPropertyValueInt(
"/properties/redis.properties"
,
"timeout"
);;
private
static
boolean
TEST_ON_BORROW = FileUtil.getPropertyValueBoolean(
"/properties/redis.properties"
,
"test_on_borrow"
);;
private
static
JedisPool jedisPool =
null
;
/**
* redis过期时间,以秒为单位
*/
public
final
static
int
EXRP_HOUR =
60
*
60
;
public
final
static
int
EXRP_DAY =
60
*
60
*
24
;
public
final
static
int
EXRP_MONTH =
60
*
60
*
24
*
30
;
/**
* 初始化Redis连接池
*/
private
static
void
initialPool(){
try
{
JedisPoolConfig config =
new
JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool =
new
JedisPool(config, ADDR_ARRAY.split(
","
)[
0
], PORT, TIMEOUT);
}
catch
(Exception e) {
logger.error(
"First create JedisPool error : "
+e);
try
{
JedisPoolConfig config =
new
JedisPoolConfig();
config.setMaxTotal(MAX_ACTIVE);
config.setMaxIdle(MAX_IDLE);
config.setMaxWaitMillis(MAX_WAIT);
config.setTestOnBorrow(TEST_ON_BORROW);
jedisPool =
new
JedisPool(config, ADDR_ARRAY.split(
","
)[
1
], PORT, TIMEOUT);
}
catch
(Exception e2){
logger.error(
"Second create JedisPool error : "
+e2);
}
}
}
/**
* 在多线程环境同步初始化
*/
private
static
synchronized
void
poolInit() {
if
(jedisPool ==
null
) {
initialPool();
}
}
/**
* 同步获取Jedis实例
* @return Jedis
*/
public
synchronized
static
Jedis getJedis() {
if
(jedisPool ==
null
) {
poolInit();
}
Jedis jedis =
null
;
try
{
if
(jedisPool !=
null
) {
jedis = jedisPool.getResource();
}
}
catch
(Exception e) {
logger.error(
"Get jedis error : "
+e);
}
finally
{
returnResource(jedis);
}
return
jedis;
}
/**
* 释放jedis资源
* @param jedis
*/
public
static
void
returnResource(
final
Jedis jedis) {
if
(jedis !=
null
&& jedisPool !=
null
) {
jedisPool.returnResource(jedis);
}
}
/**
* 设置 String
* @param key
* @param value
*/
public
static
void
setString(String key ,String value){
try
{
value = StringUtil.isEmpty(value) ?
""
: value;
getJedis().set(key,value);
}
catch
(Exception e) {
logger.error(
"Set key error : "
+e);
}
}
/**
* 设置 过期时间
* @param key
* @param seconds 以秒为单位
* @param value
*/
public
static
void
setString(String key ,
int
seconds,String value){
try
{
value = StringUtil.isEmpty(value) ?
""
: value;
getJedis().setex(key, seconds, value);
}
catch
(Exception e) {
logger.error(
"Set keyex error : "
+e);
}
}
/**
* 获取String值
* @param key
* @return value
*/
public
static
String getString(String key){
if
(getJedis() ==
null
|| !getJedis().exists(key)){
return
null
;
}
return
getJedis().get(key);
}
}