Netty源码分析系列--3. 服务器启动ServerBootStrap分析

1. ServerBootstrap的使用:

通过链式的方式逐个调用groupchannelhandlerchildHandler方法,本质上是对需要的变量进行赋值

    ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .handler(new LoggingHandler(LogLevel.INFO))
                        .childHandler(new MyServerInitializer());

ServerBootStrap继承了抽象类AbstractBootstrap,作用是启动ServerChannel。一些重要的变量定义位于父类AbstractBootStrap中,而另一些位于子类ServerBootStrap中,下面会提到。

2. group方法

  public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
    super.group(parentGroup);
    if (childGroup == null) {
        throw new NullPointerException("childGroup");
    }
    if (this.childGroup != null) {
        throw new IllegalStateException("childGroup set already");
    }
    this.childGroup = childGroup;
    return this;
}
  • 第1行将参数 parentGroup 也就是bossGroup对象传入父类的group方法。
  • 倒数第2行将 childGroup 也就是 workerGroup赋值给当前类的成员变量childGroup
  • 父类AbstractBootStrap的group方法如下:可以看到是将bossGroup对象赋值给父类的成员变量group
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();
    }

    @SuppressWarnings("unchecked")
    private B self() {
        return (B) this;
    }
  • self()方法将AbstractBootStrap类型强制转换为ServerBootStrap类型。

2. channel方法

channel方法位于父类中,本质是new一个ReflectiveChannelFactory,并赋值给父类的成员变量channelFactory在需要的时候,使用持有的channelFactory变量来创建channel

public B channel(Class<? extends C> channelClass) {
        if (channelClass == null) {
            throw new NullPointerException("channelClass");
        }
        return channelFactory(new ReflectiveChannelFactory<C>(channelClass));
    }

    public B channelFactory(ChannelFactory<? extends C> channelFactory) {
        if (channelFactory == null) {
            throw new NullPointerException("channelFactory");
        }
        if (this.channelFactory != null) {
            throw new IllegalStateException("channelFactory set already");
        }

        this.channelFactory = channelFactory;
        return self();
    }

ReflectiveChannelFactory实现了ChannelFactory接口,它的newChannel方法调用传入的Class对象的构造函数创建实例:

public class ReflectiveChannelFactory<T extends Channel> implements ChannelFactory<T> {

private final Class<? extends T> clazz;

public ReflectiveChannelFactory(Class<? extends T> clazz) {
    if (clazz == null) {
        throw new NullPointerException("clazz");
    }
    this.clazz = clazz;
}

@Override
public T newChannel() {
    try {
        return clazz.getConstructor().newInstance();
    } catch (Throwable t) {
        throw new ChannelException("Unable to create Channel from class " + clazz, t);
    }
 }
}

3. childHandler方法

该方法比较简单,逻辑依然和上面的方法类似,对变量childHandler进行赋值:

    public ServerBootstrap childHandler(ChannelHandler childHandler) {
      if (childHandler == null) {
          throw new NullPointerException("childHandler");
      }
      this.childHandler = childHandler;
      return this;
     }

4. 以上方法小结

group, channel, childHandler方法对变量group, childGroup, channelFactory, childHandler变量进行了赋值。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容