Java 类redis.clients.jedis.exceptions.JedisConnectionException 实例源码

项目:dubbocloud    文件:RedisRegistry.java   
@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);
        }
    }
}
项目:newblog    文件:JedisUtil.java   
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;
}
项目:dubbo2    文件:RedisRegistry.java   
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;
}
项目:JRediClients    文件:Connection.java   
@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;
}
项目:JRediClients    文件:Protocol.java   
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);
    }
}
项目:JRediClients    文件:Protocol.java   
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);
        }
    }
项目:JRediClients    文件:Protocol.java   
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;
}
项目:x7    文件:Protocol.java   
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);
    }
  }
项目:JRediClients    文件:ScriptingCommandsTest.java   
@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();
}
项目:JRediClients    文件:ConnectionTest.java   
@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());
  }
}
项目:JRediClients    文件:SSLJedisTest.java   
/**
 * 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.
  }
}
项目:JRediClients    文件:JedisTest.java   
@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");
}
项目:newblog    文件:JedisUtil.java   
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;
}
项目:newblog    文件:JedisUtil.java   
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);
    }
}
项目:newblog    文件:JedisUtil.java   
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);
    }
}
项目:newblog    文件:JedisUtil.java   
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;
}
项目:newblog    文件:JedisUtil.java   
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);
    }
}
项目:newblog    文件:JedisUtil.java   
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;
}
项目:newblog    文件:JedisUtil.java   
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);
    }
}
项目:newblog    文件:JedisUtil.java   
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);
    }
}
项目:newblog    文件:JedisUtil.java   
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;
}
项目:newblog    文件:JedisUtil.java   
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;
}
项目:newblog    文件:JedisUtil.java   
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);
    }
}
项目:newblog    文件:JedisUtil.java   
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;
}
项目:x7    文件:JedisClusterConnectionHandler.java   
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);
  }
}
项目:distributed-task-scheduler    文件:RedisDataCache.java   
/** {@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);
}
项目:distributed-task-scheduler    文件:RedisDataCache.java   
/** {@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);
}
项目:distributed-task-scheduler    文件:RedisDataCache.java   
/** {@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;
}
项目:distributed-task-scheduler    文件:RedisDataCache.java   
/** {@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;
}
项目:distributed-task-scheduler    文件:RedisDataCache.java   
/** {@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);
}
项目:dooo    文件:RedisConfig.java   
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() {
        }
    };
}
项目:distributed-task-scheduler    文件:RedisDataCache.java   
/** {@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;
}
项目:distributed-task-scheduler    文件:RedisDataCache.java   
/** {@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);
}
项目:distributed-task-scheduler    文件:RedisDataCache.java   
/** {@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;
}
项目:distributed-task-scheduler    文件:RedisDataCache.java   
/** {@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;
}
项目:distributed-task-scheduler    文件:RedisDataCache.java   
/** {@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);
}
项目:distributed-task-scheduler    文件:RedisDataCache.java   
/** {@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;
}
项目:distributed-task-scheduler    文件:RedisDataCache.java   
/** {@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;
}
项目:dubbox-hystrix    文件:RedisRegistry.java   
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;
}
项目:dubbox-hystrix    文件:RedisRegistry.java   
@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);
        }
    }
}