Jedis単点構成
6369 ワード
pom jarパッケージの導入
スプリング構成
redis.propertiesプロファイル
RedisUtil.JAvaツールクラス
使用方法
redis.clients
jedis
2.9.0
スプリング構成
redis.propertiesプロファイル
redis.ip=127.0.0.1
redis.port=6379
redis.timeout=10000
#
redis.pool.maxActive=1024
# idel
redis.pool.maxIdle=50
# ,
redis.pool.maxWait=2000
# borrow Object ,
redis.pool.testOnBorrow=true
RedisUtil.JAvaツールクラス
/**
*Class Description:
*
*/
package com.redis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
/**
* redis
*
* @author
*
*/
public class RedisUtil {
private static final Logger log = LoggerFactory.getLogger(RedisUtil.class);
private static int connCount = 0;//
/**
*
*/
private ShardedJedisPool shardedJedisPool;
/**
*
*
* @return conn
*/
public ShardedJedis getConnFromShardedPool() {
ShardedJedis shardedJedis = null;
try {
shardedJedis = shardedJedisPool.getResource();
} catch (Exception e) {
log.error("Get ShardedJedis from ShardedJedisPool failed...", e);
e.printStackTrace();
}
return shardedJedis;
}
/**
* @ :
*
* @return conn
*/
public ShardedJedis getSafeConnFromShardedPool() {
// , 20
int maxTryCount = 20;
ShardedJedis shardedJedis = null;
for (int i = 0; i < maxTryCount; i++) {
try {
shardedJedis = shardedJedisPool.getResource();
break;
} catch (Exception e) {
//
if (i == maxTryCount) {
break;
}
log.error(
"==========>>Get ShardedJedis from ShardedJedisPool failed., "
+ (i + 1) + " , " + maxTryCount
+ ", 。", e);
e.printStackTrace();
try {
// 1
Thread.sleep(1000);
} catch (Exception e2) {
log.error("", e);
}
}
}
if (shardedJedis != null) {
synchronized (this) {
connCount++;
log.info("Get ShardedJedis from ShardedJedisPool success.... ="
+ connCount);
}
}
return shardedJedis;
}
/**
*
*
* @param conn
*/
public void closeConnShardedJedis(ShardedJedis shardedJedis) {
if (null != shardedJedis) {
try {
shardedJedisPool.returnResource(shardedJedis);
} catch (Exception e) {
log.error("Close shardedJedis connection failed.");
e.printStackTrace();
}
}
synchronized (this) {
connCount--;
log.info("==========>>Close ShardedJedis from ShardedJedisPool success.... ="
+ connCount);
}
}
/**
*
*
* @param conn
*/
public boolean setData(String key, String value) {
try {
ShardedJedis jedis = shardedJedisPool.getResource();
jedis.set(key, value);
shardedJedisPool.returnResource(jedis);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
*
*
* @param conn
*/
public String getData(String key) {
String value = null;
try {
ShardedJedis jedis = shardedJedisPool.getResource();
value = jedis.get(key);
shardedJedisPool.returnResource(jedis);
return value;
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
/**
*
*
* @return
*/
public ShardedJedisPool getShardedJedisPool() {
return shardedJedisPool;
}
/**
*
*
* @return
*/
public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
this.shardedJedisPool = shardedJedisPool;
}
}
使用方法
@Autowired
private RedisUtil redisUtil;
public static void main(String[] args) {
ShardedJedis sj = null;
try {
sj = redisUtil.getSafeConnFromShardedPool();
sj.set("testkey","testvalue");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sj != null) {
redisUtil.closeConnShardedJedis(sj);
}
}
}