Example usage for io.netty.channel.group ChannelGroupFuture isSuccess

List of usage examples for io.netty.channel.group ChannelGroupFuture isSuccess

Introduction

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

Prototype

@Override
boolean isSuccess();

Source Link

Document

Returns true if and only if all I/O operations associated with this future were successful without any failure.

Usage

From source file:com.linkedin.mitm.proxy.ProxyServer.java

License:Open Source License

/**
 * Stop proxy server//w w  w  .  j  a  v  a 2  s.  c om
 * */
public void stop() {
    ChannelGroupFuture future = _allChannels.close().awaitUninterruptibly();
    if (!future.isSuccess()) {
        final Iterator<ChannelFuture> iter = future.iterator();
        while (iter.hasNext()) {
            final ChannelFuture cf = iter.next();
            if (!cf.isSuccess()) {
                LOG.warn(String.format("Failed to close channel %s because %s", cf.channel(), cf.cause()));
            }
        }
    }
    _acceptorGroup.shutdownGracefully();
    _upstreamWorkerGroup.shutdownGracefully();
    _downstreamWorkerGroup.shutdownGracefully();
}

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"));
                        }/* w ww. ja va  2  s  . 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:org.apache.activemq.artemis.core.remoting.impl.netty.NettyAcceptor.java

License:Apache License

public synchronized void stop() {
    if (channelClazz == null) {
        return;/* w  w  w  .j ava2s .com*/
    }

    if (protocolHandler != null) {
        protocolHandler.close();
    }

    if (batchFlusherFuture != null) {
        batchFlusherFuture.cancel(false);

        flusher.cancel();

        flusher = null;

        batchFlusherFuture = null;
    }

    // serverChannelGroup has been unbound in pause()
    if (serverChannelGroup != null) {
        serverChannelGroup.close().awaitUninterruptibly();
    }

    if (channelGroup != null) {
        ChannelGroupFuture future = channelGroup.close().awaitUninterruptibly();

        if (!future.isSuccess()) {
            ActiveMQServerLogger.LOGGER.nettyChannelGroupError();
            Iterator<Channel> iterator = future.group().iterator();
            while (iterator.hasNext()) {
                Channel channel = iterator.next();
                if (channel.isActive()) {
                    ActiveMQServerLogger.LOGGER.nettyChannelStillOpen(channel, channel.remoteAddress());
                }
            }
        }
    }

    // Shutdown the EventLoopGroup if no new task was added for 100ms or if
    // 3000ms elapsed.
    eventLoopGroup.shutdownGracefully(100, 3000, TimeUnit.MILLISECONDS);
    eventLoopGroup = null;

    channelClazz = null;

    for (Connection connection : connections.values()) {
        listener.connectionDestroyed(connection.getID());
    }

    connections.clear();

    if (notificationService != null) {
        TypedProperties props = new TypedProperties();
        props.putSimpleStringProperty(new SimpleString("factory"),
                new SimpleString(NettyAcceptorFactory.class.getName()));
        props.putSimpleStringProperty(new SimpleString("host"), new SimpleString(host));
        props.putIntProperty(new SimpleString("port"), port);
        Notification notification = new Notification(null, CoreNotificationType.ACCEPTOR_STOPPED, props);
        try {
            notificationService.sendNotification(notification);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    paused = false;
}

From source file:org.apache.activemq.artemis.core.remoting.impl.netty.NettyAcceptor.java

License:Apache License

public synchronized void pause() {
    if (paused) {
        return;/* ww w. ja  v  a2 s .  co m*/
    }

    if (channelClazz == null) {
        return;
    }

    // We *pause* the acceptor so no new connections are made
    if (serverChannelGroup != null) {
        ChannelGroupFuture future = serverChannelGroup.close().awaitUninterruptibly();
        if (!future.isSuccess()) {
            ActiveMQServerLogger.LOGGER.nettyChannelGroupBindError();
            Iterator<Channel> iterator = future.group().iterator();
            while (iterator.hasNext()) {
                Channel channel = iterator.next();
                if (channel.isActive()) {
                    ActiveMQServerLogger.LOGGER.nettyChannelStillBound(channel, channel.remoteAddress());
                }
            }
        }
    }
    paused = true;
}

From source file:org.apache.activemq.core.remoting.impl.netty.NettyAcceptor.java

License:Apache License

public synchronized void stop() {
    if (channelClazz == null) {
        return;/*from w  w w  . j av  a  2s . co m*/
    }

    if (protocolHandler != null) {
        protocolHandler.close();
    }

    if (batchFlusherFuture != null) {
        batchFlusherFuture.cancel(false);

        flusher.cancel();

        flusher = null;

        batchFlusherFuture = null;
    }

    // serverChannelGroup has been unbound in pause()
    serverChannelGroup.close().awaitUninterruptibly();
    ChannelGroupFuture future = channelGroup.close().awaitUninterruptibly();

    if (!future.isSuccess()) {
        ActiveMQServerLogger.LOGGER.nettyChannelGroupError();
        Iterator<Channel> iterator = future.group().iterator();
        while (iterator.hasNext()) {
            Channel channel = iterator.next();
            if (channel.isActive()) {
                ActiveMQServerLogger.LOGGER.nettyChannelStillOpen(channel, channel.remoteAddress());
            }
        }
    }

    // Shutdown the EventLoopGroup if no new task was added for 100ms or if
    // 3000ms elapsed.
    eventLoopGroup.shutdownGracefully(100, 3000, TimeUnit.MILLISECONDS);
    eventLoopGroup = null;

    channelClazz = null;

    for (Connection connection : connections.values()) {
        listener.connectionDestroyed(connection.getID());
    }

    connections.clear();

    if (notificationService != null) {
        TypedProperties props = new TypedProperties();
        props.putSimpleStringProperty(new SimpleString("factory"),
                new SimpleString(NettyAcceptorFactory.class.getName()));
        props.putSimpleStringProperty(new SimpleString("host"), new SimpleString(host));
        props.putIntProperty(new SimpleString("port"), port);
        Notification notification = new Notification(null, CoreNotificationType.ACCEPTOR_STOPPED, props);
        try {
            notificationService.sendNotification(notification);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    paused = false;
}

From source file:org.apache.activemq.core.remoting.impl.netty.NettyAcceptor.java

License:Apache License

public synchronized void pause() {
    if (paused) {
        return;//from ww  w.j  a  va2s.co  m
    }

    if (channelClazz == null) {
        return;
    }

    // We *pause* the acceptor so no new connections are made
    ChannelGroupFuture future = serverChannelGroup.close().awaitUninterruptibly();
    if (!future.isSuccess()) {
        ActiveMQServerLogger.LOGGER.nettyChannelGroupBindError();
        Iterator<Channel> iterator = future.group().iterator();
        while (iterator.hasNext()) {
            Channel channel = iterator.next();
            if (channel.isActive()) {
                ActiveMQServerLogger.LOGGER.nettyChannelStillBound(channel, channel.remoteAddress());
            }
        }
    }
    paused = true;
}

From source file:org.hornetq.core.remoting.impl.netty.NettyAcceptor.java

License:Apache License

public synchronized void stop() {
    if (channelClazz == null) {
        return;//from  w w  w . ja  v a 2s .c om
    }

    if (protocolHandler != null) {
        protocolHandler.close();
    }

    if (batchFlusherFuture != null) {
        batchFlusherFuture.cancel(false);

        flusher.cancel();

        flusher = null;

        batchFlusherFuture = null;
    }

    // serverChannelGroup has been unbound in pause()
    serverChannelGroup.close().awaitUninterruptibly();
    ChannelGroupFuture future = channelGroup.close().awaitUninterruptibly();

    if (!future.isSuccess()) {
        HornetQServerLogger.LOGGER.nettyChannelGroupError();
        Iterator<Channel> iterator = future.group().iterator();
        while (iterator.hasNext()) {
            Channel channel = iterator.next();
            if (channel.isActive()) {
                HornetQServerLogger.LOGGER.nettyChannelStillOpen(channel, channel.remoteAddress());
            }
        }
    }

    // Shutdown the EventLoopGroup if no new task was added for 100ms or if
    // 3000ms elapsed.
    eventLoopGroup.shutdownGracefully(100, 3000, TimeUnit.MILLISECONDS);
    eventLoopGroup = null;

    channelClazz = null;

    for (Connection connection : connections.values()) {
        listener.connectionDestroyed(connection.getID());
    }

    connections.clear();

    if (notificationService != null) {
        TypedProperties props = new TypedProperties();
        props.putSimpleStringProperty(new SimpleString("factory"),
                new SimpleString(NettyAcceptorFactory.class.getName()));
        props.putSimpleStringProperty(new SimpleString("host"), new SimpleString(host));
        props.putIntProperty(new SimpleString("port"), port);
        Notification notification = new Notification(null, CoreNotificationType.ACCEPTOR_STOPPED, props);
        try {
            notificationService.sendNotification(notification);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    paused = false;
}

From source file:org.hornetq.core.remoting.impl.netty.NettyAcceptor.java

License:Apache License

public synchronized void pause() {
    if (paused) {
        return;/*from   w w w.  java  2 s  . c om*/
    }

    if (channelClazz == null) {
        return;
    }

    // We *pause* the acceptor so no new connections are made
    ChannelGroupFuture future = serverChannelGroup.close().awaitUninterruptibly();
    if (!future.isSuccess()) {
        HornetQServerLogger.LOGGER.nettyChannelGroupBindError();
        Iterator<Channel> iterator = future.group().iterator();
        while (iterator.hasNext()) {
            Channel channel = iterator.next();
            if (channel.isActive()) {
                HornetQServerLogger.LOGGER.nettyChannelStillBound(channel, channel.remoteAddress());
            }
        }
    }
    paused = true;
}

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

License:Open Source License

@Override
protected void doStop() {
    try {/*from w w  w  . j a va  2s  .  com*/
        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.spout.vanilla.protocol.rcon.RemoteConnectionServer.java

License:Open Source License

public void close() throws IOException {
    ChannelGroupFuture f = group.close().awaitUninterruptibly();
    if (!f.isSuccess()) {
        for (ChannelFuture future : f) {
            if (!future.isSuccess()) {
                throw new IOException(future.cause());
            }/*from   w  w w .j  a  va2 s .c  o m*/
        }
    }
    bootstrap.group().shutdownGracefully();
}