您的位置:首页 > Web前端 > BootStrap

Netty源码学习系列之2-ServerBootstrap的初始化

2020-06-01 19:55 801 查看

前言

    根据前文我们知道,NioEventLoopGroup和NioEventLoop是netty对Reactor线程模型的实现,而本文要说的ServerBootstrap是对上面二者的整合与调用,是一个统筹者和协调者。具体netty使用的是Reactor单线程模型还是多线程模型、抑或者主从多线程模型,都是ServerBootstrap的不同配置决定的。

    下面照例粘贴一下示例demo(以Reactor多线程模式构建),开始正文。

public class NettyDemo1 {
// netty服务端的一般性写法
public static void main(String[] args) {
EventLoopGroup boss = new NioEventLoopGroup(1);
EventLoopGroup worker = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boss, worker).channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
pipeline.addLast(new NettyServerHandler());
}
});
ChannelFuture channelFuture = bootstrap.bind(90);
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
}

 

一、ServerBootstrap的初始化

    ServerBootstrap的无参构造器啥都没做,它使用的build模式给属性赋值,即上面示例中看到的,每执行一个赋值方法都会返回当前对象的引用使得可以继续链式调用。下面挨个方法追踪。

public ServerBootstrap() { }

 

1、ServerBootstrap.group方法

    ServerBootstrap有两个可用重载group方法(如下的两个),其中接收一个group入参的方法会调用有两个入参的group方法,只是两个参数传同一个group。这两个group方法决定了netty使用的Reactor线程模型的类型,一个group入参的方法对应Reactor单线程模型,两个入参且不是同一个group的方法对应Reactor多线程模型或主从多线程模型(具体是哪一种取决于实例化parentGroup时的线程数)。此处只是提一下,先有个印象,后面会对线程模型进行详细研究。

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

    可以看到上述group方法对两个入参进行了不同位置的赋值,将第一个参数parentGroup传给了父类AbstractBootstrap的group方法,如下,即最终赋值给了AbstractBootstrap中的group属性。第二个参数直接赋值给了ServerBootstrap的childGroup属性。

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

 

2、ServerBootstrap.option/childOption方法和ServerBootstrap.attr/childAttr方法

    这四个方法只是做了属性的赋值,分别赋值给了AbstractBootstrap的options属性和attrs属性以及ServerBootstrap的childOptions属性和childAttrs属性。

public <T> B option(ChannelOption<T> option, T value) {
ObjectUtil.checkNotNull(option, "option");

56c
if (value == null) {
synchronized (options) {
options.remove(option);
}
} else {
synchronized (options) {
options.put(option, value);
}
}
return self();
}
public
ad0
<T> B attr(AttributeKey<T> key, T value) {
ObjectUtil.checkNotNull(key, "key");
if (value == null) {
synchronized (attrs) {
attrs.remove(key);
}
} else {
synchronized (attrs) {
attrs.put(key, value);
}
}
return self();
}

 

3、ServerBootstrap.channel方法

    调用的是父类AbstractBootstrap的channel方法:

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

    可以看到先封装成了一个ReflectiveChannelFactory对象,然后调用channelFactory方法,下面挨个看。ReflectiveChannelFactory的构造器如下,可见就是将传入class对象的构造器取出来赋值,此时constructor存放的就是NioServerSocketChannel的构造器。

public ReflectiveChannelFactory(Class<? extends T> clazz) {
ObjectUtil.checkNotNull(clazz, "clazz");
try {
this.constructor = clazz.getConstructor();
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("Class " + StringUtil.simpleClassName(clazz) +
" does not have a public non-arg constructor", e);
}
}

    channelFactory方法的工作是将上面创建的ReflectiveChannelFactory对象赋值给AbstractBootstrap的channelFactory属性:

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

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

 

4、ServerBootstrap.handler方法和ServerBootstrap.childHandler方法

    handler方法的入参赋值给了AbstractBootstrap的handler属性,childHandler方法的入参赋值给了ServerBootstrap的childHandler属性。看到这里想必园友们也能看出ServerBootstrap的赋值规律了,凡是child开头的都放在ServerBootstrap中,而不带child的大多放在其父类ABstractBootstrap中。

public B handler(ChannelHandler handler) {
this.handler = ObjectUtil.checkNotNull(handler, "handler");
return self();
}
public ServerBootstrap childHandler(ChannelHandler childHandler) {
this.childHandler = ObjectUtil.checkNotNull(childHandler, "childHandler");
return this;
}

 

5、完成赋值后ServerBootstrap的快照图

 

 

小结

    ServerBootstrap的初始化过程看起来赋了很多值,但都只是做了准备工作,看起来轻松又简单,但请注意,这是暴风雨前宁静。前面的各种赋值到底有什么用处?很多属性分为有child前缀和没有child前缀,这样设置又有什么意图?下一期将进入ServerBootstrap的bind方法,这是netty的深水区,很多谜底也将在这里得到揭晓,敬请期待!

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: