bossGroup 接收完请求怎么推送到workGroup组的

本文深入解析了Netty框架中ServerBootstrapAcceptor的作用及其实现原理,详细阐述了其在channelPipeline中的注册过程与channelRead方法的执行流程,为理解Netty服务器端工作原理提供了关键洞见。

io.netty.bootstrap.ServerBootstrap.ServerBootstrapAcceptor

channelPipleline默认会有一个headContext    --》    ServerBootstrapAcceptor   --》   TailContext

ServerBootstrapAcceptor.channelRead 方法如下

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() {     //bossGroup接收完线程,这里注
                               //册到workGroup中,子Group中的NioEnvet集合通过chooser代理对象去轮询或者..接收注册
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    forceClose(child, future.cause());
                }
            }
        });
    } catch (Throwable t) {
        forceClose(child, t);
    }
}

上述方法中的msg 对象指的是 ServerSocketChannel accept出来的socketChannel

SocketChannel ch = SocketUtils.accept(javaChannel());

try {
    if (ch != null) {
        buf.add(new NioSocketChannel(this, ch));
        return 1;
    }
} catch (Throwable t) {

 

 

ServerBootstrapAcceptor  这个是什么时候注册到pipleLine的context双向队列中的

当创建ServerSocketChannel的时候,然后init方法,代码如下

void init(Channel channel) throws Exception {
    final Map<ChannelOption<?>, Object> options = options0();
    synchronized (options) {
        setChannelOptions(channel, options, logger);
    }

    final Map<AttributeKey<?>, Object> attrs = attrs0();
    synchronized (attrs) {
        for (Entry<AttributeKey<?>, Object> e: attrs.entrySet()) {
            @SuppressWarnings("unchecked")
            AttributeKey<Object> key = (AttributeKey<Object>) e.getKey();
            channel.attr(key).set(e.getValue());
        }
    }

    ChannelPipeline p = channel.pipeline();

    final EventLoopGroup currentChildGroup = childGroup;
    final ChannelHandler currentChildHandler = childHandler;
    final Entry<ChannelOption<?>, Object>[] currentChildOptions;
    final Entry<AttributeKey<?>, Object>[] currentChildAttrs;
    synchronized (childOptions) {
        currentChildOptions = childOptions.entrySet().toArray(newOptionArray(childOptions.size()));
    }
    synchronized (childAttrs) {
        currentChildAttrs = childAttrs.entrySet().toArray(newAttrArray(childAttrs.size()));
    }

    p.addLast(new ChannelInitializer<Channel>() {
        @Override
        public void initChannel(final Channel ch) throws Exception {
            final ChannelPipeline pipeline = ch.pipeline();
            ChannelHandler handler = config.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));
                }
            });
        }
    });
}

 

方法 initChannel什么时候调用,任务注册的时候,ChannelInitializer初始化调用

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值