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();
}
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;
}
}
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();
}
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;
}
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());
}
Aggregations