What is the difference between handler( ChannelHan

2020-07-14 09:45发布

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?

标签: java netty
2条回答
一夜七次
2楼-- · 2020-07-14 10:42

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2020-07-14 10:50

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
查看更多
登录 后发表回答