源码

  1. //第二部分
  2. ServerBootstrap bootstrap = new ServerBootstrap();
  3. // 将parentGroup、childGroup初始化到bootstrap
  4. bootstrap.group(parentGroup, childGroup)
  5. // 将指定类型的channel的工厂类初始化到bootstrap
  6. .channel(NioServerSocketChannel.class)
  7. .attr(AttributeKey.valueOf("depart"), "行政部")
  8. .childAttr(AttributeKey.valueOf("addr"), "北京海淀")
  9. // 添加日志处理器
  10. .handler(new LoggingHandler(LogLevel.INFO))
  11. .childHandler(new ChannelInitializer<SocketChannel>() {
  12. @Override
  13. protected void initChannel(SocketChannel ch) throws Exception {
  14. ChannelPipeline pipeline = ch.pipeline();
  15. pipeline.addLast(new StringDecoder());
  16. }
  17. });

group

bootstrap.group(parentGroup, childGroup)
  • 两类

    • ServerBootStrap中的group就是childGroup

      1. public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
      2. super.group(parentGroup);
      3. if (childGroup == null) {
      4. throw new NullPointerException("childGroup");
      5. }
      6. if (this.childGroup != null) {
      7. throw new IllegalStateException("childGroup set already");
      8. }
      9. //childGroup
      10. this.childGroup = childGroup;
      11. return this;
      12. }
    • 它的父类AbstractBootStrap中的group就是parentGroup

      1. public B group(EventLoopGroup group) {
      2. if (group == null) {
      3. throw new NullPointerException("group");
      4. }
      5. if (this.group != null) {
      6. throw new IllegalStateException("group set already");
      7. }
      8. this.group = group;
      9. return self();
      10. }

      channel

      channel(NioServerSocketChannel.class)
  • 此方法返回了一个channel工厂,同时,内部封装了ReflectiveChannelFactory对象

    1. public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C extends Channel> implements Cloneable {
    2. public B channel(Class<? extends C> channelClass) {
    3. if (channelClass == null) {
    4. throw new NullPointerException("channelClass");
    5. }
    6. //返回一个channel工厂,channelClass就是NioServerSocketChannel
    7. return channelFactory(new ReflectiveChannelFactory<C>(channelClass));
    8. }
    9. }

    new ReflectiveChannelFactory(channelClass)
    1. public class ReflectiveChannelFactory<T extends Channel> implements ChannelFactory<T> {
    2. private final Constructor<? extends T> constructor;
    3. public ReflectiveChannelFactory(Class<? extends T> clazz) {
    4. ObjectUtil.checkNotNull(clazz, "clazz");
    5. try {
    6. // 初始化该构造器为 NioServerSockectChannel的无参构造器
    7. this.constructor = clazz.getConstructor();
    8. } catch (NoSuchMethodException e) {
    9. throw new IllegalArgumentException("Class " + StringUtil.simpleClassName(clazz) +
    10. " does not have a public non-arg constructor", e);
    11. }
    12. }
    13. }

    attr() 和 childAttr()属性
  • attr是在handler中可以获取到

  • childAttr是可以在childHandler中可以获取到使用
  • 为何呢???
    • Netty将具有关系的两个属性分别放到了AbstractBootStrap(不带child的存放地方),ServerBootstrap(带child前缀的),那么对应的是两个Group ,parentGroup可以获取AbstractBootStrap中的属性值,而childGroup可以获取到ServerBootstrap,而且group控制着eventLoop进而控制着调用链,即handler所以自然而言对应的handler可以获取到对应的属性(后面具体看handler的源码在来分析,此处为个人思考)
  • 不过attr和childAttr的存放位置源码

    attr
    1. public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerChannel> {
    2. private final Map<AttributeKey<?>, Object> childAttrs = new LinkedHashMap<AttributeKey<?>, Object>();
    3. public <T> ServerBootstrap childAttr(AttributeKey<T> childKey, T value) {
    4. if (childKey == null) {
    5. throw new NullPointerException("childKey");
    6. }
    7. if (value == null) {
    8. childAttrs.remove(childKey);
    9. } else {
    10. childAttrs.put(childKey, value);
    11. }
    12. return this;
    13. }
    14. }

    childAttr
    1. public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C extends Channel> implements Cloneable {
    2. //私有属性,子类 ServerBootstrap 不共享
    3. private final Map<AttributeKey<?>, Object> attrs = new LinkedHashMap<AttributeKey<?>, Object>();
    4. public <T> B attr(AttributeKey<T> key, T value) {
    5. if (key == null) {
    6. throw new NullPointerException("key");
    7. }
    8. if (value == null) {
    9. synchronized (attrs) {
    10. attrs.remove(key);
    11. }
    12. } else {
    13. synchronized (attrs) {
    14. attrs.put(key, value);
    15. }
    16. }
    17. return self();
    18. }
    19. }