netty源码阅读之ServerBootStrap类

netty源码阅读之ServerBootStrap类

在netty中有两个引导器,一个是服务端的引导器,一个是客户端的引导器。本次主要看ServerBootstrap类的实例化在看它干啥事,先通过各个类的注释认识类下的方法成员与属性成员在干啥,然后debug进去了解运行的细节~~

调试代码如下
在这里插入图片描述

1. ServerBootStrap类

注释明了说明ServerBootStrap类用于简易地引导出Server Channel。说白了它的意义主要就是为Server Channel服务的。

/**
 * {
   @link Bootstrap} sub-class which allows easy bootstrap of {
   @link ServerChannel}
 *
 */
public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerChannel> {
   

1.1成员属性
带有child的都是与workerGroup相关的成员对象。每当有一个client新连接进来的时候就会利用这些属性给client连接进行初始化。

private final Map<ChannelOption<?>, Object> childOptions = new LinkedHashMap<ChannelOption<?>, Object>();
    private final Map<AttributeKey<?>, Object> childAttrs = new LinkedHashMap<AttributeKey<?>, Object>();
    private final ServerBootstrapConfig config = new ServerBootstrapConfig(this);
    private volatile EventLoopGroup childGroup;
    private volatile ChannelHandler childHandler;

1.2调用的构造器方法:
将bossgroup对象传到了父类的构造器下,应该是交给父类保存bossGroup对象或说与bossGroup相关的信息。

 /**
     * Set the {@link EventLoopGroup} for the parent (acceptor) and the child (client). These
     * {@link EventLoopGroup}'s are used to handle all the events and IO for {@link ServerChannel} and
     * {@link Channel}'s.
     */
    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.3 childOption方法
给新连接进来的客户端连接(chanel)绑定对应的Channel连接属性,注释写的明白,每一个新连接进来都会经过acceptor 接收

/**
     * Allow to specify a {@link ChannelOption} which is used for the {@link Channel} instances once they get created
     * (after the acceptor accepted the {@link Channel}). Use a value of {@code null} to remove a previous set
     * {@link ChannelOption}.
     */
    public <T> ServerBootstrap childOption(ChannelOption<T> childOption, T value) {
   
        if (childOption == null) {
   
            throw new NullPointerException("childOption");
        }
        if (value == null) {
   
            synchronized (childOptions) {
   
                childOptions.remove(childOption);
            }
        } else {
   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值