Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between handler( ChannelHandler c) and childHandler( ChannelHandler c) for ServerBootstrap?

Tags:

java

netty

I am new to Netty. One thing I find confusing is that ServerBootstrap has two methods: handler( ChannelHandler c), which is inherited from AbstractBootstrap, and childHandler( ChannelHandler c), both of which seem to be doing the same thing, based on the javadoc. So, is that true? Are there any differences between the two methods?

like image 729
JBT Avatar asked Oct 19 '14 03:10

JBT


2 Answers

The handler, which is defined in the AbstractBootstrap is used when writing Netty based clients.

When writing netty based servers, that can work upon multiple accepted channels, use a child handler which will handle I/O and data for the accepted channes, by using childHandler as defined in the ServerBootstrap.

like image 145
Manish Maheshwari Avatar answered Sep 21 '22 18:09

Manish Maheshwari


Handler method will be executed on ServerBootstrap initialization, however childHandler will be executed when connection completes.

b.group(bossGroup, workerGroup)
    .channel(NioServerSocketChannel.class)
    .handler(new LoggingHandler(LogLevel.INFO))
    .childHandler(new ServerInitializer(this.hander));

When you start the server, you can see the log as below:

2017-09-20 08:44:34,034 INFO    nioEventLoopGroup-2-1   LoggingHandler:150   [id: 0x920c9647, L:/0:0:0:0:0:0:0:0:6030] ACTIVE
2017-09-20 08:44:34,034 INFO    nioEventLoopGroup-3-1   LoggingHandler:150   [id: 0x048bb39e, L:/0:0:0:0:0:0:0:0:6031] ACTIVE
like image 23
Jacky Avatar answered Sep 17 '22 18:09

Jacky