Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantage of ChannelInitializer over Channel Handler in Netty

What are the advantages of using ChannelInitializer over a chain of ChannelHandlers directly?

For example with a server bootstrap I could do:

bootstrap.childHandler(channel_handler);

Add in the implementation of channel_handler i would implement the following

class simple_channel_handler implements ChannelHandler
{
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("handler added");
        ctx.pipeline().addLast(new simple_channel_handler_2());
    }
}

Where as in case of ChannelInitializer

        ch.pipeline().addLast(
                               new channel_handler_1(), 
                               new channel_handler_2()
                             );

And in each of the handler i could do

class channel_handler_1 extends ChannelInboundHandlerAdapter
{

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("Channel just became active");
        ctx.fireChannelRead(ctx); // Fire directly to channel handler 2
    }
}

So is the only advantage that channel handler one need not understand where it is firing the channel read to ? I don't see any other advantage of using a channel initializer

like image 416
Desert Ice Avatar asked Jan 28 '15 10:01

Desert Ice


People also ask

What is Netty handler?

Events and Handlers. Netty uses an event-driven application paradigm, so the pipeline of the data processing is a chain of events going through handlers. Events and handlers can be related to the inbound and outbound data flow. Inbound events can be the following: Channel activation and deactivation.

Is Netty a TCP?

Netty is a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket server.


1 Answers

As per the documentation (see here http://netty.io/wiki/user-guide-for-4.x.html)

The handler specified here will always be evaluated by a newly accepted Channel. The ChannelInitializer is a special handler that is purposed to help a user configure a new Channel. It is most likely that you want to configure the ChannelPipeline of the new Channel by adding some handlers such as DiscardServerHandler to implement your network application. As the application gets complicated, it is likely that you will add more handlers to the pipeline and extract this anonymous class into a top level class eventually.

So the ChannelInitializer is a clean way to add your handlers as needed, especially if you have more than one.

It does not prevent to have one handler adding more handlers (as you did in your first example), for instance for dynamically add/remmove one handler in the pipeline according to the context, but for "static" or "default" series of handlers, using the ChannelInitializer is a cleaner way since it is really close to the bootstrap definition and therefore more readable.

like image 147
Frederic Brégier Avatar answered Sep 24 '22 02:09

Frederic Brégier