public synchronized void recover() { logger.debug("recover..."); if (status.equals(STATUS.READY)) { logger.debug("The redis's status is ready, return void."); return; } jedisPool.returnBrokenResource(subscriber); subscriber = null; subscriber = jedisPool.getResource(); status = STATUS.INIT; new Thread( new Runnable() { @Override public void run() { try { logger.debug("重新预订阅主题..."); subscriber.subscribe(listener, "~~~~~~~"); } catch (Exception e) { logger.error(e.getMessage(), e); logger.error("预订阅退出..."); status = STATUS.BROKEN; } } }) .start(); status = STATUS.READY; logger.debug("recover done"); }
/** Try lock with value */ public Boolean tryLock() { if (isTimeout()) { return true; } Jedis jedis = null; try { jedis = pool.getResource(); String newHoldValue = identity + getTimestamp(); String result = jedis.set(key, newHoldValue, "NX", "PX", expireTime); if ("OK".equals(result)) { lastHoldValue = newHoldValue; return true; } return false; } catch (JedisConnectionException e) { if (jedis != null) { pool.returnBrokenResource(jedis); } updateLastFailedTime(); throw e; } finally { if (jedis != null) { pool.returnResource(jedis); } } }
private Map<String, Double> getPrices(JedisPool pool, boolean openingPrices) { Map<String, Double> prices = new HashMap<String, Double>(); Jedis jedis = null; try { jedis = pool.getResource(); String[] keys = new String[TICKER_SYMBOLS.length]; int i = 0; for (String symbol : TICKER_SYMBOLS) { symbol = (openingPrices) ? symbol + ".start" : symbol; keys[i++] = "stock." + symbol; } List<String> values = jedis.mget(keys); i = 0; for (String value : values) { if (value == null) { throw new IllegalStateException( "Redis should contain a value for stock " + TICKER_SYMBOLS[i++]); } prices.put(TICKER_SYMBOLS[i++], Double.valueOf(value)); } pool.returnResource(jedis); } catch (RuntimeException re) { if (jedis != null) pool.returnBrokenResource(jedis); } return prices; }
public void clear() { Jedis jedis = jedisPool.getResource(); try { jedis.del(name); jedisPool.returnResource(jedis); } catch (Exception e) { jedisPool.returnBrokenResource(jedis); } }
public void set(String value) { Jedis jedis = jedisPool.getResource(); try { jedis.set(name, value); jedisPool.returnResource(jedis); } catch (Exception e) { jedisPool.returnBrokenResource(jedis); } }
public static void release(Jedis jedis, boolean isBroken) { if (jedis != null) { if (isBroken) { jedisPool.returnBrokenResource(jedis); } else { jedisPool.returnResource(jedis); } } }
protected Jedis getJedis() { Jedis jedis = null; try { jedis = pool.getResource(); return jedis; } catch (JedisConnectionException e) { log.error("", e); if (jedis != null) pool.returnBrokenResource(jedis); } return null; }
public String get() { Jedis jedis = jedisPool.getResource(); String res = null; try { res = jedis.get(name); jedisPool.returnResource(jedis); } catch (Exception e) { jedisPool.returnBrokenResource(jedis); } return res; }
/** * Return jedis connection to the pool, call different return methods depends on the * conectionBroken status. */ protected void closeResource(Jedis jedis, boolean conectionBroken) { try { if (conectionBroken) { jedisPool.returnBrokenResource(jedis); } else { jedisPool.returnResource(jedis); } } catch (Exception e) { logger.error("return back jedis failed, will fore close the jedis.", e); JedisUtils.destroyJedis(jedis); } }
@Override public String getRedisData(String subject) { Jedis resource = null; String result = null; try { resource = jedisPool.getResource(); result = resource.get(subject); } catch (Exception e) { logger.error(e.getMessage(), e); jedisPool.returnBrokenResource(resource); } finally { if (resource != null) { jedisPool.returnResource(resource); } } return result; }
private void initializeRedis(JedisPool pool) { Jedis jedis = null; try { jedis = pool.getResource(); for (String ticker : TICKER_SYMBOLS) { String key = "stock." + ticker; if (jedis.get(key + ".start") != null) continue; String value = String.valueOf((int) (1000.0 - 200.0 * random.nextFloat())); jedis.set(key, value); jedis.set(key + ".start", value); } pool.returnResource(jedis); } catch (RuntimeException re) { if (jedis != null) pool.returnBrokenResource(jedis); } }
public void unlock() { if (isTimeout()) { return; } Jedis jedis = null; try { jedis = pool.getResource(); jedis.eval(buildDeleteScript(), 1, key, lastHoldValue); } catch (JedisConnectionException e) { if (jedis != null) { pool.returnBrokenResource(jedis); } } finally { if (jedis != null) { pool.returnResource(jedis); } } }
/** * 获取数据 * * @param key * @return */ public static String get(String key) { String value = null; JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); value = jedis.get(key); } catch (Exception e) { // 释放redis对象 pool.returnBrokenResource(jedis); e.printStackTrace(); } finally { // 返还到连接池 returnResource(pool, jedis); } return value; }
@Override public void addClientId(String clientId) { Jedis resource = null; try { resource = jedisPool.getResource(); resource.sadd("clientId", clientId); Set<String> list = resource.smembers("clientId"); for (String s : list) { System.out.println(">>>>" + s); } } catch (Exception e) { logger.error(e.getMessage(), e); jedisPool.returnBrokenResource(resource); } finally { if (resource != null) { jedisPool.returnResource(resource); } } }
@Override public void run() { lock.readLock().lock(); try { if (!writableMap.isEmpty()) { JedisPool pool = writableMap.get(getARandomJedis(writableMap)); Jedis jedis = null; try { jedis = pool.getResource(); initializeRedis(pool); int increment = (int) (random.nextFloat() * 100.0 - 48.0); jedis.incrBy( "stock." + TICKER_SYMBOLS[random.nextInt(TICKER_SYMBOLS.length)], increment); pool.returnResource(jedis); } catch (RuntimeException re) { if (jedis != null) pool.returnBrokenResource(jedis); } } } catch (Exception e) { // Just swallow this } finally { lock.readLock().unlock(); } }
public void returnBrokenResource(Jedis redis) { pool.returnBrokenResource(redis); }