Right way to shutdown in case of error

Trustin Lee (이희승) trustin at gmail.com
Mon Sep 14 09:42:38 EDT 2009


Regardless if you use ChannelGroup or not, you must call
ChannelFactory.releaseExternalResources() from outside ChannelHandler.

As Mike mentioned, you have to close all channels before calling
ChannelFactory.releaseExternalResources(), but it's not absolutely
necessary to use ChannelGroup.  ChannelGroup eases managing many
Channels though.

HTH,
Trustin

— Trustin Lee, http://gleamynode.net/

On Mon, Sep 14, 2009 at 8:12 PM, float_dublin <Kostya.Hvan at gmail.com> wrote:
>
> OK, I am using ChannelGroups this time still with no success. No channel gets
> into Channelgroup in case of "Connection refused". Examining ClientBootstrap
> gives no clue :( Application still hangs. So is a new thread for shuting
> down the only solution?
>
> Code:
> public class ClientTest {
>
>    @ChannelPipelineCoverage("all")
>    private class Client extends SimpleChannelHandler {
>
>        @Override
>        public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent
> e) throws Exception {
>            group.add(e.getChannel());
>        }
>
>        @Override
>        public void exceptionCaught(ChannelHandlerContext ctx, final
> ExceptionEvent e) throws Exception {
>            e.getCause().printStackTrace();
>            System.out.println("Channels in group: " + group.size());
>            group.close().addListener(new ChannelGroupFutureListener() {
>
>                public void operationComplete(ChannelGroupFuture future)
> throws Exception {
>                    bootstrap.releaseExternalResources();
>                }
>            });
>        }
>    }
>    final ClientBootstrap bootstrap = new ClientBootstrap(new
> NioClientSocketChannelFactory(
>            Executors.newCachedThreadPool(),
> Executors.newCachedThreadPool()));
>    final ChannelGroup group = new DefaultChannelGroup();
>
>    private void initClient() {
>        bootstrap.setOption("tcpNoDelay", true);
>        bootstrap.setOption("keepAlive", true);
>
>        final ChannelPipeline pipeline = bootstrap.getPipeline();
>        pipeline.addLast("handler", new Client());
>        group.add(bootstrap.connect(new InetSocketAddress("localhost",
> 20001)).getChannel());
>    }
>
>    public static void main(String[] args) {
>        new ClientTest().initClient();
>    }
> }
> --
> View this message in context: http://n2.nabble.com/Right-way-to-shutdown-in-case-of-error-tp3637599p3641407.html
> Sent from the Netty User Group mailing list archive at Nabble.com.
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users
>



More information about the netty-users mailing list