Netty 5.0源码分析-Bootstrap

1. 前言

io.netty.bootstrap类包提供包含丰富API的帮助类,能够非常方便的实现典型的服务器端和客户端通道初始化功能。

包含的接口类:

//提供工厂类的newChannel方法创建一个新的Channel。触发事件为:
// -Bootstrap.bind()
// -Bootstrap.connect()
// -ServerBootstrap.bind()
ChannelFactory<T extends Channel>
//创建一个新Channel的工厂类。触发事件为:
// -Bootstrap.bind()
// -Bootstrap.connect()
// -ServerBootstrap.bind()
ServerChannelFactory<T extends ServerChannel>

包含的类为:

//提供简易方式启动一个Channel的帮助类。
//通过支持方法链(method-chaining)使AbstractBootstrap的配置更加简单。
AbstractBootstrap<B extends AbstractBootstrap<B, C>, C extends Channel>
//为客户端提供简易的方法去启动一个Channel
Bootstrap
//为服务器端提供简易的方法去启动一个ServerChannel。
ServerBootstrap

2. 接口的实现细节

2.1 ChannelFactory

声明

ChannelFactory仅包含一个方法:T newChannel(eventLoop)。其中,

T 表示创建的Channel类型;
eventLoop 表示注册在该channel上的处理所有I/O操作的事件循环模型。

实现

ChannelFactory的实现类是BootstrapChannelFactory,它是一个不可变的final类,实现了接口的newChannel方法,利用反射创建Channel实例。源码如下:

@Override
public T newChannel(EventLoop eventLoop) {
  try {
     Constructor<? extends T> constructor = clazz.getConstructor(EventLoop.class);
     return constructor.newInstance(eventLoop);
  } catch (Throwable t) {
     throw new ChannelException("Unable to create Channel from class " + clazz, t);
    }
  }

2.2 ServerChannelFactory

声明

ServerChannelFactory仅包含一个方法:T newChannel(eventLoop, childGroup),其中,

T 表示创建的ServerChannel类型;
EventLoop 表示处理所有网络连接操作的父事件循环模型;
EventLoopGroup 表示处理所有网络I/O操作的子时间循环模型。

实现

ServerChannelFactory的实现类是ServerBootstrapChannelFactory,它是一个不可变的final类,实现了接口的newChannel方法,利用反射创建ServerChannel实例。源码如下:

@Override
  public T newChannel(EventLoop eventLoop, EventLoopGroup childGroup) {
    try {
        Constructor<? extends T> constructor = clazz.getConstructor(EventLoop.class, EventLoopGroup.class);
        return constructor.newInstance(eventLoop, childGroup);
    } catch (Throwable t) {
        throw new ChannelException("Unable to create Channel from class " + clazz, t);
      }
    }

3. 类的实现细节

3.1 AbstractBootstrap

AbstractBootstrap类的基本属性如下表所示:

属性含义
group特殊的事件处理执行器,在其上注册Channel处理异步任务的事件循环
handlerchannel上的I/O事件处理器,事件能够在其中的ChannelPipeline中流动
localAddress网络套接字地址
optionschannel通道的参数配置
attrsbootstrap的属性配置

AbstractBootstrap提供了BootstrapServerBootstrap的公共的抽象方法实现。

基本的方法列表

public B group(EventLoopGroup group)   //为group属性赋值
public B handler(ChannelHandler handler)  //为handler属性赋值
public B localAddress(SocketAddress localAddress)  //为localAddress属性赋值,另外提供了其他几种localAddress赋值方法,满足不同的实现需求。
public <T> B option(ChannelOption<T> option, T value)  //为option属性赋值
public <T> B attr(AttributeKey<T> key, T value)  //为attr属性赋值

初始化和注册方法initAndRegister

AbstractBootstrap提供的最重要的方法就是initAndRegister。该方法的处理流程如下:
1,创建channel通道,动态调用Bootstrap还是ServerBootstrapcreateChannel方法通过channelFactory工厂模式生成Channel对象;
2,根据bootstrap是Bootstrap还是ServerBootstrap来调用init(channel),主要实现将bootstrap的事件处理器handler添加到channel的事件处理链pipeline、将bootstrap的options和attrs属性配置到channel的ChannelConfig属性和attrs属性;
3,生成ChannelPromise对象regFuture,将其注册到channel上,用来获取事件处理完成后的返回结果(异步阻塞方式);
4,返回处理结果值ChannelFuture。

源代码实现如下:

final ChannelFuture initAndRegister() {
    Channel channel;
    try {
        channel = createChannel();
    } catch (Throwable t) {
        return VoidChannel.INSTANCE.newFailedFuture(t);
    }

    try {
        init(channel);
    } catch (Throwable t) {
        channel.unsafe().closeForcibly();
        return channel.newFailedFuture(t);
    }

    ChannelPromise regFuture = channel.newPromise();
    channel.unsafe().register(regFuture);
    if (regFuture.cause() != null) {
        if (channel.isRegistered()) {
            channel.close();
        } else {
            channel.unsafe().closeForcibly();
        }
    }
    return regFuture;
}

服务器端的bind方法

主要利用initAndRegister中配置的channel、regFuture和localAddress属性值启动channel事件处理循环模型eventLoop的执行器Executor,用来绑定地址,异步阻塞接受事件请求,利用ChannelFutureListener监听事件是否执行完成,返回事件的成功或失败的结果。

