netty的BootStrap类是一个辅助类,其暴露出的接口有助于我们更方便的建立服务器和客户端,下面从源码角度分析Bootstrap是如何引导建立客户端的(使用断点的方式跟踪源码)
1、首先找到io.netty.example中的EchoClient,打上断点,Debug模式开始跟踪源码(断点如图)
2、b.group方法
class AbastactBootStrap
public B group(EventLoopGroup group) {
if (group == null) {
throw new NullPointerException("group");
}
if (this.group != null) {
throw new IllegalStateException("group set already");
}
this.group = group;
return self();
}
这里就有了一个重要的类AbastactBootStrap,其中包含了重要的属性和方法的实现
AbastactBootStrap
属性
//保存EventLoopGroup的引用
volatile EventLoopGroup group;
//保存channel的工厂类,用于创建channel实例
private volatile ChannelFactory<? extends C> channelFactory;
//handler的引用,多为ChannelInitializer的实现类,可添加多个handler
private volatile ChannelHandler handler;
方法之后再介绍,
3、b.channel(channel.class)
public B channel(Class<? extends C> chann