Search in sources :

Example 16 with JedisConnectionException

use of redis.clients.jedis.exceptions.JedisConnectionException in project cachecloud by sohutv.

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 17 with JedisConnectionException

use of redis.clients.jedis.exceptions.JedisConnectionException in project cachecloud by sohutv.

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 18 with JedisConnectionException

use of redis.clients.jedis.exceptions.JedisConnectionException in project dubbo by alibaba.

the class RedisProtocol method refer.

public <T> Invoker<T> refer(final Class<T> type, final URL url) throws RpcException {
    try {
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setTestOnBorrow(url.getParameter("test.on.borrow", true));
        config.setTestOnReturn(url.getParameter("test.on.return", false));
        config.setTestWhileIdle(url.getParameter("test.while.idle", false));
        if (url.getParameter("max.idle", 0) > 0)
            config.setMaxIdle(url.getParameter("max.idle", 0));
        if (url.getParameter("min.idle", 0) > 0)
            config.setMinIdle(url.getParameter("min.idle", 0));
        if (url.getParameter("max.active", 0) > 0)
            config.setMaxTotal(url.getParameter("max.active", 0));
        if (url.getParameter("max.total", 0) > 0)
            config.setMaxTotal(url.getParameter("max.total", 0));
        if (url.getParameter("max.wait", 0) > 0)
            config.setMaxWaitMillis(url.getParameter("max.wait", 0));
        if (url.getParameter("num.tests.per.eviction.run", 0) > 0)
            config.setNumTestsPerEvictionRun(url.getParameter("num.tests.per.eviction.run", 0));
        if (url.getParameter("time.between.eviction.runs.millis", 0) > 0)
            config.setTimeBetweenEvictionRunsMillis(url.getParameter("time.between.eviction.runs.millis", 0));
        if (url.getParameter("min.evictable.idle.time.millis", 0) > 0)
            config.setMinEvictableIdleTimeMillis(url.getParameter("min.evictable.idle.time.millis", 0));
        final JedisPool jedisPool = new JedisPool(config, url.getHost(), url.getPort(DEFAULT_PORT), url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT));
        final int expiry = url.getParameter("expiry", 0);
        final String get = url.getParameter("get", "get");
        final String set = url.getParameter("set", Map.class.equals(type) ? "put" : "set");
        final String delete = url.getParameter("delete", Map.class.equals(type) ? "remove" : "delete");
        return new AbstractInvoker<T>(type, url) {

            protected Result doInvoke(Invocation invocation) throws Throwable {
                Jedis resource = null;
                try {
                    resource = jedisPool.getResource();
                    if (get.equals(invocation.getMethodName())) {
                        if (invocation.getArguments().length != 1) {
                            throw new IllegalArgumentException("The redis get method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
                        }
                        byte[] value = resource.get(String.valueOf(invocation.getArguments()[0]).getBytes());
                        if (value == null) {
                            return new RpcResult();
                        }
                        ObjectInput oin = getSerialization(url).deserialize(url, new ByteArrayInputStream(value));
                        return new RpcResult(oin.readObject());
                    } else if (set.equals(invocation.getMethodName())) {
                        if (invocation.getArguments().length != 2) {
                            throw new IllegalArgumentException("The redis set method arguments mismatch, must be two arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
                        }
                        byte[] key = String.valueOf(invocation.getArguments()[0]).getBytes();
                        ByteArrayOutputStream output = new ByteArrayOutputStream();
                        ObjectOutput value = getSerialization(url).serialize(url, output);
                        value.writeObject(invocation.getArguments()[1]);
                        resource.set(key, output.toByteArray());
                        if (expiry > 1000) {
                            resource.expire(key, expiry / 1000);
                        }
                        return new RpcResult();
                    } else if (delete.equals(invocation.getMethodName())) {
                        if (invocation.getArguments().length != 1) {
                            throw new IllegalArgumentException("The redis delete method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
                        }
                        resource.del(String.valueOf(invocation.getArguments()[0]).getBytes());
                        return new RpcResult();
                    } else {
                        throw new UnsupportedOperationException("Unsupported method " + invocation.getMethodName() + " in redis service.");
                    }
                } catch (Throwable t) {
                    RpcException re = new RpcException("Failed to invoke redis service method. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url + ", cause: " + t.getMessage(), t);
                    if (t instanceof TimeoutException || t instanceof SocketTimeoutException) {
                        re.setCode(RpcException.TIMEOUT_EXCEPTION);
                    } else if (t instanceof JedisConnectionException || t instanceof IOException) {
                        re.setCode(RpcException.NETWORK_EXCEPTION);
                    } else if (t instanceof JedisDataException) {
                        re.setCode(RpcException.SERIALIZATION_EXCEPTION);
                    }
                    throw re;
                } finally {
                    if (resource != null) {
                        try {
                            jedisPool.returnResource(resource);
                        } catch (Throwable t) {
                            logger.warn("returnResource error: " + t.getMessage(), t);
                        }
                    }
                }
            }

            public void destroy() {
                super.destroy();
                try {
                    jedisPool.destroy();
                } catch (Throwable e) {
                    logger.warn(e.getMessage(), e);
                }
            }
        };
    } catch (Throwable t) {
        throw new RpcException("Failed to refer redis service. interface: " + type.getName() + ", url: " + url + ", cause: " + t.getMessage(), t);
    }
}
Also used : Invocation(com.alibaba.dubbo.rpc.Invocation) ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) RpcResult(com.alibaba.dubbo.rpc.RpcResult) AbstractInvoker(com.alibaba.dubbo.rpc.protocol.AbstractInvoker) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) Jedis(redis.clients.jedis.Jedis) SocketTimeoutException(java.net.SocketTimeoutException) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) RpcException(com.alibaba.dubbo.rpc.RpcException) JedisPool(redis.clients.jedis.JedisPool) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) TimeoutException(java.util.concurrent.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 19 with JedisConnectionException

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

the class MessageReceiver method start.

public void start() {
    log.info("Ready to receive messages from Redis pubsub.");
    try {
        receiveMessage = true;
        jedis = new Jedis(host, port);
        // Set the name of this client to be able to distinguish when doing
        // CLIENT LIST on redis-cli
        jedis.clientSetname("bbb-screenshare");
        Runnable messageReceiver = new Runnable() {

            public void run() {
                if (receiveMessage) {
                    try {
                        jedis.psubscribe(new PubSubListener(), MessagingConstants.FROM_BBB_APPS_PATTERN);
                    } catch (JedisConnectionException ex) {
                        log.warn("Exception on Jedis connection. Resubscribing to pubsub.");
                        start();
                    }
                }
            }
        };
        msgReceiverExec.execute(messageReceiver);
    } catch (Exception e) {
        log.error("Error subscribing to channels: " + e.getMessage());
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException)

Example 20 with JedisConnectionException

use of redis.clients.jedis.exceptions.JedisConnectionException in project jstorm by alibaba.

the class RedisSinkBolt method retrySet.

private String retrySet(byte[] key, byte[] value) {
    int retry = 0;
    String ret;
    while (true) {
        Jedis jedis = null;
        try {
            jedis = pool.getResource();
            ret = jedis.set(key, value);
            return ret;
        } catch (JedisConnectionException e) {
            if (jedis != null) {
                pool.returnBrokenResource(jedis);
                jedis = null;
            }
            if (retry > retryLimit) {
                throw e;
            }
            retry++;
        } finally {
            if (jedis != null) {
                pool.returnResource(jedis);
            }
        }
    }
}
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