netty启动流程源码2

执行如下代码:
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这个类;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值