源代码实现如下:

private static void doBind0(
   final ChannelFuture regFuture, final Channel channel,
   final SocketAddress localAddress, final ChannelPromise promise) {

   // This method is invoked before channelRegistered() is triggered.  Give user handlers a chance to set up
   // the pipeline in its channelRegistered() implementation.
   channel.eventLoop().execute(new Runnable() {
       @Override
       public void run() {
           if (regFuture.isSuccess()) {
               channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
           } else {
               promise.setFailure(regFuture.cause());
           }
       }
   });
}

3.2 Bootstrap

Bootstrap继承自AbstractBootstrap,其类的形式如下所示:

public final class Bootstrap extends AbstractBootstrap<Bootstrap, Channel>

这样,除了拥有AbstractBootstrap的属性值和方法外,它还包含两个属性值:channelFactoryremoteAddress

channel方法

Bootstrap最重要的目的在于为客户端程序启动一个Channel,为各种异步事件提供处理机制。Channel方法采用工厂模式来生成新的Channel对象。主要流程如下:
1,调用Bootstrap.channel(chanelClass),在其中调用channelFactory(channelFactory);
2,channelFactory的参数调用BootstrapChannelFactory的构造函数生成ChannelFactory对象;
3,记录下Channel对象的构造工厂对象channelFactory。

其实在Bootstrap创建的过程中,调用channel方法并没有立刻得到channel对象,而是得到channel的工厂模式对象channelFactory,真正创建channel对象是在调用Bootstrap.connect()方法时,触发initAndRegister(),然后执行createChannel()方法,最终利用工厂模式对象channelFactory生成Channel对象。

createChannel的源代码如下:

@Override
Channel createChannel() {
    EventLoop eventLoop = group().next();
    return channelFactory().newChannel(eventLoop);
}

newChannel调用BootstrapChannelFactory.newChannel来真正生成channel对象,其源代码如下:

@Override
public T newChannel(EventLoop eventLoop) {
    try {
        Constructor<? extends T> constructor = clazz.getConstructor(EventLoop.class);
        return constructor.newInstance(eventLoop);
    } catch (Throwable t) {
        throw new ChannelException("Unable to create Channel from class " + clazz, t);
    }
}

注:T代表继承自Channel的泛型。

3.3 ServerBootstrap

ServerBootstrap继承自AbstractBootstrap,其类的形式如下所示:

public final class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerChannel>

除了拥有AbstractBootstrap的属性值和方法外,为了支持主从工作模式,提供了子事件处理模型所需要的属性:childOptions、childAttrs、childGroup、childHandler。

为了创建channel对象,提供了工厂模式对象属性:channelFactory。

ServerBootstrap构造函数需要考虑主EventLoop和从EventLoop,在ServerBootstrap(bootstrap)的实现里,主从启动器共用相同的属性。不过,这种方式不常用。一般而言,我们会分别设置主EventLoop和从EventLoop的事件处理模型属性,主EventLoop主要接受各种请求,从EventLoop主要处理各种请求。

基本属性方法

ServerBootstrap的父Channel的属性设置通过继承AbstractBootstrap抽象类实现,子Channel的属性设置通过下标中提供的方法实现:

方法含义
childOption(ChannelOption childOption, T value)设置childOptions属性
childAttr(AttributeKey childKey, T value)设置childAttrs属性
group(EventLoopGroup parentGroup, EventLoopGroup childGroup)设置childGroup属性
childHandler(ChannelHandler childHandler)设置childHandler属性

group方法

设置主从Channel的事件处理模型EventExecutorGroup,用于处理所有的事件和I/O操作。源代码如下:

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;
}

可见,其分别设置主EventLoop和从EventLoop的事件处理模型。

channel方法

ServerBootstrap最重要的目的在于为服务器端程序启动一个Channel,为各种异步事件提供处理机制。Channel方法采用工厂模式来生成新的Channel对象。主要流程如下:
1,调用Bootstrap.channel(chanelClass),在其中调用channelFactory(channelFactory);
2,channelFactory的参数调用ServerBootstrapChannelFactory的构造函数生成ChannelFactory对象;
3,记录下ServerChannel对象的构造工厂对象channelFactory。

其实在ServerBootstrap创建的过程中,调用channel方法并没有立刻得到channel对象,而是得到ServerChannel的工厂模式对象channelFactory,真正创建channel对象是在调用ServerBootstrap.bind()方法时,触发initAndRegister(),然后执行createChannel()方法,最终利用工厂模式对象channelFactory生成Channel对象。

createChannel的源代码如下:

@Override
Channel createChannel() {
    EventLoop eventLoop = group().next();
    return channelFactory().newChannel(eventLoop, childGroup);
}

newChannel调用BootstrapChannelFactory.newChannel来真正生成channel对象,其源代码如下:

@Override
public T newChannel(EventLoop eventLoop, EventLoopGroup childGroup) {
    try {
        Constructor<? extends T> constructor = clazz.getConstructor(EventLoop.class, EventLoopGroup.class);
        return constructor.newInstance(eventLoop, childGroup);
    } catch (Throwable t) {
        throw new ChannelException("Unable to create Channel from class " + clazz, t);
    }
}

注:T代表继承自ServerChannel的泛型。


(END)

转载于:https://www.cnblogs.com/vectoryao/p/6273252.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值