Search code examples
nettychannelillegalstateexception

Netty - Find Channel ID


I just started working with Netty. I already have a server, and im writing code for a client.

In the client, i set a new PipelineFactory for the ClientBootstrap object with this code

    bootstrap.setPipelineFactory(new ChannelPipelineFactory() 
    {
        public ChannelPipeline getPipeline() 
        {
            return Channels.pipeline(new ResponseHandler());
        }
    }
                                );

where ResponseHandler() is my class that extends SimpleChannelHandler.

Im looking to find the Channel ID. I did

 bootstrap.getPipeline().getChannel().getId();

but it throws IllelgalStateException and says i cant call getPipeline() because i have called setPipelineFactory(). What is the method to obtain the Channel ID ?


Solution

  • Have you called connect? Netty tutorial (3.5)

    I think after connecting you can get the channel ID on all the methods of SimpleChannelHandler (3.5) with

    ChannelEvent.getChannel().getId()
    

    For Netty 4

    given the ChannelHandlerContext object (variable name = ctx) it would be

    ctx.channel().id()