Привет всем, я пытаюсь настроить сервер Redis с помощью java. Мой сервер Redis - это Linux-сервер с неограниченным ulimit.
Вот мой весенний бин, который создает связь
открытый класс JedisService реализует IJedisService, InitializingBean, DisposableBean {private JedisPool jedisPool;
public JedisService() {
}
public JedisPool getJedisPool() {
return jedisPool;
}
@Override
public void destroy() throws Exception {
if(jedisPool != null){
jedisPool.destroy();
}
}
@Override
public void afterPropertiesSet() throws Exception {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxActive(1000);
poolConfig.setMaxIdle(10);
poolConfig.setMinIdle(1);
poolConfig.setMaxWait(30000);
poolConfig.setNumTestsPerEvictionRun(10);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true);
poolConfig.setTimeBetweenEvictionRunsMillis(30000);
jedisPool = new JedisPool(poolConfig,Config.REDIS_HOST,
RestrictionUtils.REDIS_PORT,RestrictionUtils.REDIS_CONNECTION_TIMEOUT);
}
public boolean validateHitsCount(String hostKey,String urlKey,int hostHitsCount, int urlHitsCount, long timeKey,Map<String, Object> overLimit){
int retryCount = 0;
return recursiveRedisConnection(retryCount,hostKey,urlKey,hostHitsCount,urlHitsCount,timeKey,overLimit);
}
private boolean recursiveRedisConnection(int retryCount,String hostKey,String urlKey,int hostHitsCount, int urlHitsCount, long timeKey,Map<String, Object> overLimit){
Jedis jedis = null;
boolean returnObj = true;
try {
//Connection
jedis = getJedisPool().getResource();
jedis.connect();
//Delete previous keys
try {
deletePreviouskeys(jedis, urlKey, hostKey, timeKey);
} catch (Exception e) {
}
//Validation
String value = jedis.get(hostKey+timeKey);
if(value != null){
try {
int count = Integer.parseInt(value);
System.out.println("HostCount: "+hostKey+ " "+count);
if(count < hostHitsCount){
jedis.incr(hostKey+timeKey);
returnObj = validateURLHits(jedis, urlKey, urlHitsCount, timeKey, overLimit);
}else{
returnObj = false;
overLimit.put("Data", "You have reached maximum limit for hits---"+hostKey);
}
} catch (Exception e) {
}
}else{
value = 1+"";
jedis.set(hostKey+timeKey, value);
jedis.expire(hostKey+timeKey, 60);
returnObj = validateURLHits(jedis, urlKey, urlHitsCount, timeKey, overLimit);
}
} catch (Exception e) {
retryCount ++;
if(retryCount < 3){
recursiveRedisConnection(retryCount,hostKey,urlKey,hostHitsCount,urlHitsCount,timeKey,overLimit);
}else{
e.printStackTrace();
returnObj = false;
}
}finally{
if(jedis != null && jedis.isConnected()){
getJedisPool().returnResource(jedis);
}
}
return returnObj;
}
private boolean validateURLHits(Jedis jedis,String urlKey, int urlHitsCount, long timeKey,Map<String, Object> overLimit){
boolean returnObj = true;
String value = jedis.get(urlKey+timeKey);
if(value != null){
try {
int count = Integer.parseInt(value);
System.out.println("URLCount: "+urlKey+ " "+count);
if(count < urlHitsCount){
jedis.incr(urlKey+timeKey);
}else{
returnObj = false;
overLimit.put("Data", "Reached maximum limit of hits for this URL");
}
}catch(Exception e){
}
}else{
jedis.set(urlKey+timeKey, 1+"");
jedis.expire(urlKey+timeKey, 60);
}
return returnObj;
}
private void deletePreviouskeys(Jedis jedis,String urlKey, String hostKey, long timeKey){
/*Set<String> keys = jedis.keys("*");
for(String key : keys){
if(!key.equalsIgnoreCase(urlKey+timeKey)){
if(!key.equalsIgnoreCase(hostKey+timeKey)){
jedis.del(key);
}
}
}*/
}
}
В контроллере вызывается метод validateURLHits. Но когда я запускаю этот код в нескольких потоках, я получаю эту ошибку
Вы проверяли, что ваш JedisPool создается только один раз и не более?