Java Code Examples for redis.clients.jedis.JedisPool#returnBrokenResource()

The following examples show how to use redis.clients.jedis.JedisPool#returnBrokenResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedisRegistry.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
public boolean isAvailable() {
       for (JedisPool jedisPool : jedisPools.values()) {
           Jedis jedis = jedisPool.getResource();
           boolean isBroken = false;
           try {
               if (jedis.isConnected()) {
                   return true; // 至少需单台机器可用
               }
           } catch (JedisConnectionException e) {
               isBroken = true;
           } finally {
               if (isBroken) {
                   jedisPool.returnBrokenResource(jedis);
               } else {
                   jedisPool.returnResource(jedis);
               }
           }
       }
       return false;
   }
 
Example 2
Source File: RedisRegistry.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public boolean isAvailable() {
    for (JedisPool jedisPool : jedisPools.values()) {
        Jedis jedis = jedisPool.getResource();
        boolean isBroken = false;
        try {
            if (jedis.isConnected()) {
                return true; // 至少需单台机器可用
            }
        } catch (JedisConnectionException e) {
            isBroken = true;
        } finally {
            if (isBroken) {
                jedisPool.returnBrokenResource(jedis);
            } else {
                jedisPool.returnResource(jedis);
            }
        }
    }
    return false;
}
 
Example 3
Source File: RedisRegistry.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public boolean isAvailable() {
    for (JedisPool jedisPool : jedisPools.values()) {
        Jedis jedis = jedisPool.getResource();
        boolean isBroken = false;
        try {
            if (jedis.isConnected()) {
                return true; // 至少需单台机器可用
            }
        } catch (JedisConnectionException e) {
            isBroken = true;
        } finally {
            if (isBroken) {
                jedisPool.returnBrokenResource(jedis);
            } else {
                jedisPool.returnResource(jedis);
            }
        }
    }
    return false;
}
 
Example 4
Source File: RedisUtils.java    From pinlater with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the connection from the connection pool and adds the wrapper catch/finally block for the
 * given function.
 *
 * This helper method saves the trouble of dealing with redis connection. When we got
 * JedisConnectionException, we will discard this connection. Otherwise, we return the connection
 * to the connection pool.
 *
 * @param jedisPool Jedis connection pool
 * @param redisDBNum Redis DB number (index) (if redisDBNum == -1, don't select a DB )
 * @param func    The function to execute inside the catch/finally block.
 * @return A Resp object, which is the return value of wrapped function.
 */
public static <Resp> Resp executeWithConnection(JedisPool jedisPool,
                                                int redisDBNum,
                                                Function<Jedis, Resp> func) {
  Preconditions.checkNotNull(jedisPool);
  Preconditions.checkNotNull(func);
  Jedis conn = null;
  boolean gotJedisConnException = false;
  try {
    conn = jedisPool.getResource();
    selectRedisDB(conn, redisDBNum);
    return func.apply(conn);
  } catch (JedisConnectionException e) {
    jedisPool.returnBrokenResource(conn);
    gotJedisConnException = true;
    throw e;
  } finally {
    if (conn != null && !gotJedisConnException) {
      jedisPool.returnResource(conn);
    }
  }
}
 
Example 5
Source File: RedisRegistry.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private void deferExpired() {
       for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
           JedisPool jedisPool = entry.getValue();
           boolean isBroken = false;
           try {
               Jedis jedis = jedisPool.getResource();
               try {
                   for (URL url : new HashSet<URL>(getRegistered())) {
                       if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
                           String key = toCategoryPath(url);
                           if (jedis.hset(key, url.toFullString(), String.valueOf(System.currentTimeMillis() + expirePeriod)) == 1) {
                               jedis.publish(key, Constants.REGISTER);
                           }
                       }
                   }
                   if (admin) {
                       clean(jedis);
                   }
                   if (!replicate) {
                       break;//  如果服务器端已同步数据,只需写入单台机器
                   }
               } catch (JedisConnectionException e){
                   isBroken = true;
               } finally {
                   if(isBroken){
                       jedisPool.returnBrokenResource(jedis);
                   } else {
                       jedisPool.returnResource(jedis);
                   }
               }
           } catch (Throwable t) {
               logger.warn("Failed to write provider heartbeat to redis registry. registry: " + entry.getKey() + ", cause: " + t.getMessage(), t);
           }
       }
   }
 
Example 6
Source File: RedisRegistry.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
private void deferExpired() {
    for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
        JedisPool jedisPool = entry.getValue();
        boolean isBroken = false;
        try {
            Jedis jedis = jedisPool.getResource();
            try {
                for (URL url : new HashSet<URL>(getRegistered())) {
                    if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
                        String key = toCategoryPath(url);
                        if (jedis.hset(key, url.toFullString(), String.valueOf(System.currentTimeMillis() + expirePeriod)) == 1) {
                            jedis.publish(key, Constants.REGISTER);
                        }
                    }
                }
                if (admin) {
                    clean(jedis);
                }
                if (!replicate) {
                    break;//  如果服务器端已同步数据,只需写入单台机器
                }
            } catch (JedisConnectionException e){
                isBroken = true;
            } finally {
                if(isBroken){
                    jedisPool.returnBrokenResource(jedis);
                } else {
                    jedisPool.returnResource(jedis);
                }
            }
        } catch (Throwable t) {
            logger.warn("Failed to write provider heartbeat to redis registry. registry: " + entry.getKey() + ", cause: " + t.getMessage(), t);
        }
    }
}
 
Example 7
Source File: RedisRegistry.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void deferExpired() {
    for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
        JedisPool jedisPool = entry.getValue();
        boolean isBroken = false;
        try {
            Jedis jedis = jedisPool.getResource();
            try {
                for (URL url : new HashSet<URL>(getRegistered())) {
                    if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
                        String key = toCategoryPath(url);
                        if (jedis.hset(key, url.toFullString(), String.valueOf(System.currentTimeMillis() + expirePeriod)) == 1) {
                            jedis.publish(key, Constants.REGISTER);
                        }
                    }
                }
                if (admin) {
                    clean(jedis);
                }
                if (!replicate) {
                    break;//  如果服务器端已同步数据,只需写入单台机器
                }
            } catch (JedisConnectionException e){
                isBroken = true;
            } finally {
                if(isBroken){
                    jedisPool.returnBrokenResource(jedis);
                } else {
                    jedisPool.returnResource(jedis);
                }
            }
        } catch (Throwable t) {
            logger.warn("Failed to write provider heartbeat to redis registry. registry: " + entry.getKey() + ", cause: " + t.getMessage(), t);
        }
    }
}
 
Example 8
Source File: JRedisCache.java    From springJredisCache with Apache License 2.0 3 votes vote down vote up
/**
 * 运行时异常,IO异常,销毁jedis对象
 *
 * @param ex
 * @param jedisPool
 * @param jedis
 */
protected void coverException(Exception ex, JedisPool jedisPool, Jedis jedis) {
    if (jedis == null) throw new NullPointerException();
    if (ex instanceof JRedisCacheException || ex instanceof IOException) {
        jedisPool.returnBrokenResource(jedis); //销毁该对象
    }
}