五 Netty 学习:服务端启动核心流程源码说明( 三 )

在这个步骤中,我们可以看到关于 JDK 底层的操作
selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this);首先拿到在前面过程中创建的 JDK 底层的 Channel,然后调用 JDK 的 register() 方法,将 this 也即 NioServerSocketChannel 对象当作 attachment 绑定到 JDK 的 Selector 上,这样后续从 Selector 拿到对应的事件之后,就可以把 Netty 领域的 Channel 拿出来 。
接下来是服务端绑定端口的逻辑,见AbstractBootstrap中的doBind0方法
private static void doBind0(final ChannelFuture regFuture, final Channel channel,final SocketAddress localAddress, final ChannelPromise promise) {// This method is invoked before channelRegistered() is triggered.Give user handlers a chance to set up// the pipeline in its channelRegistered() implementation.channel.eventLoop().execute(new Runnable() {@Overridepublic void run() {if (regFuture.isSuccess()) {channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);} else {promise.setFailure(regFuture.cause());}}});}图例本文所有图例见:processon: Netty学习笔记
代码hello-netty
更多内容见:Netty专栏
参考资料跟闪电侠学 Netty:Netty 即时聊天实战与底层原理
深度解析Netty源码
【五 Netty 学习:服务端启动核心流程源码说明】

经验总结扩展阅读