您的位置:首页 > 运维架构

netty 启动分析 NioEventLoopGroup

2016-10-24 14:36 1196 查看
ServerBootstrap.bind()方法会实例化一个channel,这个channel的类型是通过channel方法指定的,例如ServerBootstrap.channel(NioServerSocketChannel.class)

指定的同时会创建一个ChannelFactory工厂,默认是ReflectiveChannelFactory,然后bind()方法通过这个ChannelFactory实例化NioServerSocketChannel。

实例化之后会初始化 init方法,获取channel的pipeline,添加handler处理器,

p.addLast(new ChannelInitializer<Channel>() {
@Override
public void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
ChannelHandler handler = handler();
if (handler != null) {
pipeline.addLast(handler);
}
pipeline.addLast(new ServerBootstrapAcceptor(
currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
}
});

然后使用bossGroup注册,bossGroup是我们创建的NioEventLoopGroup,它维护了一组NioEventLoop,每个维护了一个selector选择器,NioEventLoopGroup。

选择一个NioEventLoop来把channel注册到这个NioEventLoop的选择器上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: