use of redis.clients.jedis.HostAndPort in project jedis by xetorthio.
the class ExceptionsTest method redirection.
@Test
public void redirection() {
HostAndPort hap = new HostAndPort("", 0);
int slot = -1;
try {
throw new JedisRedirectionException(MESSAGE, hap, slot);
} catch (Exception e) {
assertSame(JedisRedirectionException.class, e.getClass());
assertEquals(MESSAGE, e.getMessage());
assertNull(e.getCause());
}
try {
throw new JedisRedirectionException(CAUSE, hap, slot);
} catch (Exception e) {
assertSame(JedisRedirectionException.class, e.getClass());
assertEquals(CAUSE, e.getCause());
assertEquals(CAUSE.toString(), e.getMessage());
}
try {
throw new JedisRedirectionException(MESSAGE, CAUSE, hap, slot);
} catch (Exception e) {
assertSame(JedisRedirectionException.class, e.getClass());
assertEquals(MESSAGE, e.getMessage());
assertEquals(CAUSE, e.getCause());
}
}
use of redis.clients.jedis.HostAndPort in project jedis by xetorthio.
the class ClusterConnectionProvider method initializeSlotsCache.
private void initializeSlotsCache(Set<HostAndPort> startNodes, JedisClientConfig clientConfig) {
ArrayList<HostAndPort> startNodeList = new ArrayList<>(startNodes);
Collections.shuffle(startNodeList);
for (HostAndPort hostAndPort : startNodeList) {
try (Connection jedis = new Connection(hostAndPort, clientConfig)) {
cache.discoverClusterNodesAndSlots(jedis);
return;
} catch (JedisConnectionException e) {
// try next nodes
}
}
throw new JedisClusterOperationException("Could not initialize cluster slots cache.");
}
use of redis.clients.jedis.HostAndPort in project jedis by xetorthio.
the class JedisSentinelTestUtil method waitForNewPromotedMaster.
public static HostAndPort waitForNewPromotedMaster(final String masterName, final Jedis sentinelJedis, final Jedis commandJedis) throws InterruptedException {
final AtomicReference<String> newmaster = new AtomicReference<String>("");
sentinelJedis.psubscribe(new JedisPubSub() {
@Override
public void onPMessage(String pattern, String channel, String message) {
if (channel.equals("+switch-master")) {
newmaster.set(message);
punsubscribe();
} else if (channel.startsWith("-failover-abort")) {
punsubscribe();
throw new FailoverAbortedException("Unfortunately sentinel cannot failover..." + " reason(channel) : " + channel + " / message : " + message);
}
}
@Override
public void onPSubscribe(String pattern, int subscribedChannels) {
commandJedis.sentinelFailover(masterName);
}
}, "*");
String[] chunks = newmaster.get().split(" ");
HostAndPort newMaster = new HostAndPort(chunks[3], Integer.parseInt(chunks[4]));
return newMaster;
}
use of redis.clients.jedis.HostAndPort in project jedis by xetorthio.
the class FailoverCommandsTest method failoverForce.
@Test
public void failoverForce() throws InterruptedException {
assumeFalse(failoverStuck);
try (Jedis master = new Jedis(masterAddress)) {
switching = true;
assertEquals("OK", master.failover(FailoverParams.failoverParams().to(new HostAndPort("127.0.0.1", replicaAddress.getPort())).force().timeout(100)));
}
}
use of redis.clients.jedis.HostAndPort in project e3mall by colg-cloud.
the class JedisTest method testJedisCluster.
/**
* redis 集群
*/
@Test
public void testJedisCluster() {
// 创建一个JedisCluster对象,有一个参数nodes是一个set类型,set中包含若干个HostAndPort对象
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.21.103", 7000));
nodes.add(new HostAndPort("192.168.21.103", 7001));
nodes.add(new HostAndPort("192.168.21.103", 7002));
nodes.add(new HostAndPort("192.168.21.103", 7003));
nodes.add(new HostAndPort("192.168.21.103", 7004));
nodes.add(new HostAndPort("192.168.21.103", 7005));
JedisCluster jedisCluster = new JedisCluster(nodes);
// 直接使用JedisCluster对象操作redis,操作完不用关闭,自带连接池
jedisCluster.set("test", "123");
System.out.println(jedisCluster.get("test"));
// 系统关闭前,关闭JedisCluster对象
try {
jedisCluster.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Aggregations