@Override public void onMessage(String key, String msg) { if (logger.isInfoEnabled()) { logger.info("redis event: " + key + " = " + msg); } if (msg.equals(Constants.REGISTER) || msg.equals(Constants.UNREGISTER)) { try { Jedis jedis = jedisPool.getResource(); boolean isBroken = false; try { doNotify(jedis, key); } catch (JedisConnectionException e){ isBroken = true; } finally { if(isBroken){ jedisPool.returnBrokenResource(jedis); } else { jedisPool.returnResource(jedis); } } } catch (Throwable t) { // TODO 通知失败没有恢复机制保障 logger.error(t.getMessage(), t); } } }
public String rpop(String key) { Jedis jedis = null; try { jedis = getJedis(); if (jedis == null) { logger.error("get jedis failed."); return null; } return jedis.rpop(key); } catch (JedisConnectionException e) { if (jedis != null) { jedis.close(); jedis = null; } logger.error("increame connect error:", e); } finally { returnJedisResource(jedis); } return null; }
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; }
@SuppressWarnings("finally") public boolean disconnect() { if (isConnected()) { try { outputStream.flush(); socket.close(); return true; } catch (IOException ex) { broken = true; throw new JedisConnectionException(ex); } finally { closeQuietly(socket); return true; } } return false; }
private static void sendCommand(final RedisOutputStream os, final byte[] command, final byte[]... args) { try { os.write(ASTERISK_BYTE); os.writeIntCrLf(args.length + 1); os.write(DOLLAR_BYTE); os.writeIntCrLf(command.length); os.write(command); os.writeCrLf(); for (final byte[] arg : args) { os.write(DOLLAR_BYTE); os.writeIntCrLf(arg.length); os.write(arg); os.writeCrLf(); } } catch (IOException e) { throw new JedisConnectionException(e); } }
private static Object process(final RedisInputStream is) { final byte b = is.readByte(); if (b == PLUS_BYTE) { return processStatusCodeReply(is); } else if (b == DOLLAR_BYTE) { return processBulkReply(is); } else if (b == ASTERISK_BYTE) { return processMultiBulkReply(is); } else if (b == COLON_BYTE) { return processInteger(is); } else if (b == MINUS_BYTE) { processError(is); return null; } else { throw new JedisConnectionException("Unknown reply: " + (char) b); } }
private static byte[] processBulkReply(final RedisInputStream is) { final int len = is.readIntCrLf(); if (len == -1) { return null; } final byte[] read = new byte[len]; int offset = 0; while (offset < len) { final int size = is.read(read, offset, (len - offset)); if (size == -1) throw new JedisConnectionException("It seems like server has closed the connection."); offset += size; } // read 2 more bytes for the command delimiter is.readByte(); is.readByte(); return read; }
@Test public void scriptExistsWithBrokenConnection() { Jedis deadClient = new Jedis(jedis.getClient().getHost(), jedis.getClient().getPort()); deadClient.auth("foobared"); deadClient.clientSetname("DEAD"); ClientKillerUtil.killClient(deadClient, "DEAD"); // sure, script doesn't exist, but it's just for checking connection try { deadClient.scriptExists("abcdefg"); } catch (JedisConnectionException e) { // ignore it } assertEquals(true, deadClient.getClient().isBroken()); deadClient.close(); }
@Test public void getErrorAfterConnectionReset() throws Exception { class TestConnection extends Connection { public TestConnection() { super("localhost", 6379); } protected boolean sendCommand(Command cmd, byte[]... args) { return super.sendCommand(cmd, args); } } TestConnection conn = new TestConnection(); try { conn.sendCommand(Command.HMSET, new byte[1024 * 1024 + 1][0]); fail("Should throw exception"); } catch (JedisConnectionException jce) { assertEquals("ERR Protocol error: invalid multibulk length", jce.getMessage()); } }
/** * Tests opening an SSL/TLS connection to redis with a custom hostname * verifier. This test should fail because "127.0.0.1" does not match the * certificate subject common name and there are no subject alternative names * in the certificate. */ @Test public void connectWithShardInfoAndCustomHostnameVerifierByIpAddress() { final URI uri = URI.create("rediss://127.0.0.1:6390"); final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); final SSLParameters sslParameters = new SSLParameters(); HostnameVerifier hostnameVerifier = new BasicHostnameVerifier(); JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier); shardInfo.setPassword("foobared"); Jedis jedis = new Jedis(shardInfo); try { jedis.get("foo"); Assert.fail("The code did not throw the expected JedisConnectionException."); } catch (JedisConnectionException e) { Assert.assertEquals("The JedisConnectionException does not contain the expected message.", "The connection to '127.0.0.1' failed ssl/tls hostname verification.", e.getMessage()); } try { jedis.close(); } catch (Throwable e1) { // Expected. } }
@Test(expected = JedisConnectionException.class) public void timeoutConnection() throws Exception { jedis = new Jedis("localhost", 6379, 15000); jedis.auth("foobared"); jedis.configSet("timeout", "1"); Thread.sleep(2000); jedis.hmget("foobar", "foo"); }
public String get(String key) { Jedis jedis = null; String result = ""; try { jedis = getJedis(); if (jedis != null) { result = jedis.get(key); } else { logger.error("get opt connection null error!"); } } catch (JedisConnectionException e) { if (jedis != null) { jedis.close(); jedis = null; } logger.error("get value connect error:", e); } finally { returnJedisResource(jedis); } return result; }
public void set(String key, Map<String, String> map) { Jedis jedis = null; try { jedis = getJedis(); if (jedis != null) { jedis.hmset(key, map); } else { logger.error("hmset opt connection null error!"); } } catch (JedisConnectionException e) { if (jedis != null) { jedis.close(); jedis = null; } logger.error("hmset connect error:", e); } finally { returnJedisResource(jedis); } }
public void hset(String key, String field, String value) { Jedis jedis = null; try { jedis = getJedis(); if (jedis != null) { jedis.hset(key, field, value); } else { logger.error("hset opt connection null error!"); } } catch (JedisConnectionException e) { if (jedis != null) { jedis.close(); jedis = null; } logger.error("hmset connect error:", e); } finally { returnJedisResource(jedis); } }
public Map<String, String> getHash(String key) { Jedis jedis = null; Map<String, String> result = null; try { jedis = getJedis(); if (jedis != null) { result = jedis.hgetAll(key); } else { logger.error("hgetall opt connection null error!"); } } catch (JedisConnectionException e) { if (jedis != null) { jedis.close(); jedis = null; } logger.error("hgetall value connect error:", e); } finally { returnJedisResource(jedis); } return result; }
public void del(String key) { Jedis jedis = null; try { jedis = getJedis(); if (jedis != null) { jedis.del(key); } else { logger.error("del opt connection null error!"); } } catch (JedisConnectionException e) { if (jedis != null) { jedis.close(); jedis = null; } logger.error("delete connect error:", e); } finally { returnJedisResource(jedis); } }
public long increame(String key) { Jedis jedis = null; try { jedis = getJedis(); if (jedis != null) { return jedis.incr(key); } else { logger.error("increame opt connection null error!"); } } catch (JedisConnectionException e) { if (jedis != null) { jedis.close(); jedis = null; } logger.error("increame connect error:", e); } finally { returnJedisResource(jedis); } return 0L; }
public void expire(String key, int seconds) { Jedis jedis = null; try { jedis = getJedis(); if (jedis != null) { jedis.expire(key, seconds); } else { logger.error("increame opt connection null error!"); } } catch (JedisConnectionException e) { if (jedis != null) { jedis.close(); jedis = null; } logger.error("increame connect error:", e); } finally { returnJedisResource(jedis); } }
public void lpush(String key, String ele) { Jedis jedis = null; try { jedis = getJedis(); if (jedis == null) { logger.error("get jedis failed."); } jedis.lpush(key, ele); } catch (JedisConnectionException e) { if (jedis != null) { jedis.close(); jedis = null; } logger.error("increame connect error:", e); } finally { returnJedisResource(jedis); } }
public Long llen(String key) { Jedis jedis = null; try { jedis = getJedis(); if (jedis == null) { logger.error("get jedis fail"); return null; } return jedis.llen(key); } catch (JedisConnectionException e) { if (jedis != null) { jedis.close(); } } finally { returnJedisResource(jedis); } return null; }
public Long zcard(String key) { Jedis jedis = null; try { jedis = getJedis(); if (jedis == null) { logger.error("get jedis fail"); return null; } return jedis.zcard(key); } catch (JedisConnectionException e) { if (jedis != null) { jedis.close(); } } finally { returnJedisResource(jedis); } return null; }
public void zrem(String key, String member) { Jedis jedis = null; try { jedis = getJedis(); if (jedis == null) { logger.error("get jedis fail"); } jedis.zrem(key, member); } catch (JedisConnectionException e) { if (jedis != null) { jedis.close(); } } finally { returnJedisResource(jedis); } }
public List<String> lrange(String key) { Jedis jedis = null; try { jedis = getJedis(); if (jedis == null) { logger.error("get jedis fail"); } List<String> list = jedis.lrange(key, 0, 50); return list; } catch (JedisConnectionException e) { if (jedis != null) { jedis.close(); } } finally { returnJedisResource(jedis); } return null; }
private void initializeSlotsCache(Set<HostAndPort> startNodes, GenericObjectPoolConfig poolConfig) { for (HostAndPort hostAndPort : startNodes) { Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort()); try { cache.discoverClusterNodesAndSlots(jedis); break; } catch (JedisConnectionException e) { // try next nodes } finally { if (jedis != null) { jedis.close(); } } } for (HostAndPort node : startNodes) { cache.setNodeIfNotExist(node); } }
/** {@inheritDoc} */ @Override public void sAdd(String key, String value) { int tries = 0; boolean sucess = false; do { tries++; try { Jedis jedis = pool.getResource(); jedis.sadd(key, value); jedis.close(); sucess = true; } catch (JedisConnectionException ex) { log.error(RedisConstants.CONN_FAILED_RETRY_MSG + tries); if (tries == numRetries) throw ex; } } while (!sucess && tries <= numRetries); }
/** {@inheritDoc} */ @Override public void zAdd(String key, Object data, double score) { int tries = 0; boolean sucess = false; do { tries++; try { Jedis jedis = pool.getResource(); jedis.zadd(key, score, SerializationUtil.serialize(data)); jedis.close(); sucess = true; } catch (JedisConnectionException ex) { log.error(RedisConstants.CONN_FAILED_RETRY_MSG + tries); if (tries == numRetries) throw ex; } } while (!sucess && tries <= numRetries); }
/** {@inheritDoc} */ @Override public Boolean zExists(String key, Object data) { int tries = 0; boolean sucess = false; boolean retVal = false; do { tries++; try { Jedis jedis = pool.getResource(); retVal = (jedis.zscore(key, SerializationUtil.serialize(data)) != null); jedis.close(); sucess = true; } catch (JedisConnectionException ex) { log.error(RedisConstants.CONN_FAILED_RETRY_MSG + tries); if (tries == numRetries) throw ex; } } while (!sucess && tries <= numRetries); return retVal; }
/** {@inheritDoc} */ @Override public Long zRemove(String key, Object... data) { int tries = 0; Long retVal = null; boolean sucess = false; do { tries++; try { Jedis jedis = pool.getResource(); retVal = jedis.zrem(key, SerializationUtil.serialize(data)); jedis.close(); sucess = true; } catch (JedisConnectionException ex) { log.error(RedisConstants.CONN_FAILED_RETRY_MSG + tries); if (tries == numRetries) throw ex; } } while (!sucess && tries <= numRetries); return retVal; }
/** {@inheritDoc} */ @Override public Set<Object> zGet(String key, double minScore, double maxScore, int offset, int count) { int tries = 0; boolean sucess = false; Set<String> retVal = null; do { tries++; try { Jedis jedis = pool.getResource(); retVal = jedis.zrangeByScore(key, minScore, maxScore, offset, count); jedis.close(); sucess = true; } catch (JedisConnectionException ex) { log.error(RedisConstants.CONN_FAILED_RETRY_MSG + tries); if (tries == numRetries) throw ex; } } while (!sucess && tries <= numRetries); return SerializationUtil.deSerialize(retVal); }
private JedisPool toJedisPool0() { return new JedisPool(new GenericObjectPoolConfig(), this.getHost(), this.getPort(), this.getTimeout(), this.getPassword(), this.getDatabase()) { @Override public Jedis getResource() { try { return super.getResource(); } catch (JedisConnectionException var2) { RedisConfig.LOGGER.error(RedisConfig.this.toString(), var2); throw new JedisConnectionException(RedisConfig.this.toString(), var2); } } @Override public void close() { } }; }
/** {@inheritDoc} */ @Override public Set<String> sMembers(String key) { int tries = 0; boolean sucess = false; Set<String> retVal = null; do { tries++; try { retVal = cluster.smembers(key); sucess = true; } catch (JedisClusterMaxRedirectionsException | JedisConnectionException ex) { log.error(RedisConstants.CONN_FAILED_RETRY_MSG + tries); if (tries == numRetries) { throw ex; } waitforFailover(); } } while (!sucess && tries <= numRetries); return retVal; }
/** {@inheritDoc} */ @Override public void zAdd(String key, Object data, double score) { int tries = 0; boolean sucess = false; do { tries++; try { cluster.zadd(key, score, SerializationUtil.serialize(data)); sucess = true; } catch (JedisClusterMaxRedirectionsException | JedisConnectionException ex) { log.error(RedisConstants.CONN_FAILED_RETRY_MSG + tries); if (tries == numRetries) { throw ex; } waitforFailover(); } } while (!sucess && tries <= numRetries); }
/** {@inheritDoc} */ @Override public Boolean zExists(String key, Object data) { int tries = 0; boolean sucess = false; boolean retVal = false; do { tries++; try { retVal = (cluster.zscore(key, SerializationUtil.serialize(data)) != null); sucess = true; } catch (JedisClusterMaxRedirectionsException | JedisConnectionException ex) { log.error(RedisConstants.CONN_FAILED_RETRY_MSG + tries); if (tries == numRetries) { throw ex; } waitforFailover(); } } while (!sucess && tries <= numRetries); return retVal; }
/** {@inheritDoc} */ @Override public Long zRemove(String key, Object... data) { int tries = 0; Long retVal = null; boolean sucess = false; do { tries++; try { retVal = cluster.zrem(key, SerializationUtil.serialize(data)); sucess = true; } catch (JedisClusterMaxRedirectionsException | JedisConnectionException ex) { log.error(RedisConstants.CONN_FAILED_RETRY_MSG + tries); if (tries == numRetries) { throw ex; } waitforFailover(); } } while (!sucess && tries <= numRetries); return retVal; }
/** {@inheritDoc} */ @Override public Set<Object> zGet(String key, double minScore, double maxScore, int offset, int count) { int tries = 0; boolean sucess = false; Set<String> retVal = null; do { tries++; try { retVal = cluster.zrangeByScore(key, minScore, maxScore, offset, count); sucess = true; } catch (JedisClusterMaxRedirectionsException | JedisConnectionException ex) { log.error(RedisConstants.CONN_FAILED_RETRY_MSG + tries); if (tries == numRetries) { throw ex; } waitforFailover(); } } while (!sucess && tries <= numRetries); return SerializationUtil.deSerialize(retVal); }
/** {@inheritDoc} */ @Override public Double zScore(String key, Object data) { int tries = 0; boolean sucess = false; Double retVal = null; do { tries++; try { retVal = cluster.zscore(key, SerializationUtil.serialize(data)); sucess = true; } catch (JedisClusterMaxRedirectionsException | JedisConnectionException ex) { log.error(RedisConstants.CONN_FAILED_RETRY_MSG + tries); if (tries == numRetries) { throw ex; } waitforFailover(); } } while (!sucess && tries <= numRetries); return retVal; }
/** {@inheritDoc} */ @Override public Long zCount(String key) { int tries = 0; boolean sucess = false; Long retVal = null; do { tries++; try { retVal = cluster.zcount(key, 0, Double.MAX_VALUE); sucess = true; } catch (JedisClusterMaxRedirectionsException | JedisConnectionException ex) { log.error(RedisConstants.CONN_FAILED_RETRY_MSG + tries); if (tries == numRetries) { throw ex; } waitforFailover(); } } while (!sucess && tries <= numRetries); return retVal; }