Example usage for io.netty.channel.group ChannelGroupFutureListener ChannelGroupFutureListener

List of usage examples for io.netty.channel.group ChannelGroupFutureListener ChannelGroupFutureListener

Introduction

In this page you can find the example usage for io.netty.channel.group ChannelGroupFutureListener ChannelGroupFutureListener.

Prototype

ChannelGroupFutureListener

Source Link

Usage

From source file:com.linkedin.r2.transport.http.client.HttpNettyClient.java

License:Apache License

@Override
public void shutdown(final Callback<None> callback) {
    LOG.info("Shutdown requested");
    if (_state.compareAndSet(State.RUNNING, State.SHUTTING_DOWN)) {
        LOG.info("Shutting down");
        final long deadline = System.currentTimeMillis() + _shutdownTimeout;
        TimeoutCallback<None> closeChannels = new TimeoutCallback<None>(_scheduler, _shutdownTimeout,
                TimeUnit.MILLISECONDS, new Callback<None>() {
                    private void finishShutdown() {
                        _state.set(State.REQUESTS_STOPPING);
                        // Timeout any waiters which haven't received a Channel yet
                        for (Callback<Channel> callback : _channelPoolManager.cancelWaiters()) {
                            callback.onError(
                                    new TimeoutException("Operation did not complete before shutdown"));
                        }//from  www . ja  va  2s.c o  m

                        // Timeout any requests still pending response
                        for (Channel c : _allChannels) {
                            TransportCallback<RestResponse> callback = c
                                    .attr(RAPResponseHandler.CALLBACK_ATTR_KEY).getAndRemove();
                            if (callback != null) {
                                errorResponse(callback,
                                        new TimeoutException("Operation did not complete before shutdown"));
                            }
                        }

                        // Close all active and idle Channels
                        final TimeoutRunnable afterClose = new TimeoutRunnable(_scheduler,
                                deadline - System.currentTimeMillis(), TimeUnit.MILLISECONDS, new Runnable() {
                                    @Override
                                    public void run() {
                                        _state.set(State.SHUTDOWN);
                                        LOG.info("Shutdown complete");
                                        callback.onSuccess(None.none());
                                    }
                                }, "Timed out waiting for channels to close, continuing shutdown");
                        _allChannels.close().addListener(new ChannelGroupFutureListener() {
                            @Override
                            public void operationComplete(ChannelGroupFuture channelGroupFuture)
                                    throws Exception {
                                if (!channelGroupFuture.isSuccess()) {
                                    LOG.warn("Failed to close some connections, ignoring");
                                }
                                afterClose.run();
                            }
                        });
                    }

                    @Override
                    public void onSuccess(None none) {
                        LOG.info("All connection pools shut down, closing all channels");
                        finishShutdown();
                    }

                    @Override
                    public void onError(Throwable e) {
                        LOG.warn("Error shutting down HTTP connection pools, ignoring and continuing shutdown",
                                e);
                        finishShutdown();
                    }
                }, "Connection pool shutdown timeout exceeded (" + _shutdownTimeout + "ms)");
        _channelPoolManager.shutdown(closeChannels);
        _jmxManager.onProviderShutdown(_channelPoolManager);
    } else {
        callback.onError(new IllegalStateException("Shutdown has already been requested."));
    }
}

From source file:com.spotify.ffwd.debug.NettyDebugServer.java

License:Apache License

public AsyncFuture<Void> stop() {
    final Channel server = this.server.getAndSet(null);

    if (server == null) {
        throw new IllegalStateException("server not started");
    }//from w  w  w  .  j a v  a2 s  .  com

    final ResolvableFuture<Void> serverClose = async.future();

    server.close().addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture f) throws Exception {
            if (!f.isSuccess()) {
                serverClose.fail(f.cause());
                return;
            }

            serverClose.resolve(null);
        }
    });

    final ResolvableFuture<Void> channelGroupClose = async.future();

    connected.close().addListener(new ChannelGroupFutureListener() {
        @Override
        public void operationComplete(ChannelGroupFuture f) throws Exception {
            if (!f.isSuccess()) {
                channelGroupClose.fail(f.cause());
                return;
            }

            channelGroupClose.resolve(null);
        }
    });

    return async.collectAndDiscard(ImmutableList.<AsyncFuture<Void>>of(serverClose, channelGroupClose));
}

From source file:io.jsync.http.impl.DefaultHttpServer.java

License:Open Source License

