执行如下代码:
serverBootstrap.group(bossGroup,workerGroup)
跟踪代码:
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
//bossGroup,workerGroup
super.group(parentGroup);
ObjectUtil.checkNotNull(childGroup, “childGroup”);
if (this.childGroup != null) {
throw new IllegalStateException(“childGroup set already”);
}
//childGroup=workerGroup
this.childGroup = childGroup;
return this;
}
发现 ServerBootstrap 这个类的childGroup保存了workgroup信息;AbstractBootstrap保存了parentGroup的信息;
接着执行代码:
//设置使用NioServerSocketChannel作为服务器通道的实现
.channel(NioServerSocketChannel.class)
跟踪代码:
//泛型B 为 ServerBootstrap C 为 ServerChannel
public B channel(Class<? extends C> channelClass) {
//channelClass=NioServerSocketChannel.class
return channelFactory(new ReflectiveChannelFactory(
ObjectUtil.checkNotNull(channelClass, “channelClass”)
));
}
public ReflectiveChannelFactory(Class<? extends T> clazz) {
//判断不为空
ObjectUtil.checkNotNull(clazz, "clazz");
try {
//clazz=NioServerSocketChannel.class
//constructor=NioServerSocketChannel.class.getConstructor();
this.constructor = clazz.getConstructor();
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("Class " + StringUtil.simpleClassName(clazz) +
" does not have a public non-arg constructor", e);
}
}
发现AbstractBootstrap这个类的 private volatile ChannelFactory<? extends C> channelFactory;这个类的这个属性赋值了ReflectiveChannelFactory这个.且这个类的构造方法中有保存了一个构造器属性,这个构造器为NioServerSocketChannel的类,后面会通` 过这个类的newchannel()方法创建一个NioServerSocketChannel的实体;
接着执行如下比较重要代码:
// 是netty的一大核心概念,表示数据流经过的处理器
.handler(new NettyTestHendler())
这个类使我们自定义的NettyTestHendler类
跟踪代码:
发现只是简单的赋值了AbstractBootstrap的属性private volatile ChannelHandler handler
接着执行如下代码:
.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
nioSocketChannel.pipeline().addLast(new StringDecoder(),new NettyServerHendler());
}
});
跟踪代码:
发现只是简单的赋值了ServerBootstrap的属性private volatile ChannelHandler handler
总结:可以看出来AbstractBootstrap保存的都是父类有关处理有关的信息.ServerBootstrap保存都是和子处理类有关的信息,其中里面具体的信息后面会分析如ChannelInitializer这个类;