Search in sources :

Example 1 with RedissonPromise

use of org.redisson.misc.RedissonPromise in project redisson by redisson.

the class ConnectionWatchdog method reconnect.

private void reconnect(final RedisConnection connection, final Channel channel) {
    if (connection.getReconnectListener() != null) {
        // new connection used only for channel init
        RedisConnection rc = new RedisConnection(connection.getRedisClient(), channel);
        RPromise<RedisConnection> connectionFuture = new RedissonPromise<RedisConnection>();
        connection.getReconnectListener().onReconnect(rc, connectionFuture);
        connectionFuture.addListener(new FutureListener<RedisConnection>() {

            @Override
            public void operationComplete(Future<RedisConnection> future) throws Exception {
                if (future.isSuccess()) {
                    refresh(connection, channel);
                }
            }
        });
    } else {
        refresh(connection, channel);
    }
}
Also used : RedissonPromise(org.redisson.misc.RedissonPromise) RedisException(org.redisson.client.RedisException) RedisConnection(org.redisson.client.RedisConnection)

Example 2 with RedissonPromise

use of org.redisson.misc.RedissonPromise in project redisson by redisson.

the class RedisClient method connectPubSubAsync.

public RFuture<RedisPubSubConnection> connectPubSubAsync() {
    final RPromise<RedisPubSubConnection> f = new RedissonPromise<RedisPubSubConnection>();
    ChannelFuture channelFuture = bootstrap.connect();
    channelFuture.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(final ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                final RedisPubSubConnection c = new RedisPubSubConnection(RedisClient.this, future.channel());
                bootstrap.group().execute(new Runnable() {

                    public void run() {
                        if (!f.trySuccess(c)) {
                            c.closeAsync();
                        }
                    }
                });
            } else {
                bootstrap.group().execute(new Runnable() {

                    public void run() {
                        f.tryFailure(future.cause());
                    }
                });
            }
        }
    });
    return f;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RedissonPromise(org.redisson.misc.RedissonPromise) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 3 with RedissonPromise

use of org.redisson.misc.RedissonPromise in project redisson by redisson.

the class RedisConnection method async.

public <T, R> RFuture<R> async(long timeout, Codec encoder, RedisCommand<T> command, Object... params) {
    final RPromise<R> promise = new RedissonPromise<R>();
    if (timeout == -1) {
        timeout = redisClient.getCommandTimeout();
    }
    if (redisClient.getBootstrap().group().isShuttingDown()) {
        RedissonShutdownException cause = new RedissonShutdownException("Redisson is shutdown");
        return RedissonPromise.newFailedFuture(cause);
    }
    final ScheduledFuture<?> scheduledFuture = redisClient.getBootstrap().group().next().schedule(new Runnable() {

        @Override
        public void run() {
            RedisTimeoutException ex = new RedisTimeoutException("Command execution timeout for " + redisClient.getAddr());
            promise.tryFailure(ex);
        }
    }, timeout, TimeUnit.MILLISECONDS);
    promise.addListener(new FutureListener<R>() {

        @Override
        public void operationComplete(Future<R> future) throws Exception {
            scheduledFuture.cancel(false);
        }
    });
    send(new CommandData<T, R>(promise, encoder, command, params));
    return promise;
}
Also used : RedissonPromise(org.redisson.misc.RedissonPromise) RedissonShutdownException(org.redisson.RedissonShutdownException) RedissonShutdownException(org.redisson.RedissonShutdownException)

Example 4 with RedissonPromise

use of org.redisson.misc.RedissonPromise in project redisson by redisson.

the class RedisClient method connectAsync.

public RFuture<RedisConnection> connectAsync() {
    final RPromise<RedisConnection> f = new RedissonPromise<RedisConnection>();
    ChannelFuture channelFuture = bootstrap.connect();
    channelFuture.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(final ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                final RedisConnection c = new RedisConnection(RedisClient.this, future.channel());
                bootstrap.group().execute(new Runnable() {

                    public void run() {
                        if (!f.trySuccess(c)) {
                            c.closeAsync();
                        }
                    }
                });
            } else {
                bootstrap.group().execute(new Runnable() {

                    public void run() {
                        f.tryFailure(future.cause());
                    }
                });
            }
        }
    });
    return f;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) RedissonPromise(org.redisson.misc.RedissonPromise) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 5 with RedissonPromise

use of org.redisson.misc.RedissonPromise 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)

Aggregations

RedissonPromise (org.redisson.misc.RedissonPromise)6 RedisConnection (org.redisson.client.RedisConnection)3 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 RedisClient (org.redisson.client.RedisClient)2 CommandData (org.redisson.client.protocol.CommandData)2 CommandsData (org.redisson.client.protocol.CommandsData)2 RedissonShutdownException (org.redisson.RedissonShutdownException)1 RedisException (org.redisson.client.RedisException)1