Java Code Examples for redis.clients.jedis.JedisSentinelPool#destroy()

The following examples show how to use redis.clients.jedis.JedisSentinelPool#destroy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TracingJedisSentinelPoolTest.java    From java-redis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testClosingTracedJedisClosesUnderlyingJedis() {
  JedisSentinelPool pool = new TracingJedisSentinelPool(
      new TracingConfiguration.Builder(mockTracer).build(), MASTER_NAME, sentinels,
      new GenericObjectPoolConfig());
  Jedis resource = pool.getResource();
  assertEquals(1, pool.getNumActive());

  resource.close();
  assertEquals(0, pool.getNumActive());
  assertEquals(1, pool.getNumIdle());

  // ensure that resource is reused
  Jedis nextResource = pool.getResource();
  assertEquals(1, pool.getNumActive());
  assertEquals(0, pool.getNumIdle());
  nextResource.close();

  pool.destroy();
}
 
Example 2
Source File: TracingJedisSentinelPoolTest.java    From java-redis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testClosingTracedJedisClosesUnderlyingJedis() {
  JedisSentinelPool pool = new TracingJedisSentinelPool(
      new TracingConfiguration.Builder(mockTracer).build(), MASTER_NAME, sentinels,
      new GenericObjectPoolConfig());
  Jedis resource = pool.getResource();
  assertEquals(1, pool.getNumActive());

  resource.close();
  assertEquals(0, pool.getNumActive());
  assertEquals(1, pool.getNumIdle());

  // ensure that resource is reused
  Jedis nextResource = pool.getResource();
  assertEquals(1, pool.getNumActive());
  assertEquals(0, pool.getNumIdle());
  nextResource.close();

  pool.destroy();
}
 
Example 3
Source File: RedisSentinelTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 4
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 5
Source File: TracingJedisSentinelPoolTest.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testSentinelPoolReturnsTracedJedis() {
  JedisSentinelPool pool = new TracingJedisSentinelPool(
      new TracingConfiguration.Builder(mockTracer).build(), MASTER_NAME, sentinels,
      new GenericObjectPoolConfig());
  Jedis jedis = pool.getResource();
  assertEquals("OK", jedis.set("key", "value"));
  assertEquals("value", jedis.get("key"));

  jedis.close();
  pool.destroy();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 6
Source File: TracingJedisSentinelPoolTest.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testSentinelPoolReturnsTracedJedis() {
  JedisSentinelPool pool = new TracingJedisSentinelPool(
      new TracingConfiguration.Builder(mockTracer).build(), MASTER_NAME, sentinels,
      new GenericObjectPoolConfig());
  Jedis jedis = pool.getResource();
  assertEquals("OK", jedis.set("key", "value"));
  assertEquals("value", jedis.get("key"));

  jedis.close();
  pool.destroy();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 7
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test(expected = JedisConnectionException.class)
public void initializeWithNotAvailableSentinelsShouldThrowException() {
  Set<String> wrongSentinels = new HashSet<String>();
  wrongSentinels.add(new HostAndPort("localhost", 65432).toString());
  wrongSentinels.add(new HostAndPort("localhost", 65431).toString());

  JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, wrongSentinels);
  pool.destroy();
}
 
Example 8
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@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();
  }
}
 
Example 9
Source File: RedisDeployCenterImpl.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 10
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 4 votes vote down vote up
@Test(expected = JedisException.class)
public void initializeWithNotMonitoredMasterNameShouldThrowException() {
  final String wrongMasterName = "wrongMasterName";
  JedisSentinelPool pool = new JedisSentinelPool(wrongMasterName, sentinels);
  pool.destroy();
}