Search in sources :

Example 6 with RedisClient

use of org.redisson.client.RedisClient in project redisson by redisson.

the class MasterSlaveConnectionManager method createClient.

@Override
public RedisClient createClient(NodeType type, String host, int port) {
    RedisClient client = createClient(host, port, config.getConnectTimeout(), config.getRetryInterval() * config.getRetryAttempts());
    clients.add(new RedisClientEntry(client, commandExecutor, type));
    return client;
}
Also used : RedisClient(org.redisson.client.RedisClient)

Example 7 with RedisClient

use of org.redisson.client.RedisClient in project redisson by redisson.

the class ReplicatedConnectionManager method connect.

private RFuture<RedisConnection> connect(BaseMasterSlaveServersConfig<?> 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)

Example 8 with RedisClient

use of org.redisson.client.RedisClient in project redisson by redisson.

the class RedisClientTest method test.

@Test
public void test() throws InterruptedException {
    RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerInstance().getRedisServerBindAddress(), RedisRunner.getDefaultRedisServerInstance().getRedisServerPort(), 1000000, 1000000);
    final RedisConnection conn = c.connect();
    conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);
    ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
    for (int i = 0; i < 100000; i++) {
        pool.execute(() -> {
            conn.async(StringCodec.INSTANCE, RedisCommands.INCR, "test");
        });
    }
    pool.shutdown();
    assertThat(pool.awaitTermination(1, TimeUnit.HOURS)).isTrue();
    assertThat((Long) conn.sync(LongCodec.INSTANCE, RedisCommands.GET, "test")).isEqualTo(100000);
    conn.sync(RedisCommands.FLUSHDB);
}
Also used : RedisClient(org.redisson.client.RedisClient) ExecutorService(java.util.concurrent.ExecutorService) RedisConnection(org.redisson.client.RedisConnection) Test(org.junit.Test)

Example 9 with RedisClient

use of org.redisson.client.RedisClient in project redisson by redisson.

the class RedisClientTest method testPipeline.

@Test
public void testPipeline() throws InterruptedException, ExecutionException {
    RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerBindAddressAndPort());
    RedisConnection conn = c.connect();
    conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);
    List<CommandData<?, ?>> commands = new ArrayList<CommandData<?, ?>>();
    CommandData<String, String> cmd1 = conn.create(null, RedisCommands.PING);
    commands.add(cmd1);
    CommandData<Long, Long> cmd2 = conn.create(null, RedisCommands.INCR, "test");
    commands.add(cmd2);
    CommandData<Long, Long> cmd3 = conn.create(null, RedisCommands.INCR, "test");
    commands.add(cmd3);
    CommandData<String, String> cmd4 = conn.create(null, RedisCommands.PING);
    commands.add(cmd4);
    RPromise<Void> p = new RedissonPromise<Void>();
    conn.send(new CommandsData(p, commands));
    assertThat(cmd1.getPromise().get()).isEqualTo("PONG");
    assertThat(cmd2.getPromise().get()).isEqualTo(1);
    assertThat(cmd3.getPromise().get()).isEqualTo(2);
    assertThat(cmd4.getPromise().get()).isEqualTo("PONG");
    conn.sync(RedisCommands.FLUSHDB);
}
Also used : RedissonPromise(org.redisson.misc.RedissonPromise) ArrayList(java.util.ArrayList) RedisClient(org.redisson.client.RedisClient) CommandsData(org.redisson.client.protocol.CommandsData) CommandData(org.redisson.client.protocol.CommandData) RedisConnection(org.redisson.client.RedisConnection) Test(org.junit.Test)

Example 10 with RedisClient

use of org.redisson.client.RedisClient in project redisson by redisson.

the class RedisClientTest method testSubscribe.

@Test
public void testSubscribe() throws InterruptedException {
    RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerBindAddressAndPort());
    RedisPubSubConnection pubSubConnection = c.connectPubSub();
    final CountDownLatch latch = new CountDownLatch(2);
    pubSubConnection.addListener(new RedisPubSubListener<Object>() {

        @Override
        public boolean onStatus(PubSubType type, String channel) {
            assertThat(type).isEqualTo(PubSubType.SUBSCRIBE);
            assertThat(Arrays.asList("test1", "test2").contains(channel)).isTrue();
            latch.countDown();
            return true;
        }

        @Override
        public void onMessage(String channel, Object message) {
        }

        @Override
        public void onPatternMessage(String pattern, String channel, Object message) {
        }
    });
    pubSubConnection.subscribe(StringCodec.INSTANCE, "test1", "test2");
    latch.await(10, TimeUnit.SECONDS);
}
Also used : RedisClient(org.redisson.client.RedisClient) RedisPubSubConnection(org.redisson.client.RedisPubSubConnection) PubSubType(org.redisson.client.protocol.pubsub.PubSubType) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

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