use of org.redisson.client.RedisClient in project redisson by redisson.
the class MasterSlaveEntry method setupMasterEntry.
public RFuture<Void> setupMasterEntry(String host, int port) {
RedisClient client = connectionManager.createClient(NodeType.MASTER, host, port);
masterEntry = new ClientConnectionsEntry(client, config.getMasterConnectionMinimumIdleSize(), config.getMasterConnectionPoolSize(), config.getSubscriptionConnectionMinimumIdleSize(), config.getSubscriptionConnectionPoolSize(), connectionManager, NodeType.MASTER);
if (config.getSubscriptionMode() == SubscriptionMode.MASTER) {
RFuture<Void> f = writeConnectionHolder.add(masterEntry);
RFuture<Void> s = pubSubConnectionHolder.add(masterEntry);
return CountListener.create(s, f);
}
return writeConnectionHolder.add(masterEntry);
}
use of org.redisson.client.RedisClient in project redisson by redisson.
the class MasterSlaveEntry method addSlave.
private RFuture<Void> addSlave(String host, int port, boolean freezed, NodeType mode) {
RedisClient client = connectionManager.createClient(NodeType.SLAVE, host, port);
ClientConnectionsEntry entry = new ClientConnectionsEntry(client, this.config.getSlaveConnectionMinimumIdleSize(), this.config.getSlaveConnectionPoolSize(), this.config.getSubscriptionConnectionMinimumIdleSize(), this.config.getSubscriptionConnectionPoolSize(), connectionManager, mode);
if (freezed) {
synchronized (entry) {
entry.setFreezed(freezed);
entry.setFreezeReason(FreezeReason.SYSTEM);
}
}
return slaveBalancer.add(entry);
}
use of org.redisson.client.RedisClient in project redisson by redisson.
the class SentinelConnectionManager method registerSentinel.
private RFuture<RedisPubSubConnection> registerSentinel(final SentinelServersConfig cfg, final URL addr, final MasterSlaveServersConfig c) {
RedisClient client = createClient(addr.getHost(), addr.getPort(), c.getConnectTimeout(), c.getRetryInterval() * c.getRetryAttempts());
RedisClient oldClient = sentinels.putIfAbsent(addr.getHost() + ":" + addr.getPort(), client);
if (oldClient != null) {
return newSucceededFuture(null);
}
RFuture<RedisPubSubConnection> pubsubFuture = client.connectPubSubAsync();
pubsubFuture.addListener(new FutureListener<RedisPubSubConnection>() {
@Override
public void operationComplete(Future<RedisPubSubConnection> future) throws Exception {
if (!future.isSuccess()) {
log.warn("Can't connect to sentinel: {}:{}", addr.getHost(), addr.getPort());
return;
}
RedisPubSubConnection pubsub = future.getNow();
pubsub.addListener(new BaseRedisPubSubListener() {
@Override
public void onMessage(String channel, Object msg) {
if ("+sentinel".equals(channel)) {
onSentinelAdded(cfg, (String) msg, c);
}
if ("+slave".equals(channel)) {
onSlaveAdded(addr, (String) msg);
}
if ("+sdown".equals(channel)) {
onNodeDown(addr, (String) msg);
}
if ("-sdown".equals(channel)) {
onNodeUp(addr, (String) msg);
}
if ("+switch-master".equals(channel)) {
onMasterChange(cfg, addr, (String) msg);
}
}
@Override
public boolean onStatus(PubSubType type, String channel) {
if (type == PubSubType.SUBSCRIBE) {
log.debug("subscribed to channel: {} from Sentinel {}:{}", channel, addr.getHost(), addr.getPort());
}
return true;
}
});
pubsub.subscribe(StringCodec.INSTANCE, "+switch-master", "+sdown", "-sdown", "+slave", "+sentinel");
log.info("sentinel: {}:{} added", addr.getHost(), addr.getPort());
}
});
return pubsubFuture;
}
use of org.redisson.client.RedisClient in project redisson by redisson.
the class RedisClientTest method testConnectAsync.
@Test
public void testConnectAsync() throws InterruptedException {
RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerBindAddressAndPort());
RFuture<RedisConnection> f = c.connectAsync();
final CountDownLatch l = new CountDownLatch(1);
f.addListener((FutureListener<RedisConnection>) future -> {
RedisConnection conn = future.get();
assertThat(conn.sync(RedisCommands.PING)).isEqualTo("PONG");
l.countDown();
});
assertThat(l.await(10, TimeUnit.SECONDS)).isTrue();
}
use of org.redisson.client.RedisClient in project redisson by redisson.
the class ClusterConnectionManager method connect.
private RFuture<RedisConnection> connect(ClusterServersConfig cfg, final URL addr) {
RedisConnection connection = nodeConnections.get(addr);
if (connection != null) {
return newSucceededFuture(connection);
}
RedisClient client = createClient(addr.getHost(), addr.getPort(), cfg.getConnectTimeout(), cfg.getRetryInterval() * cfg.getRetryAttempts());
final RPromise<RedisConnection> result = newPromise();
RFuture<RedisConnection> future = client.connectAsync();
future.addListener(new FutureListener<RedisConnection>() {
@Override
public void operationComplete(Future<RedisConnection> future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
RedisConnection connection = future.getNow();
RPromise<RedisConnection> promise = newPromise();
connectListener.onConnect(promise, connection, null, config);
promise.addListener(new FutureListener<RedisConnection>() {
@Override
public void operationComplete(Future<RedisConnection> future) throws Exception {
if (!future.isSuccess()) {
result.tryFailure(future.cause());
return;
}
RedisConnection connection = future.getNow();
if (connection.isActive()) {
nodeConnections.put(addr, connection);
result.trySuccess(connection);
} else {
connection.closeAsync();
result.tryFailure(new RedisException("Connection to " + connection.getRedisClient().getAddr() + " is not active!"));
}
}
});
}
});
return result;
}
Aggregations