Search in sources :

Example 6 with JedisPool

use of redis.clients.jedis.JedisPool in project twitter-2-weibo by rjyo.

the class InitServlet method destroy.

@Override
public void destroy() {
    JedisPool pool = getPool(getServletContext());
    pool.destroy();
}
Also used : JedisPool(redis.clients.jedis.JedisPool)

Example 7 with JedisPool

use of redis.clients.jedis.JedisPool in project twitter-2-weibo by rjyo.

the class InitServlet method createJedisPool.

public JedisPool createJedisPool() {
    JedisPool jedisPool;
    GenericObjectPool.Config config = new GenericObjectPool.Config();
    config.testOnBorrow = true;
    config.testWhileIdle = true;
    config.maxActive = 25;
    config.maxIdle = 0;
    config.minIdle = 0;
    log.debug("Jedis pool created.");
    try {
        String services = System.getenv("VCAP_SERVICES");
        if (services != null) {
            JSONObject obj = new JSONObject(services);
            obj = obj.getJSONArray("redis-2.2").getJSONObject(0).getJSONObject("credentials");
            String hostname = obj.getString("hostname");
            int port = obj.getInt("port");
            String password = obj.getString("password");
            jedisPool = new JedisPool(config, hostname, port, 0, password);
        } else {
            jedisPool = new JedisPool(config, "localhost");
            log.info("Using localhost Redis server");
        }
        return jedisPool;
    } catch (JSONException e) {
        log.error("Failed to init", e);
        return null;
    }
}
Also used : JSONObject(weibo4j.org.json.JSONObject) ServletConfig(javax.servlet.ServletConfig) JSONException(weibo4j.org.json.JSONException) JedisPool(redis.clients.jedis.JedisPool) GenericObjectPool(org.apache.commons.pool.impl.GenericObjectPool)

Example 8 with JedisPool

use of redis.clients.jedis.JedisPool in project cachecloud by sohutv.

the class PoolBenchmark method withPool.

private static void withPool() throws Exception {
    final JedisPool pool = new JedisPool(new GenericObjectPoolConfig(), hnp.getHost(), hnp.getPort(), 2000, "foobared");
    List<Thread> tds = new ArrayList<Thread>();
    final AtomicInteger ind = new AtomicInteger();
    for (int i = 0; i < 50; i++) {
        Thread hj = new Thread(new Runnable() {

            public void run() {
                for (int i = 0; (i = ind.getAndIncrement()) < TOTAL_OPERATIONS; ) {
                    try {
                        Jedis j = pool.getResource();
                        final String key = "foo" + i;
                        j.set(key, key);
                        j.get(key);
                        j.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        tds.add(hj);
        hj.start();
    }
    for (Thread t : tds) t.join();
    pool.destroy();
}
Also used : Jedis(redis.clients.jedis.Jedis) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) JedisPool(redis.clients.jedis.JedisPool)

Example 9 with JedisPool

use of redis.clients.jedis.JedisPool in project cachecloud by sohutv.

the class RedisStandaloneBuilder method build.

public JedisPool build() {
    if (jedisPool == null) {
        while (true) {
            try {
                LOCK.tryLock(100, TimeUnit.MILLISECONDS);
                if (jedisPool == null) {
                    /**
                         * 心跳返回的请求为空;
                         */
                    String response = HttpUtils.doGet(String.format(ConstUtils.REDIS_STANDALONE_URL, appId));
                    if (response == null || response.isEmpty()) {
                        logger.warn("cannot get response from server, appId={}. continue...", appId);
                        continue;
                    }
                    /**
                         * 心跳返回的请求无效;
                         */
                    ObjectMapper mapper = new ObjectMapper();
                    JsonNode responseJson = null;
                    try {
                        responseJson = mapper.readTree(response);
                    } catch (Exception e) {
                        logger.error("read json from response error, appId: {}.", appId, e);
                    }
                    if (responseJson == null) {
                        logger.warn("invalid response, appId: {}. continue...", appId);
                        continue;
                    }
                    /**
                         * 从心跳中提取HostAndPort,构造JedisPool实例;
                         */
                    String instance = responseJson.get("standalone").asText();
                    String[] instanceArr = instance.split(":");
                    if (instanceArr.length != 2) {
                        logger.warn("instance info is invalid, instance: {}, appId: {}, continue...", instance, appId);
                        continue;
                    }
                    //收集上报数据
                    //                        ClientDataCollectReportExecutor.getInstance();
                    jedisPool = new JedisPool(poolConfig, instanceArr[0], Integer.valueOf(instanceArr[1]), timeout);
                    return jedisPool;
                }
            } catch (InterruptedException e) {
                logger.error("error in build().", e);
            }
        }
    }
    return jedisPool;
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) JedisPool(redis.clients.jedis.JedisPool) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 10 with JedisPool

use of redis.clients.jedis.JedisPool in project cachecloud by sohutv.

the class JedisPoolTest method checkJedisIsReusedWhenReturned.

@Test
public void checkJedisIsReusedWhenReturned() {
    JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort());
    Jedis jedis = pool.getResource();
    jedis.auth("foobared");
    jedis.set("foo", "0");
    jedis.close();
    jedis = pool.getResource();
    jedis.auth("foobared");
    jedis.incr("foo");
    jedis.close();
    pool.destroy();
    assertTrue(pool.isClosed());
}
Also used : Jedis(redis.clients.jedis.Jedis) JedisPool(redis.clients.jedis.JedisPool) JedisPoolConfig(redis.clients.jedis.JedisPoolConfig) Test(org.junit.Test)

Aggregations

JedisPool (redis.clients.jedis.JedisPool)72 Test (org.junit.Test)47 Jedis (redis.clients.jedis.Jedis)46 JedisPoolConfig (redis.clients.jedis.JedisPoolConfig)35 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)16 URI (java.net.URI)5 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)5 HostAndPort (redis.clients.jedis.HostAndPort)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