netty-Channel-boss-worker-group

一、说说 netty 的命名习惯

1、xxGroup : 指的是一个组,类里很可能就有一个数组,管理一个数组的对象的生命周期;
例如:NioEventLoopGroup 是一个 基于NIO 的 处理事件的管理组,里面数组是 NioEventLoop 对象组,
   NioEventLoop 才是实际的处理 事件的执行者;
  ChannelPipeline 是特例,也是组的概念,方便统一管理,数组对象封装的是 AbstractChannelHandlerContext ,
  AbstractChannelContext 是 ChannelHandler的上下文环境,里面封装的是 ChannelHandler,
  ChannelHandler 是 Channel 的 事件处理助手,用户可以自由扩展实现

2、doXX : 才是 实际的去执行任务。
例如: Channel connect();    子类实现时, doConnect()方法才是实际去干活,也或许是 doConnect0(), xxHandler.connect() 句柄都是回调函数,作用有(1)通知用户自定义的 handler 和 框架默认的 handler  (2) 异步修改变量 

3、invokeXX 事件促发函数,基本上是 调用 xxHandler的方法。

二、bossGroup  与 workerGroup 
1、在 server 端,建立 2 层 处理事件组,bossGroup 是父级Group,  workerGroup 是 childGroup 
2、 new NioEventLoopGroup() 创建时做了什么?
   2.1 在  MultithreadEventExecutorGroup 抽象层, 构建 thread, 开始调子类实现的run()方法,既是 NioEventLoop实现的run() 
children = new SingleThreadEventExecutor[nThreads] ; 构建一个线程事件执行数组,
children [i] = newChild(threadFactory , args) ;
每一个child 具体实现类是 NioEventLoop,都有自己的 thread 、taskQueue 任务队列

2.1.1 NioEventLoop.run() 干了什么? 
死循环,时刻监听 Channel 的读写事件,应用了 NIO 的 selector、provider , 有事件通过 channel.unsafe() 处理事件
  
3、bossGroup 注册 Channel ,线程是 bossGroup线程组里的其中一个线程

3.1 在  MultithreadEventLoopGroup抽象层实现是 通过next()用选择器随机从事件执行者数组里选一个EventLoop来注册Channel
3.1.1 具体在  SingleThreadEventLoop
		channel.unsafe().register(this, promise); 传入了 EventLoop 
3.1.2 就能判断 当前线程的 eventLoop 是否 是跟 child[i]: EventLoop 同一个线程了

    3.2 doBind(SocketAddress ) 操作促发 
      initAndRegister()初始化和注册Channel,
      channel 需要 initChannel()有且只会执行第一次,初始化;
      --主要是 channel的pipeline 需要添加 ChannelHandler;
      --其中一个handler 是 ServerBootStrapAccrptor();传入了 workerGroup, 监听了Channel的读事件,childGroup 注册Channel =》childGroup线程组监听Channel的读写事件
p.addLast(new ChannelInitializer<Channel>() {
    @Override
    public void initChannel(final Channel ch) throws Exception {
        final ChannelPipeline pipeline = ch.pipeline();
        ChannelHandler handler = handler();
        if (handler != null) {
            pipeline.addLast(handler);
        }

        ch.eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                pipeline.addLast(new ServerBootstrapAcceptor(
                        ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
            }
        });
    }
});

public void channelRead(ChannelHandlerContext ctx, Object msg) {
    final Channel child = (Channel) msg;

    child.pipeline().addLast(childHandler);

    setChannelOptions(child, childOptions, logger);

    for (Entry<AttributeKey<?>, Object> e: childAttrs) {
        child.attr((AttributeKey<Object>) e.getKey()).set(e.getValue());
    }

    try {
        childGroup.register(child).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    forceClose(child, future.cause());
                }
            }
        });
    } catch (Throwable t) {
        forceClose(child, t);
    }
}


    3.3 在 doRegister()后, 
3.2.1 Channel的 pipeline 告诉自己的所有的handler 相关事件
3.2.2 事件结果封装的 promise告诉自己的监听者 相关事件
doRegister();
neverRegistered = false;
registered = true;

// Ensure we call handlerAdded(...) before we actually notify the promise. This is needed as the
// user may already fire events through the pipeline in the ChannelFutureListener.
pipeline.invokeHandlerAddedIfNeeded();

safeSetSuccess(promise);
pipeline.fireChannelRegistered();



4、group 与 excutor 类图设计

 


5、boss 线程组 怎么样和 worker 线程组 通信的?


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值