Search in sources :

Example 16 with FutureListener

use of io.netty.util.concurrent.FutureListener in project netty by netty.

the class DefaultHttp2ConnectionTest method removeAllStreamsWhileIteratingActiveStreamsAndExceptionOccurs.

@Test
public void removeAllStreamsWhileIteratingActiveStreamsAndExceptionOccurs() throws Exception {
    final Endpoint<Http2RemoteFlowController> remote = client.remote();
    final Endpoint<Http2LocalFlowController> local = client.local();
    for (int c = 3, s = 2; c < 5000; c += 2, s += 2) {
        local.createStream(c, false);
        remote.createStream(s, false);
    }
    final Promise<Void> promise = group.next().newPromise();
    final CountDownLatch latch = new CountDownLatch(1);
    try {
        client.forEachActiveStream(new Http2StreamVisitor() {

            @Override
            public boolean visit(Http2Stream stream) throws Http2Exception {
                // This close call is basically a noop, because the following statement will throw an exception.
                client.close(promise);
                // Do an invalid operation while iterating.
                remote.createStream(3, false);
                return true;
            }
        });
    } catch (Http2Exception ignored) {
        client.close(promise).addListener(new FutureListener<Void>() {

            @Override
            public void operationComplete(Future<Void> future) throws Exception {
                assertTrue(promise.isDone());
                latch.countDown();
            }
        });
    }
    assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(io.netty.handler.codec.http2.Http2Connection.Endpoint) Future(io.netty.util.concurrent.Future) Test(org.junit.jupiter.api.Test)

Example 17 with FutureListener

use of io.netty.util.concurrent.FutureListener in project netty by netty.

the class DnsAddressResolverGroupTest method testUseConfiguredEventLoop.

@Test
public void testUseConfiguredEventLoop() throws InterruptedException {
    NioEventLoopGroup group = new NioEventLoopGroup(1);
    final EventLoop loop = group.next();
    DefaultEventLoopGroup defaultEventLoopGroup = new DefaultEventLoopGroup(1);
    DnsNameResolverBuilder builder = new DnsNameResolverBuilder().eventLoop(loop).channelType(NioDatagramChannel.class);
    DnsAddressResolverGroup resolverGroup = new DnsAddressResolverGroup(builder);
    try {
        final Promise<?> promise = loop.newPromise();
        AddressResolver<?> resolver = resolverGroup.getResolver(defaultEventLoopGroup.next());
        resolver.resolve(new SocketAddress() {

            private static final long serialVersionUID = 3169703458729818468L;
        }).addListener(new FutureListener<Object>() {

            @Override
            public void operationComplete(Future<Object> future) {
                try {
                    assertThat(future.cause(), instanceOf(UnsupportedAddressTypeException.class));
                    assertTrue(loop.inEventLoop());
                    promise.setSuccess(null);
                } catch (Throwable cause) {
                    promise.setFailure(cause);
                }
            }
        }).await();
        promise.sync();
    } finally {
        resolverGroup.close();
        group.shutdownGracefully();
        defaultEventLoopGroup.shutdownGracefully();
    }
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) UnsupportedAddressTypeException(java.nio.channels.UnsupportedAddressTypeException) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) EventLoop(io.netty.channel.EventLoop) Future(io.netty.util.concurrent.Future) SocketAddress(java.net.SocketAddress) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test)

Example 18 with FutureListener

use of io.netty.util.concurrent.FutureListener 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)

Example 19 with FutureListener

use of io.netty.util.concurrent.FutureListener in project redisson by redisson.

the class LoadBalancerManager method add.

public RFuture<Void> add(final ClientConnectionsEntry entry) {
    final RPromise<Void> result = connectionManager.newPromise();
    FutureListener<Void> listener = new FutureListener<Void>() {

        AtomicInteger counter = new AtomicInteger(2);

        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            if (!future.isSuccess()) {
                result.tryFailure(future.cause());
                return;
            }
            if (counter.decrementAndGet() == 0) {
                addr2Entry.put(entry.getClient().getAddr(), entry);
                result.trySuccess(null);
            }
        }
    };
    RFuture<Void> slaveFuture = slaveConnectionPool.add(entry);
    slaveFuture.addListener(listener);
    RFuture<Void> pubSubFuture = pubSubConnectionPool.add(entry);
    pubSubFuture.addListener(listener);
    return result;
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RFuture(org.redisson.api.RFuture) Future(io.netty.util.concurrent.Future)

Example 20 with FutureListener

use of io.netty.util.concurrent.FutureListener in project redisson by redisson.

the class CommandAsyncService method readAllAsync.

@Override
public <T, R> RFuture<Collection<R>> readAllAsync(RedisCommand<T> command, Object... params) {
    final RPromise<Collection<R>> mainPromise = connectionManager.newPromise();
    final Set<MasterSlaveEntry> nodes = connectionManager.getEntrySet();
    final List<R> results = new ArrayList<R>();
    final AtomicInteger counter = new AtomicInteger(nodes.size());
    FutureListener<R> listener = new FutureListener<R>() {

        @Override
        public void operationComplete(Future<R> future) throws Exception {
            if (!future.isSuccess()) {
                mainPromise.tryFailure(future.cause());
                return;
            }
            R result = future.getNow();
            if (result instanceof Collection) {
                synchronized (results) {
                    results.addAll((Collection) result);
                }
            } else {
                synchronized (results) {
                    results.add(result);
                }
            }
            if (counter.decrementAndGet() == 0 && !mainPromise.isDone()) {
                mainPromise.trySuccess(results);
            }
        }
    };
    for (MasterSlaveEntry entry : nodes) {
        RPromise<R> promise = connectionManager.newPromise();
        promise.addListener(listener);
        async(true, new NodeSource(entry), connectionManager.getCodec(), command, params, promise, 0);
    }
    return mainPromise;
}
Also used : FutureListener(io.netty.util.concurrent.FutureListener) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ArrayList(java.util.ArrayList) NodeSource(org.redisson.connection.NodeSource) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) Collection(java.util.Collection) RFuture(org.redisson.api.RFuture) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future)

Aggregations

FutureListener (io.netty.util.concurrent.FutureListener)29 Future (io.netty.util.concurrent.Future)26 RFuture (org.redisson.api.RFuture)20 ChannelFuture (io.netty.channel.ChannelFuture)10 ChannelFutureListener (io.netty.channel.ChannelFutureListener)10 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 Timeout (io.netty.util.Timeout)6 TimerTask (io.netty.util.TimerTask)6 ArrayList (java.util.ArrayList)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RedisConnection (org.redisson.client.RedisConnection)6 Channel (io.netty.channel.Channel)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)4 ScheduledFuture (io.netty.util.concurrent.ScheduledFuture)4 Test (org.junit.jupiter.api.Test)4 RedisConnectionException (org.redisson.client.RedisConnectionException)4 RedisException (org.redisson.client.RedisException)4 NodeSource (org.redisson.connection.NodeSource)4