Java 无法从池JedisConnectionException获取资源

Java 无法从池JedisConnectionException获取资源,java,redis,jedis,Java,Redis,Jedis,大家好,我正在尝试使用java设置redis服务器。 我的Redis服务器是带有ulimit unlimited的linux服务器 这是我的Springbean,它创建了连接 公共类JedisService实现IJedisService、initializebean、DisposableBean{ 私人绝地武士 public JedisService() { } public JedisPool getJedisPool() { return jedisPool; } @Overr

大家好,我正在尝试使用java设置redis服务器。 我的Redis服务器是带有ulimit unlimited的linux服务器

这是我的Springbean,它创建了连接

公共类JedisService实现IJedisService、initializebean、DisposableBean{ 私人绝地武士

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);
            }
        }

    }*/
}
public-JedisService(){
}
public JedisPool getJedisPool(){
返回滑阀;
}
@凌驾
public void destroy()引发异常{
if(spool!=null){
销毁();
}
}
@凌驾
public void afterPropertieSet()引发异常{
JedisPoolConfig poolConfig=新的JedisPoolConfig();
poolConfig.setMaxActive(1000);
poolConfig.setMaxIdle(10);
poolConfig.setMinIdle(1);
poolConfig.setMaxWait(30000);
setNumtestsPervictionRun(10);
poolConfig.settstonbrow(true);
poolConfig.settstonReturn(true);
poolConfig.setTestWhileIdle(true);
poolConfig.SetTimeBetweenVictionRunsmillis(30000);
jedisPool=新的jedisPool(poolConfig,Config.REDIS_HOST,
RestrictionUtils.REDIS\u端口、RestrictionUtils.REDIS\u连接\u超时);
}
public boolean validateHitsCount(字符串hostKey、字符串urlKey、int hosthitsunt、int urlhitsunt、长时间键、映射超限){
int retryCount=0;
返回RecursiveDisconnection(retryCount、hostKey、urlKey、hostHitsCount、urlHitsCount、timeKey、overLimit);
}
私有布尔RecursiveDisconnection(int-retryCount、String-hostKey、String-urlKey、int-HostHitScont、int-UrlHitScont、long-timeKey、映射超限){
绝地=零;
布尔returnObj=true;
试一试{
//联系
jedis=getJedisPool().getResource();
绝地武士。连接();
//删除以前的键
试一试{
DeletePreviousKey(绝地、urlKey、主机密钥、时间密钥);
}捕获(例外e){
}
//验证
字符串值=jedis.get(主机键+时间键);
if(值!=null){
试一试{
int count=Integer.parseInt(值);
System.out.println(“主机计数:“+hostKey+”+count);
如果(计数
}

validateURLHits方法在控制器中调用。 但是当我在多个线程中运行这段代码时,我得到了这个错误

redis.clients.jedis.exceptions.JedisConnectionException:无法从池中获取资源 位于redis.clients.util.Pool.getResource(Pool.java:22) 在com.til.ibeat.service.JedisService.recursiveRedisConnection上(JedisService.java:60) 在com.til.ibeat.service.JedisService.recursiveRedisConnection上(JedisService.java:95) 在com.til.ibeat.service.JedisService.recursiveRedisConnection上(JedisService.java:95) 在com.til.ibeat.service.JedisService.validateHitsCount上(JedisService.java:52) 位于com.til.ibeat.controller.MashupController.handleRequest(MashupController.java:66) 位于org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48) 位于org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:763) 位于org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:709) 位于org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:613) 位于org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:525) 位于javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 位于javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 位于org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 位于org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 位于org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 位于org.apache.catalina.core.StandardContextValve.in