菜鸡学习netty源码(一)——ServerBootStrap启动

1.概述

对于初学者而然,写一个netty本地进行测试的Server端和Client端,我们最先接触到的类就是ServerBootstrap和Bootstrap。这两个类都有一个公共的父类就是AbstractBootstrap.

那既然 ServerBootstrap和Bootstrap都有一个公共的分类,那就证明它们两个肯定有很多公共的职责和方法。

AbstractBootStrap

我们先进行来看AbstractBootStrap这个类

构造函数

public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C extends Channel> implements Cloneable {

    volatile EventLoopGroup group;
    @SuppressWarnings("deprecation")
    private volatile ChannelFactory<? extends C> channelFactory;
    private volatile SocketAddress localAddress;
    private final Map<ChannelOption<?>, Object> options = new LinkedHashMap<ChannelOption<?>, Object>();
    private final Map<AttributeKey<?>, Object> attrs = new LinkedHashMap<AttributeKey<?>, Object>();
    private volatile ChannelHandler handler;

    AbstractBootstrap() {
        // Disallow extending from a different package.
    }

    AbstractBootstrap(AbstractBootstrap<B, C> bootstrap) {
        group = bootstrap.group;
        channelFactory = bootstrap.channelFactory;
        handler = bootstrap.handler;
        localAddress = bootstrap.localAddress;
        synchronized (bootstrap.options) {
            options.putAll(bootstrap.options);
        }
        synchronized (bootstrap.attrs) {
            attrs.putAll(bootstrap.attrs);
        }
    }

............. //省略别的代码
}

从上面的代码 我们可以看出来几点:

1.AbstractBootStrap这个抽象类定义B,C两个泛型,B泛型继承了AbstractBootStrap这个类,C泛型继承于Channel这个类

2.提供了两个构造函数,一个是无参的 一个是有参的,有参的构造函数中,有两行synchronized 进行修饰的代码,是options和attrs属性可能会有并发问题吗?

3.实现了Cloneable接口,这个Cloneable接口在java中表示这个类是否可以被克隆,我对此有这个疑问,为啥要实现这个接口呢 

看完AbstractBootStrap的这个构造函数,我们一起看下ServerBootstrap和Bootstrap在我们写的demo中都涉及到一些方法,

group方法

   public B group(EventLoopGroup group) {
        ObjectUtil.checkNotNull(group, "group");
        if (this.group != null) {
            throw new IllegalStateException("group set already");
        }
        this.group = group;
        return self();
    }

 这个group方法看上去就是一个简单的属性赋值的操作,并没有什么特别之处,跟咱们普通的set方法的区别在于最后调用了self方法,但是我们看下self方法之后,就是返回了一个自身的对象。

    private B self() {
        return (B) this;
    }

channel方法

    public B channel(Class<? extends C> channelClass) {
        return channelFactory(new ReflectiveChannelFactory<C>(
                ObjectUtil.checkNotNull(channelClass, "channelClass")
        ));
    }

这块就跟group方法不一样了,看上去不再是简单的属性赋值了,我们点击到channelFactory方法中进行查看。心想着总该不一样了,但是点击进去还是属性赋值的操作,但是这次属性赋值有点区别了,是根据传入的channelClass的Class对象 构建了一个ReflectiveChannelFactory对象

  public B channelFactory(io.netty.channel.ChannelFactory<? extends C> channelFactory) {
        return channelFactory((ChannelFactory<C>) channelFactory);
    }

    public B channelFactory(ChannelFactory<? extends C> channelFactory) {
        ObjectUtil.checkNotNull(channelFactory, "channelFactory");
        if (this.channelFactory != null) {
            thr

相关推荐

  1. 学习netty(二)——BootStrap启动

    2024-05-03 04:34:03       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-03 04:34:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-03 04:34:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-03 04:34:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-03 04:34:03       18 阅读

热门阅读

  1. Redis 实现分布式Session 登录相关细节

    2024-05-03 04:34:03       9 阅读
  2. VSCode 配置 Qt 开发环境

    2024-05-03 04:34:03       11 阅读
  3. 模拟实现将字符串转化为整形

    2024-05-03 04:34:03       16 阅读
  4. Matlab基本语法

    2024-05-03 04:34:03       10 阅读
  5. js如何把json列表转成数组

    2024-05-03 04:34:03       12 阅读
  6. 第八周学习笔记DAY.5-实用类介绍

    2024-05-03 04:34:03       12 阅读
  7. [C++基础学习]----03-程序流程结构之跳转语句详解

    2024-05-03 04:34:03       12 阅读