Search in sources :

Example 41 with JedisPool

use of redis.clients.jedis.JedisPool in project dubbo by alibaba.

the class RedisRegistry method doSubscribe.

@Override
public void doSubscribe(final URL url, final NotifyListener listener) {
    String service = toServicePath(url);
    Notifier notifier = notifiers.get(service);
    if (notifier == null) {
        Notifier newNotifier = new Notifier(service);
        notifiers.putIfAbsent(service, newNotifier);
        notifier = notifiers.get(service);
        if (notifier == newNotifier) {
            notifier.start();
        }
    }
    boolean success = false;
    RpcException exception = null;
    for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
        JedisPool jedisPool = entry.getValue();
        try {
            Jedis jedis = jedisPool.getResource();
            try {
                if (service.endsWith(Constants.ANY_VALUE)) {
                    admin = true;
                    Set<String> keys = jedis.keys(service);
                    if (keys != null && !keys.isEmpty()) {
                        Map<String, Set<String>> serviceKeys = new HashMap<String, Set<String>>();
                        for (String key : keys) {
                            String serviceKey = toServicePath(key);
                            Set<String> sk = serviceKeys.get(serviceKey);
                            if (sk == null) {
                                sk = new HashSet<String>();
                                serviceKeys.put(serviceKey, sk);
                            }
                            sk.add(key);
                        }
                        for (Set<String> sk : serviceKeys.values()) {
                            doNotify(jedis, sk, url, Arrays.asList(listener));
                        }
                    }
                } else {
                    doNotify(jedis, jedis.keys(service + Constants.PATH_SEPARATOR + Constants.ANY_VALUE), url, Arrays.asList(listener));
                }
                success = true;
                // Just read one server's data
                break;
            } finally {
                jedis.close();
            }
        } catch (Throwable t) {
            // Try the next server
            exception = new RpcException("Failed to subscribe service from redis registry. registry: " + entry.getKey() + ", service: " + url + ", cause: " + t.getMessage(), t);
        }
    }
    if (exception != null) {
        if (success) {
            logger.warn(exception.getMessage(), exception);
        } else {
            throw exception;
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Jedis(redis.clients.jedis.Jedis) RpcException(com.alibaba.dubbo.rpc.RpcException) JedisPool(redis.clients.jedis.JedisPool) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 42 with JedisPool

use of redis.clients.jedis.JedisPool in project tech by ffyyhh995511.

the class JedisService method smembers.

/**
 * 返回set集合 values 数据
 * @param key
 * @return
 */
public Set<String> smembers(String key) {
    Set<String> rs = null;
    Jedis jedis = null;
    JedisPool jedisPool = null;
    try {
        jedisPool = getJedisPool();
        jedis = jedisPool.getResource();
        if (jedis != null) {
            rs = jedis.smembers(key);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
    return rs;
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisPool(redis.clients.jedis.JedisPool)

Example 43 with JedisPool

use of redis.clients.jedis.JedisPool in project tech by ffyyhh995511.

the class JedisService method hlen.

/**
 * 哈希长度
 * @param key
 * @return
 */
public Long hlen(String key) {
    Long rs = null;
    Jedis jedis = null;
    JedisPool jedisPool = null;
    try {
        jedisPool = getJedisPool();
        jedis = jedisPool.getResource();
        if (jedis != null) {
            rs = jedis.hlen(key);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
    return rs;
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisPool(redis.clients.jedis.JedisPool)

Example 44 with JedisPool

use of redis.clients.jedis.JedisPool in project tech by ffyyhh995511.

the class JedisService method dbSize.

/**
 * 返回当前选中数据库中键的数量
 * @return
 */
public long dbSize() {
    long rs = 0;
    Jedis jedis = null;
    JedisPool jedisPool = null;
    try {
        jedisPool = getJedisPool();
        jedis = jedisPool.getResource();
        if (jedis != null) {
            rs = jedis.dbSize();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
    return rs;
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisPool(redis.clients.jedis.JedisPool)

Example 45 with JedisPool

use of redis.clients.jedis.JedisPool in project tech by ffyyhh995511.

the class JedisService method zrange.

/**
 * 返回排序集合
 * @param key
 * @param start
 * @param end
 * @return
 */
public Set<String> zrange(String key, long start, long end) {
    Set<String> rs = null;
    Jedis jedis = null;
    JedisPool jedisPool = null;
    try {
        jedisPool = getJedisPool();
        jedis = jedisPool.getResource();
        if (jedis != null) {
            rs = jedis.zrange(key, start, end);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
    return rs;
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisPool(redis.clients.jedis.JedisPool)

Aggregations

JedisPool (redis.clients.jedis.JedisPool)108 Jedis (redis.clients.jedis.Jedis)75 Test (org.junit.Test)47 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)40 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)17 HostAndPort (redis.clients.jedis.HostAndPort)6 URI (java.net.URI)5 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)5 JedisCluster (redis.clients.jedis.JedisCluster)5 RpcException (com.alibaba.dubbo.rpc.RpcException)4 URISyntaxException (java.net.URISyntaxException)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 JedisException (redis.clients.jedis.exceptions.JedisException)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 InvalidURIException (redis.clients.jedis.exceptions.InvalidURIException)3 ArrayList (java.util.ArrayList)2