Search in sources :

Example 31 with JedisConnectionException

use of redis.clients.jedis.exceptions.JedisConnectionException in project jedis by xetorthio.

the class SSLJedisTest method connectWithShardInfoAndEmptyTrustStore.

/**
   * Tests opening an SSL/TLS connection to redis with an empty certificate
   * trust store. This test should fail because there is no trust anchor for the
   * redis server certificate.
   * 
   * @throws Exception
   */
@Test
public void connectWithShardInfoAndEmptyTrustStore() throws Exception {
    final URI uri = URI.create("rediss://localhost:6390");
    final SSLSocketFactory sslSocketFactory = createTrustNoOneSslSocketFactory();
    JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, null, null);
    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("Unexpected first inner exception.", SSLException.class, e.getCause().getClass());
        Assert.assertEquals("Unexpected second inner exception.", RuntimeException.class, e.getCause().getCause().getClass());
        Assert.assertEquals("Unexpected third inner exception.", InvalidAlgorithmParameterException.class, e.getCause().getCause().getCause().getClass());
    }
    try {
        jedis.close();
    } catch (Throwable e1) {
    // Expected.
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) JedisShardInfo(redis.clients.jedis.JedisShardInfo) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URI(java.net.URI) SSLException(javax.net.ssl.SSLException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) Test(org.junit.Test)

Example 32 with JedisConnectionException

use of redis.clients.jedis.exceptions.JedisConnectionException in project jedis by xetorthio.

the class JedisSentinelPoolTest method returnResourceShouldResetState.

@Test
public void returnResourceShouldResetState() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2);
    Jedis jedis = pool.getResource();
    Jedis jedis2 = null;
    try {
        jedis.set("hello", "jedis");
        Transaction t = jedis.multi();
        t.set("hello", "world");
        jedis.close();
        jedis2 = pool.getResource();
        assertTrue(jedis == jedis2);
        assertEquals("jedis", jedis2.get("hello"));
    } catch (JedisConnectionException e) {
        if (jedis2 != null) {
            jedis2 = null;
        }
    } finally {
        jedis2.close();
        pool.destroy();
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) Test(org.junit.Test)

Example 33 with JedisConnectionException

use of redis.clients.jedis.exceptions.JedisConnectionException in project jedis by xetorthio.

the class Protocol method processBulkReply.

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;
}
Also used : JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException)

Example 34 with JedisConnectionException

use of redis.clients.jedis.exceptions.JedisConnectionException in project jedis by xetorthio.

the class Protocol method sendCommand.

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);
    }
}
Also used : IOException(java.io.IOException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException)

Example 35 with JedisConnectionException

use of redis.clients.jedis.exceptions.JedisConnectionException in project leopard by tanhaichao.

the class JedisPool method getResource.

@Override
public Jedis getResource() {
    long startTime = System.nanoTime();
    Jedis resource = null;
    try {
        resource = super.getResource();
    } catch (JedisConnectionException e) {
        if (redisConnectionListener != null) {
            redisConnectionListener.broken();
        }
        throw e;
    } finally {
        if (redisConnectionListener != null) {
            redisConnectionListener.open(resource, startTime);
        }
    }
    return resource;
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException)

Aggregations

JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)35 Jedis (redis.clients.jedis.Jedis)15 IOException (java.io.IOException)8 Logger (org.apache.log4j.Logger)8 Test (org.junit.Test)7 JedisDataException (redis.clients.jedis.exceptions.JedisDataException)5 URI (java.net.URI)3 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)3 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)3 JedisShardInfo (redis.clients.jedis.JedisShardInfo)3 UsefulDataModel (com.sohu.tv.jedis.stat.model.UsefulDataModel)2 InetSocketAddress (java.net.InetSocketAddress)2 Socket (java.net.Socket)2 SSLParameters (javax.net.ssl.SSLParameters)2 BinaryJedis (redis.clients.jedis.BinaryJedis)2 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)2 Transaction (redis.clients.jedis.Transaction)2 RedisInputStream (redis.clients.util.RedisInputStream)2 RedisOutputStream (redis.clients.util.RedisOutputStream)2 ServerInfo (beans.dbaccess.ServerInfo)1