Search in sources :

Example 1 with RedisClient

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);
}
Also used : RedisClient(org.redisson.client.RedisClient)

Example 2 with RedisClient

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);
}
Also used : RedisClient(org.redisson.client.RedisClient)

Example 3 with RedisClient

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;
}
Also used : RedisClient(org.redisson.client.RedisClient) RedisPubSubConnection(org.redisson.client.RedisPubSubConnection) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) PubSubType(org.redisson.client.protocol.pubsub.PubSubType) RedisConnectionException(org.redisson.client.RedisConnectionException)

Example 4 with RedisClient

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();
}
Also used : RedisClient(org.redisson.client.RedisClient) PubSubType(org.redisson.client.protocol.pubsub.PubSubType) RPromise(org.redisson.misc.RPromise) Arrays(java.util.Arrays) RedisConnection(org.redisson.client.RedisConnection) BeforeClass(org.junit.BeforeClass) StringCodec(org.redisson.client.codec.StringCodec) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RedisClient(org.redisson.client.RedisClient) ArrayList(java.util.ArrayList) RFuture(org.redisson.api.RFuture) RedisPubSubListener(org.redisson.client.RedisPubSubListener) Map(java.util.Map) After(org.junit.After) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) RedisPubSubConnection(org.redisson.client.RedisPubSubConnection) AfterClass(org.junit.AfterClass) LongCodec(org.redisson.client.codec.LongCodec) FutureListener(io.netty.util.concurrent.FutureListener) RedissonPromise(org.redisson.misc.RedissonPromise) IOException(java.io.IOException) Test(org.junit.Test) CommandData(org.redisson.client.protocol.CommandData) Executors(java.util.concurrent.Executors) RedisCommands(org.redisson.client.protocol.RedisCommands) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CommandsData(org.redisson.client.protocol.CommandsData) CountDownLatch(java.util.concurrent.CountDownLatch) RedisConnection(org.redisson.client.RedisConnection) Test(org.junit.Test)

Example 5 with RedisClient

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;
}
Also used : RedisClient(org.redisson.client.RedisClient) FutureListener(io.netty.util.concurrent.FutureListener) RPromise(org.redisson.misc.RPromise) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) RFuture(org.redisson.api.RFuture) Future(io.netty.util.concurrent.Future) RedisException(org.redisson.client.RedisException) RedisException(org.redisson.client.RedisException) RedisConnectionException(org.redisson.client.RedisConnectionException) RedisConnection(org.redisson.client.RedisConnection)

Aggregations

RedisClient (org.redisson.client.RedisClient)12 RedisConnection (org.redisson.client.RedisConnection)7 Test (org.junit.Test)6 FutureListener (io.netty.util.concurrent.FutureListener)3 ArrayList (java.util.ArrayList)3 RFuture (org.redisson.api.RFuture)3 RedisConnectionException (org.redisson.client.RedisConnectionException)3 RedisPubSubConnection (org.redisson.client.RedisPubSubConnection)3 CommandData (org.redisson.client.protocol.CommandData)3 CommandsData (org.redisson.client.protocol.CommandsData)3 PubSubType (org.redisson.client.protocol.pubsub.PubSubType)3 RPromise (org.redisson.misc.RPromise)3 RedissonPromise (org.redisson.misc.RedissonPromise)3 Future (io.netty.util.concurrent.Future)2 ScheduledFuture (io.netty.util.concurrent.ScheduledFuture)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 RedisException (org.redisson.client.RedisException)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1