本文整理匯總了Java中redis.clients.jedis.JedisPool.getResource方法的典型用法代碼示例。如果您正苦於以下問題:Java JedisPool.getResource方法的具體用法?Java JedisPool.getResource怎麽用?Java JedisPool.getResource使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類redis.clients.jedis.JedisPool
的用法示例。
在下文中一共展示了JedisPool.getResource方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: isAvailable
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
public boolean isAvailable() {
for (JedisPool jedisPool : jedisPools.values()) {
Jedis jedis = jedisPool.getResource();
boolean isBroken = false;
try {
if (jedis.isConnected()) {
return true; // 至少需單台機器可用
}
} catch (JedisConnectionException e) {
isBroken = true;
} finally {
if (isBroken) {
jedisPool.returnBrokenResource(jedis);
} else {
jedisPool.returnResource(jedis);
}
}
}
return false;
}
示例2: available
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
@Override
public boolean available() {
for (JedisPool jedisPool : jedisPools.values()) {
Jedis jedis = jedisPool.getResource();
try {
if (jedis.isConnected()) {
return true; // 至少需單台機器可用
}
} catch (JedisConnectionException e) {
logger.error("Jedis Connection Exception", e);
} finally {
if(jedis != null){
jedis.close();
}
}
}
return false;
}
示例3: testCloseConnectionOnMakeObject
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
@Test
public void testCloseConnectionOnMakeObject() {
JedisPoolConfig config = new JedisPoolConfig();
config.setTestOnBorrow(true);
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000,
"wrong pass");
Jedis jedis = new Jedis("redis://:[email protected]:6379/");
int currentClientCount = getClientCount(jedis.clientList());
try {
pool.getResource();
fail("Should throw exception as password is incorrect.");
} catch (Exception e) {
assertEquals(currentClientCount, getClientCount(jedis.clientList()));
}
}
示例4: selectDatabaseOnActivation
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
@Test
public void selectDatabaseOnActivation() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000,
"foobared");
Jedis jedis0 = pool.getResource();
assertEquals(0L, jedis0.getDB().longValue());
jedis0.select(1);
assertEquals(1L, jedis0.getDB().longValue());
pool.returnResource(jedis0);
Jedis jedis1 = pool.getResource();
assertTrue("Jedis instance was not reused", jedis1 == jedis0);
assertEquals(0L, jedis1.getDB().longValue());
pool.returnResource(jedis1);
pool.destroy();
assertTrue(pool.isClosed());
}
示例5: checkResourceIsCloseable
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
@Test
public void checkResourceIsCloseable() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(1);
config.setBlockWhenExhausted(false);
JedisPool pool = new JedisPool(config, hnp.getHost(), hnp.getPort(), 2000, "foobared");
Jedis jedis = pool.getResource();
try {
jedis.set("hello", "jedis");
} finally {
jedis.close();
}
Jedis jedis2 = pool.getResource();
try {
assertEquals(jedis, jedis2);
} finally {
jedis2.close();
}
}
示例6: main
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
public static void main(String[] args) throws InterruptedException {
Bench<JedisPool> bench = new JedisBench() {
@Override
public void executeOperation(String data, JedisPool benchInstance, int threadNumber, int iteration,
MetricRegistry metrics) {
Jedis jedis = benchInstance.getResource();
Timer.Context time = metrics.timer("incr").time();
jedis.incr("incr_" + threadNumber + "_" + iteration);
time.stop();
jedis.close();
}
};
Benchmark benchmark = new Benchmark(bench);
benchmark.run(args);
}
示例7: main
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
public static void main(String[] args) throws InterruptedException {
Bench<JedisPool> bench = new JedisBench() {
@Override
public void executeOperation(String data, JedisPool benchInstance, int threadNumber, int iteration,
MetricRegistry metrics) {
Jedis jedis = benchInstance.getResource();
Timer.Context time = metrics.timer("set").time();
String key = "set_" + threadNumber;
jedis.sadd(key, data);
time.stop();
jedis.close();
}
};
Benchmark benchmark = new Benchmark(bench);
benchmark.run(args);
}
示例8: main
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
public static void main(String[] args) throws InterruptedException {
Bench<JedisPool> bench = new JedisBench() {
@Override
public void executeOperation(String data, JedisPool benchInstance, int threadNumber, int iteration,
MetricRegistry metrics) {
Jedis jedis = benchInstance.getResource();
Timer.Context time = metrics.timer("bucket").time();
String key = "bucket_" + threadNumber + "_" + iteration;
jedis.set(key, data);
time.stop();
jedis.close();
}
};
Benchmark benchmark = new Benchmark(bench);
benchmark.run(args);
}
示例9: checkConnectionWithDefaultPort
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
@Test
public void checkConnectionWithDefaultPort() {
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort());
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
pool.returnResource(jedis);
pool.destroy();
assertTrue(pool.isClosed());
}
示例10: getTweets
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
@Override
public Optional<Tweet> getTweets(User user, String id) {
JedisPool jedisPool = JedisFactory.getPool();
try (Jedis jedis = jedisPool.getResource()) {
String key = user.getName() + ":tweet:" + id;
Tweet tweet = new Tweet(null, null, user, id);
tweet.setMessage(jedis.hget(key, "message"));
tweet.setDate(jedis.hget(key, "date"));
return Optional.of(tweet);
} catch (Exception e) {
System.out.println("Mock getTweets");
e.printStackTrace();
return null;
}
}
示例11: doUnregister
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
@Override
public void doUnregister(NURL nurl) {
String key = toCategoryPath(nurl);
String value = nurl.toFullString();
MregException exception = null;
boolean success = false;
for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
JedisPool jedisPool = entry.getValue();
try {
Jedis jedis = jedisPool.getResource();
try {
jedis.hdel(key, value);
jedis.publish(key, Consts.UNREGISTER);
success = true;
if (! replicate) {
break; // 如果服務器端已同步數據,隻需寫入單台機器
}
} catch (JedisConnectionException e){
logger.error("Jedis Connection Exception", e);
} finally {
if(jedis != null){
jedis.close();
}
}
} catch (Throwable t) {
exception = new MregException("Failed to unregister service to redis registry. registry: " + entry.getKey() + ", service: " + nurl + ", cause: " + t.getMessage(), t);
}
}
if (exception != null) {
if (success) {
logger.warn(exception.getMessage(), exception);
} else {
throw exception;
}
}
}
示例12: startWithUrlString
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
@Test
public void startWithUrlString() {
Jedis j = new Jedis("localhost", 6380);
j.auth("foobared");
j.select(2);
j.set("foo", "bar");
JedisPool pool = new JedisPool("redis://:[email protected]:6380/2");
Jedis jedis = pool.getResource();
assertEquals("PONG", jedis.ping());
assertEquals("bar", jedis.get("foo"));
}
示例13: getFollowersOfUsers
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
@Override
public Optional<Set<String>> getFollowersOfUsers(User user) {
JedisPool jedisPool = JedisFactory.getPool();
try (Jedis jedis = jedisPool.getResource()) {
String key = user.getName() + ":follower";
return Optional.of(jedis.smembers(key));
} catch (Exception e) {
System.out.println("Mock getFollowersOfUsers");
e.printStackTrace();
return null;
}
// return Optional.ofNullable("getFollowersOfUsers: Redis-Answer-Expression");
}
示例14: withPool
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
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);
pool.returnResource(j);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
tds.add(hj);
hj.start();
}
for (Thread t : tds)
t.join();
pool.destroy();
}
示例15: deferExpired
import redis.clients.jedis.JedisPool; //導入方法依賴的package包/類
private void deferExpired() {
for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
JedisPool jedisPool = entry.getValue();
boolean isBroken = false;
try {
Jedis jedis = jedisPool.getResource();
try {
for (URL url : new HashSet<URL>(getRegistered())) {
if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
String key = toCategoryPath(url);
if (jedis.hset(key, url.toFullString(), String.valueOf(System.currentTimeMillis() + expirePeriod)) == 1) {
jedis.publish(key, Constants.REGISTER);
}
}
}
if (admin) {
clean(jedis);
}
if (!replicate) {
break;// 如果服務器端已同步數據,隻需寫入單台機器
}
} catch (JedisConnectionException e){
isBroken = true;
} finally {
if(isBroken){
jedisPool.returnBrokenResource(jedis);
} else {
jedisPool.returnResource(jedis);
}
}
} catch (Throwable t) {
logger.warn("Failed to write provider heartbeat to redis registry. registry: " + entry.getKey() + ", cause: " + t.getMessage(), t);
}
}
}