本文整理汇总了Java中redis.clients.jedis.JedisSentinelPool.getResource方法的典型用法代码示例。如果您正苦于以下问题:Java JedisSentinelPool.getResource方法的具体用法?Java JedisSentinelPool.getResource怎么用?Java JedisSentinelPool.getResource使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redis.clients.jedis.JedisSentinelPool
的用法示例。
在下文中一共展示了JedisSentinelPool.getResource方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkResourceIsCloseable
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
@Test
public void checkResourceIsCloseable() {
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();
try {
jedis.set("hello", "jedis");
} finally {
jedis.close();
}
Jedis jedis2 = pool.getResource();
try {
assertEquals(jedis, jedis2);
} finally {
jedis2.close();
}
}
示例2: customClientName
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
@Test
public void customClientName() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(1);
config.setBlockWhenExhausted(false);
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000,
"foobared", 0, "my_shiny_client_name");
Jedis jedis = pool.getResource();
try {
assertEquals("my_shiny_client_name", jedis.clientGetname());
} finally {
jedis.close();
pool.destroy();
}
assertTrue(pool.isClosed());
}
示例3: forceFailover
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
private void forceFailover(JedisSentinelPool pool) throws InterruptedException {
HostAndPort oldMaster = pool.getCurrentHostMaster();
// jedis connection should be master
Jedis beforeFailoverJedis = pool.getResource();
assertEquals("PONG", beforeFailoverJedis.ping());
waitForFailover(pool, oldMaster);
Jedis afterFailoverJedis = pool.getResource();
assertEquals("PONG", afterFailoverJedis.ping());
assertEquals("foobared", afterFailoverJedis.configGet("requirepass").get(1));
assertEquals(2, afterFailoverJedis.getDB().intValue());
// returning both connections to the pool should not throw
beforeFailoverJedis.close();
afterFailoverJedis.close();
}
示例4: testSentinel
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
@Test
public void testSentinel() {
JedisSentinelPool sentinelPool = ClientBuilder.redisSentinel(appId)
.setConnectionTimeout(2000)
.setSoTimeout(1000)
.build();
HostAndPort currentHostMaster = sentinelPool.getCurrentHostMaster();
logger.info("current master: {}", currentHostMaster.toString());
Jedis jedis = sentinelPool.getResource();
for (int i = 0; i < 10; i++) {
jedis.lpush("mylist", "list-" + i);
}
jedis.close();
sentinelPool.destroy();
}
示例5: forceFailover
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
private void forceFailover(JedisSentinelPool pool) throws InterruptedException {
HostAndPort oldMaster = pool.getCurrentHostMaster();
// jedis connection should be master
Jedis beforeFailoverJedis = pool.getResource();
assertEquals("PONG", beforeFailoverJedis.ping());
waitForFailover(pool, oldMaster);
Jedis afterFailoverJedis = pool.getResource();
assertEquals("PONG", afterFailoverJedis.ping());
assertEquals("foobared", afterFailoverJedis.configGet("requirepass").get(1));
assertEquals(2, afterFailoverJedis.getDB());
// returning both connections to the pool should not throw
beforeFailoverJedis.close();
afterFailoverJedis.close();
}
示例6: checkCloseableConnections
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
@Test
public void checkCloseableConnections() throws Exception {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000,
"foobared", 2);
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
pool.returnResource(jedis);
pool.close();
assertTrue(pool.isClosed());
}
示例7: returningBorrowedInstanceBeforeFailoverShouldNotAffectBorrowing
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
@Test
public void returningBorrowedInstanceBeforeFailoverShouldNotAffectBorrowing()
throws InterruptedException {
final JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels,
new GenericObjectPoolConfig(), 2000, "foobared", 2);
Jedis borrowed = pool.getResource();
forceFailover(pool);
Thread.sleep(1000);
// returns instance which was borrowed before failover
borrowed.close();
final AtomicBoolean isBorrowed = new AtomicBoolean(false);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
pool.getResource();
isBorrowed.set(true);
}
});
t.start();
// wait for 5 secs
t.join(5000);
assertTrue(isBorrowed.get());
}
示例8: returnResourceShouldResetState
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
@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");
pool.returnResource(jedis);
jedis2 = pool.getResource();
assertTrue(jedis == jedis2);
assertEquals("jedis", jedis2.get("hello"));
} catch (JedisConnectionException e) {
if (jedis2 != null) {
pool.returnBrokenResource(jedis2);
jedis2 = null;
}
} finally {
if (jedis2 != null) jedis2.close();
pool.destroy();
}
}
示例9: testSentinelExample
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
@Test
public void testSentinelExample() {
JedisSentinelPool sentinelPool = null;
// 使用默认配置
// sentinelPool = ClientBuilder.redisSentinel(appId).build();
/**
* 自定义配置
*/
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 3);
poolConfig.setMinIdle(GenericObjectPoolConfig.DEFAULT_MIN_IDLE * 2);
poolConfig.setJmxEnabled(true);
poolConfig.setMaxWaitMillis(3000);
sentinelPool = ClientBuilder.redisSentinel(appId)
.setPoolConfig(poolConfig)
.setConnectionTimeout(2000)
.setSoTimeout(1000)
.build();
Jedis jedis = sentinelPool.getResource();
jedis.set("key1", "1");
assertEquals("2", jedis.incr("key1"));
jedis.close();
}
示例10: checkCloseableConnections
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
@Test
public void checkCloseableConnections() throws Exception {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000,
"foobared", 2);
Jedis jedis = pool.getResource();
jedis.auth("foobared");
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
jedis.close();
pool.close();
assertTrue(pool.isClosed());
}
示例11: returnResourceShouldResetState
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
@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();
}
}
示例12: addSentinel
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
@Override
public boolean addSentinel(long appId, String sentinelHost) {
AppDesc appDesc = appDao.getAppDescById(appId);
JedisSentinelPool jedisSentinelPool = redisCenter.getJedisSentinelPool(appDesc);
if (jedisSentinelPool == null) {
return false;
}
List<InstanceInfo> instanceInfos = instanceDao.getInstListByAppId(appId);
String masterName = null;
for (Iterator<InstanceInfo> i = instanceInfos.iterator(); i.hasNext(); ) {
InstanceInfo instanceInfo = i.next();
if (instanceInfo.getType() != ConstUtils.CACHE_REDIS_SENTINEL) {
i.remove();
continue;
}
if (masterName == null && StringUtils.isNotBlank(instanceInfo.getCmd())) {
masterName = instanceInfo.getCmd();
}
}
Jedis jedis = null;
String masterHost = null;
Integer masterPort = null;
try {
jedis = jedisSentinelPool.getResource();
masterHost = jedis.getClient().getHost();
masterPort = jedis.getClient().getPort();
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
jedis.close();
jedisSentinelPool.destroy();
}
boolean isRun = runSentinel(appDesc, sentinelHost, masterName, masterHost, masterPort);
if (!isRun) {
return false;
}
return true;
}
示例13: test
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
@Test
public void test() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(1);
config.setBlockWhenExhausted(false);
Set<String> sentinels = new HashSet<String>();
sentinels.add(new HostAndPort("192.168.0.36", 26379).toString());
JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels,
config, 10000000);
Jedis jedis = pool.getResource();
Jedis jedis2 = null;
try {
jedis.set("hello", "jedis");
Transaction t = jedis.multi();
t.set("hello", "world");
pool.returnResource(jedis);
jedis2 = pool.getResource();
assertTrue(jedis == jedis2);
assertEquals("jedis", jedis2.get("hello"));
} catch (JedisConnectionException e) {
if (jedis2 != null) {
pool.returnBrokenResource(jedis2);
jedis2 = null;
}
} finally {
if (jedis2 != null)
pool.returnResource(jedis2);
pool.destroy();
}
}
示例14: main
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
public static void main(String[] args) {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(1);
config.setBlockWhenExhausted(false);
Set<String> sentinels = new HashSet<String>();
sentinels.add(new HostAndPort("192.168.0.36", 26379).toString());
JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels,
config, 10000000);
Jedis jedis = pool.getResource();
jedis.set("hello", "world");
System.out.println(jedis.get("hello"));
pool.destroy();
}
示例15: setUpRedis
import redis.clients.jedis.JedisSentinelPool; //导入方法依赖的package包/类
@Before
public void setUpRedis() throws IOException, SchedulerConfigException {
final List<Integer> sentinels = Arrays.asList(getPort(), getPort());
final List<Integer> group1 = Arrays.asList(getPort(), getPort());
final List<Integer> group2 = Arrays.asList(getPort(), getPort());
//creates a cluster with 3 sentinels, quorum size of 2 and 3 replication groups, each with one master and one slave
redisCluster = RedisCluster.builder().sentinelPorts(sentinels).quorumSize(2)
.serverPorts(group1).replicationGroup("master1", 1)
.serverPorts(group2).replicationGroup("master2", 1)
.ephemeralServers().replicationGroup("master3", 1)
.build();
redisCluster.start();
Set<String> jedisSentinelHosts = JedisUtil.sentinelHosts(redisCluster);
joinedHosts = Joiner.on(",").join(jedisSentinelHosts);
final short database = 1;
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setTestOnBorrow(true);
jedisPoolConfig.setTestOnCreate(true);
jedisPoolConfig.setTestOnReturn(true);
jedisPoolConfig.setMaxWaitMillis(2000);
jedisPoolConfig.setMaxTotal(20);
jedisPool = new JedisSentinelPool("master1", jedisSentinelHosts, jedisPoolConfig);
jobStore = new RedisJobStore();
jobStore.setHost(joinedHosts);
jobStore.setJedisPool(jedisSentinelPool);
jobStore.setLockTimeout(2000);
jobStore.setMasterGroupName("master1");
jobStore.setRedisSentinel(true);
jobStore.setInstanceId("testJobStore1");
jobStore.setDatabase(database);
mockScheduleSignaler = mock(SchedulerSignaler.class);
jobStore.initialize(null, mockScheduleSignaler);
schema = new RedisJobStoreSchema();
jedis = jedisPool.getResource();
jedis.flushDB();
}