当前位置: 首页>>代码示例>>Java>>正文


Java JedisSentinelPool类代码示例

本文整理汇总了Java中redis.clients.jedis.JedisSentinelPool的典型用法代码示例。如果您正苦于以下问题:Java JedisSentinelPool类的具体用法?Java JedisSentinelPool怎么用?Java JedisSentinelPool使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


JedisSentinelPool类属于redis.clients.jedis包,在下文中一共展示了JedisSentinelPool类的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();
  }
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:23,代码来源:JedisSentinelPoolTest.java

示例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());
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:20,代码来源:JedisSentinelPoolTest.java

示例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();
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:19,代码来源:JedisSentinelPoolTest.java

示例4: initializeDatabaseConnection

import redis.clients.jedis.JedisSentinelPool; //导入依赖的package包/类
private void initializeDatabaseConnection() throws LifecycleException {
  try {
    if (getSentinelMaster() != null) {
      Set<String> sentinelSet = getSentinelSet();
      if (sentinelSet != null && sentinelSet.size() > 0) {
        connectionPool = new JedisSentinelPool(getSentinelMaster(), sentinelSet, this.connectionPoolConfig, getTimeout(), getPassword());
      } else {
        throw new LifecycleException("Error configuring Redis Sentinel connection pool: expected both `sentinelMaster` and `sentiels` to be configured");
      }
    } else {
      connectionPool = new JedisPool(this.connectionPoolConfig, getHost(), getPort(), getTimeout(), getPassword());
    }
  } catch (Exception e) {
    e.printStackTrace();
    throw new LifecycleException("Error connecting to Redis", e);
  }
}
 
开发者ID:xiaowing,项目名称:various_demos,代码行数:18,代码来源:RedisSessionManager.java

示例5: 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();
}
 
开发者ID:sohutv,项目名称:cachecloud,代码行数:17,代码来源:RedisSentinelTest.java

示例6: 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();
}
 
开发者ID:sohutv,项目名称:cachecloud,代码行数:19,代码来源:JedisSentinelPoolTest.java

示例7: ManageableJedisConnection

import redis.clients.jedis.JedisSentinelPool; //导入依赖的package包/类
/**
 * Constructor.
 *
 * @param redisConfiguration configuration
 */
public ManageableJedisConnection(final RedisConfiguration redisConfiguration) {
    this.nRetry = redisConfiguration.getRetry();
    this.waitBeforeRetryMs = redisConfiguration.getWaitBeforeRetryMs();

    switch (redisConfiguration.getType()) {
        case REDIS:
            LOGGER.info("Creates Simple Redis connection pool");
            this.pool = new JedisPool(new JedisPoolConfig(), redisConfiguration.getHost(),
                    redisConfiguration.getPort(), redisConfiguration.getTimeout());
            break;
        case SENTINEL:
            LOGGER.info("Creates Redis Sentinel connection pool");
            this.pool = new JedisSentinelPool(redisConfiguration.getMasterName(), redisConfiguration.getSentinels(),
                    new JedisPoolConfig(), redisConfiguration.getTimeout());
            break;
        default: {
            throw new IllegalArgumentException("Unexpected jedis pool type provided in configuration file");
        }
    }

}
 
开发者ID:voyages-sncf-technologies,项目名称:hesperides,代码行数:27,代码来源:ManageableJedisConnection.java

示例8: RedisTokenRepository

import redis.clients.jedis.JedisSentinelPool; //导入依赖的package包/类
/**
 * Creates a new RedisTokenRepository using Redis sentinels.
 * @param config The Redis configuration to use
 */
public RedisTokenRepository(final RedisTokenRepositorySentinelConfig config) {
    final Set<String> sentinels = new HashSet<>();

    for (final RedisTokenRepositorySentinelConfig.HostAndPort hostAndPort : config.getHostsAndPorts()) {
        sentinels.add(hostAndPort.getHost() + ':' + hostAndPort.getPort());
    }

    this.jedisPool = new JedisSentinelPool(
        config.getMasterName(),
        sentinels,
        new JedisPoolConfig(),
        config.getTimeout(),
        config.getPassword(),
        config.getDatabase()
    );
}
 
开发者ID:hpe-idol,项目名称:redis-hod-token-repository,代码行数:21,代码来源:RedisTokenRepository.java

示例9: initializeDatabaseConnection

import redis.clients.jedis.JedisSentinelPool; //导入依赖的package包/类
/**
 * 初始化DB连接
 */
protected void initializeDatabaseConnection() throws LifecycleException {
	try {
		if (getSentinelMaster() != null) {
			Set<String> sentinelSet = getSentinelSet();
			if (sentinelSet != null && sentinelSet.size() > 0) {
				connectionPool = new JedisSentinelPool(getSentinelMaster(), sentinelSet, this.connectionPoolConfig, getTimeout(), getPassword());
			} else {
				throw new LifecycleException(
						"Error configuring Redis Sentinel connection pool: expected both `sentinelMaster` and `sentiels` to be configured");
			}
		} else {
			connectionPool = new JedisPool(this.connectionPoolConfig, getHost(), getPort(), getTimeout(), getPassword());
		}
	} catch (Exception e) {
		e.printStackTrace();
		throw new LifecycleException("Error connecting to Redis", e);
	}
}
 
开发者ID:liuxinglanyue,项目名称:distributed-session-manager,代码行数:22,代码来源:RedisSessionManager.java

示例10: getPool

import redis.clients.jedis.JedisSentinelPool; //导入依赖的package包/类
@Override
protected Pool<Jedis> getPool(JedisPoolConfig jedisPoolConfig, String serverAddress) {
    String[] hosts = serverAddress.split(";");
    String host0 = hosts[0];
    String[] masterNameAndHost0 = host0.split("@");
    String masterName;
    if (masterNameAndHost0.length < 2) {
        masterName = DEFAULT_MASTER_NAME;
    } else {
        masterName = masterNameAndHost0[0];
        hosts[0] = masterNameAndHost0[1];
    }

    Set<String> sentinels = new HashSet<String>(Arrays.asList(hosts));
    JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, jedisPoolConfig, timeout);
    return jedisSentinelPool;
}
 
开发者ID:pister,项目名称:wint,代码行数:18,代码来源:SentinelRedisClient.java

示例11: initializeWithNotAvailableSentinelsShouldThrowException

import redis.clients.jedis.JedisSentinelPool; //导入依赖的package包/类
@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();
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:10,代码来源:JedisSentinelPoolTest.java

示例12: 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());
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:15,代码来源:JedisSentinelPoolTest.java

示例13: ensureSafeTwiceFailover

import redis.clients.jedis.JedisSentinelPool; //导入依赖的package包/类
@Test
public void ensureSafeTwiceFailover() throws InterruptedException {
  JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels,
      new GenericObjectPoolConfig(), 2000, "foobared", 2);

  forceFailover(pool);
  // after failover sentinel needs a bit of time to stabilize before a new
  // failover
  Thread.sleep(100);
  forceFailover(pool);

  // you can test failover as much as possible
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:14,代码来源:JedisSentinelPoolTest.java

示例14: 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());
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:31,代码来源:JedisSentinelPoolTest.java

示例15: 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();
  }
}
 
开发者ID:qq1588518,项目名称:JRediClients,代码行数:33,代码来源:JedisSentinelPoolTest.java


注:本文中的redis.clients.jedis.JedisSentinelPool类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。