源码
//第二部分ServerBootstrap bootstrap = new ServerBootstrap();// 将parentGroup、childGroup初始化到bootstrapbootstrap.group(parentGroup, childGroup)// 将指定类型的channel的工厂类初始化到bootstrap.channel(NioServerSocketChannel.class).attr(AttributeKey.valueOf("depart"), "行政部").childAttr(AttributeKey.valueOf("addr"), "北京海淀")// 添加日志处理器.handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline = ch.pipeline();pipeline.addLast(new StringDecoder());}});
group
bootstrap.group(parentGroup, childGroup)
两类
ServerBootStrap中的group就是childGroup
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");}//childGroupthis.childGroup = childGroup;return this;}
它的父类AbstractBootStrap中的group就是parentGroup
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();}
channel
channel(NioServerSocketChannel.class)
此方法返回了一个channel工厂,同时,内部封装了ReflectiveChannelFactory对象
public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C extends Channel> implements Cloneable {public B channel(Class<? extends C> channelClass) {if (channelClass == null) {throw new NullPointerException("channelClass");}//返回一个channel工厂,channelClass就是NioServerSocketChannelreturn channelFactory(new ReflectiveChannelFactory<C>(channelClass));}}
new ReflectiveChannelFactory
(channelClass) public class ReflectiveChannelFactory<T extends Channel> implements ChannelFactory<T> {private final Constructor<? extends T> constructor;public ReflectiveChannelFactory(Class<? extends T> clazz) {ObjectUtil.checkNotNull(clazz, "clazz");try {// 初始化该构造器为 NioServerSockectChannel的无参构造器this.constructor = clazz.getConstructor();} catch (NoSuchMethodException e) {throw new IllegalArgumentException("Class " + StringUtil.simpleClassName(clazz) +" does not have a public non-arg constructor", e);}}}
attr() 和 childAttr()属性
attr是在handler中可以获取到
- childAttr是可以在childHandler中可以获取到使用
- 为何呢???
- Netty将具有关系的两个属性分别放到了AbstractBootStrap(不带child的存放地方),ServerBootstrap(带child前缀的),那么对应的是两个Group ,parentGroup可以获取AbstractBootStrap中的属性值,而childGroup可以获取到ServerBootstrap,而且group控制着eventLoop进而控制着调用链,即handler所以自然而言对应的handler可以获取到对应的属性(后面具体看handler的源码在来分析,此处为个人思考)
-
attr
public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerChannel> {private final Map<AttributeKey<?>, Object> childAttrs = new LinkedHashMap<AttributeKey<?>, Object>();public <T> ServerBootstrap childAttr(AttributeKey<T> childKey, T value) {if (childKey == null) {throw new NullPointerException("childKey");}if (value == null) {childAttrs.remove(childKey);} else {childAttrs.put(childKey, value);}return this;}}
childAttr
public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C extends Channel> implements Cloneable {//私有属性,子类 ServerBootstrap 不共享private final Map<AttributeKey<?>, Object> attrs = new LinkedHashMap<AttributeKey<?>, Object>();public <T> B attr(AttributeKey<T> key, T value) {if (key == null) {throw new NullPointerException("key");}if (value == null) {synchronized (attrs) {attrs.remove(key);}} else {synchronized (attrs) {attrs.put(key, value);}}return self();}}
