JBT
JBT

Reputation: 8756

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

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?

Upvotes: 7

Views: 4213

Answers (2)

Manish Maheshwari
Manish Maheshwari

Reputation: 4134

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 channels, by using childHandler as defined in the ServerBootstrap.

Upvotes: 8

Jacky
Jacky

Reputation: 155

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

Upvotes: 2

Related Questions