private void actualClose(final DefaultContext closeContext, final Handler<AsyncResult<Void>> done) {
    if (id != null) {
        async.sharedHttpServers().remove(id);
    }/*  w w w  . j  a va2s .c o  m*/

    for (ServerConnection conn : connectionMap.values()) {
        conn.close();
    }

    // We need to reset it since sock.internalClose() above can call into the close handlers of sockets on the same thread
    // which can cause context id for the thread to change!

    async.setContext(closeContext);

    final CountDownLatch latch = new CountDownLatch(1);

    ChannelGroupFuture fut = serverChannelGroup.close();
    fut.addListener(new ChannelGroupFutureListener() {
        public void operationComplete(ChannelGroupFuture channelGroupFuture) throws Exception {
            latch.countDown();
        }
    });

    // Always sync
    try {
        latch.await(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
    }

    executeCloseDone(closeContext, done, fut.cause());
}

From source file:io.jsync.net.impl.DefaultNetServer.java

License:Open Source License

private void actualClose(final DefaultContext closeContext, final Handler<AsyncResult<Void>> done) {
    if (id != null) {
        async.sharedNetServers().remove(id);
    }//  w w w.j  a v  a2 s  . com

    for (DefaultNetSocket sock : socketMap.values()) {
        sock.close();
    }

    // We need to reset it since sock.internalClose() above can call into the close handlers of sockets on the same thread
    // which can cause context id for the thread to change!

    async.setContext(closeContext);

    ChannelGroupFuture fut = serverChannelGroup.close();
    fut.addListener(new ChannelGroupFutureListener() {
        public void operationComplete(ChannelGroupFuture fut) throws Exception {
            executeCloseDone(closeContext, done, fut.cause());
        }
    });

}

From source file:org.helios.octo.client.OctoShared.java

License:Open Source License

/**
 * Stops and disconnects all clients and de-allocates all resources
 *//* w ww  .j  av  a 2  s  . c  om*/
public void shutdownAll() {
    if (!stopping.compareAndSet(false, true)) {
        throw new RuntimeException("This OctoShared is already being shutdown");
    }
    log.info("Stopping all OctoClients....");
    channelGroup.close().addListener(new ChannelGroupFutureListener() {
        @Override
        public void operationComplete(ChannelGroupFuture future) throws Exception {
            log.info("ChannelGroup Closed");
            group.shutdownGracefully().addListener(new FutureListener<Object>() {
                @Override
                public void operationComplete(Future<Object> future) throws Exception {
                    log.info("Event Loop Group Closed.\n\n\t\tBye.");
                    instance = null;
                    stopping.set(false);
                }
            });
        }
    });
}

From source file:org.robotninjas.protobuf.netty.server.RpcServer.java

License:Open Source License

@Override
protected void doStop() {
    try {//from   w  w w  .  ja v a 2  s  . c om
        ChannelGroupFuture f = allChannels.close();
        f.addListener(new ChannelGroupFutureListener() {
            @Override
            public void operationComplete(ChannelGroupFuture future) throws Exception {
                if (future.isSuccess()) {
                    notifyStopped();
                } else {
                    notifyFailed(future.cause());
                }
            }
        });
    } catch (Throwable t) {
        notifyFailed(t);
        Throwables.propagate(t);
    }
}

From source file:org.vertx.java.core.http.impl.DefaultHttpServer.java

License:Open Source License

private void actualClose(final DefaultContext closeContext, final Handler<AsyncResult<Void>> done) {
    if (id != null) {
        vertx.sharedHttpServers().remove(id);
    }//from   w w  w  . ja  v a  2  s.co  m

    for (ServerConnection conn : connectionMap.values()) {
        conn.close();
    }

    // We need to reset it since sock.internalClose() above can call into the close handlers of sockets on the same thread
    // which can cause context id for the thread to change!

    vertx.setContext(closeContext);

    final CountDownLatch latch = new CountDownLatch(1);

    ChannelGroupFuture fut = serverChannelGroup.close();
    fut.addListener(new ChannelGroupFutureListener() {
        public void operationComplete(ChannelGroupFuture channelGroupFuture) throws Exception {
            latch.countDown();
        }
    });

    // Always sync
    try {
        latch.await(10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
    }

    executeCloseDone(closeContext, done, fut.cause());
}

From source file:org.vertx.java.core.net.impl.DefaultNetServer.java

License:Open Source License

private void actualClose(final DefaultContext closeContext, final Handler<AsyncResult<Void>> done) {
    if (id != null) {
        vertx.sharedNetServers().remove(id);
    }//from w  w  w.j a v a 2s .c o m

    for (DefaultNetSocket sock : socketMap.values()) {
        sock.close();
    }

    // We need to reset it since sock.internalClose() above can call into the close handlers of sockets on the same thread
    // which can cause context id for the thread to change!

    vertx.setContext(closeContext);

    ChannelGroupFuture fut = serverChannelGroup.close();
    fut.addListener(new ChannelGroupFutureListener() {
        public void operationComplete(ChannelGroupFuture fut) throws Exception {
            executeCloseDone(closeContext, done, fut.cause());
        }
    });

}