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 {