美文网首页
bootstrap-boss-worker关系

bootstrap-boss-worker关系

作者: xhrg | 来源:发表于2018-12-21 16:54 被阅读0次

对于启动类,有以下这种简单的方式:

ServerBootstrap serverBootstrap = new ServerBootstrap();
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();
        serverBootstrap
                .group(boss, worker)
                .channel(NioServerSocketChannel.class)
                .childHandler(new TestServerChannelInitializer())
                .bind(1234); 

比较诡异的有以下(伪代码):

//第一种
serverBootstrap.group(boss,worker).bind(1234);
serverBootstrap.bind(1235);
serverBootstrap.bind(1236);
//第二种
serverBootstrap.group(boss,boss);
//第三种
serverBootstrap1.group(boss,worker);
serverBootstrap2.group(boss,worker);
//这样的组合剩下的脑补

对于这样的代码,可以有很多种。其实NioEventLoopGroup对应于一个线程池,这个线程池里的NioEventLoop是单线程的,NioEventLoop中可以对应多个Channel,每个Channel对应于1个端口。而ServerBootstrap其实仅仅是把boss,worker,NioServerSocketChannel组装起来。组装起来后获取的是ServerBootstrap的引用,然后bind后才会获取到一个ChannelFuture。而对于boss和worker来说,这样的复用,也只是线程池这个概念被2个地方使用了而已。

//代码来自AbstractBootstrap
  final ChannelFuture initAndRegister() {
        Channel channel = null;
        try {
            channel = channelFactory.newChannel();
            init(channel);//这一步会把新建的channel放入worker中。
        } catch (Throwable t) {
            if (channel != null) {
                // channel can be null if newChannel crashed (eg SocketException("too many open files"))
                channel.unsafe().closeForcibly();
                // as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
                return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE).setFailure(t);
            }
            // as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
            return new DefaultChannelPromise(new FailedChannel(), GlobalEventExecutor.INSTANCE).setFailure(t);
        }
      //这一步会把channel放入boss中。
        ChannelFuture regFuture = config().group().register(channel);
        if (regFuture.cause() != null) {
            if (channel.isRegistered()) {
                channel.close();
            } else {
                channel.unsafe().closeForcibly();
            }
        }

        // If we are here and the promise is not failed, it's one of the following cases:
        // 1) If we attempted registration from the event loop, the registration has been completed at this point.
        //    i.e. It's safe to attempt bind() or connect() now because the channel has been registered.
        // 2) If we attempted registration from the other thread, the registration request has been successfully
        //    added to the event loop's task queue for later execution.
        //    i.e. It's safe to attempt bind() or connect() now:
        //         because bind() or connect() will be executed *after* the scheduled registration task is executed
        //         because register(), bind(), and connect() are all bound to the same thread.

        return regFuture;
    }

相关文章

  • bootstrap-boss-worker关系

    对于启动类,有以下这种简单的方式: 比较诡异的有以下(伪代码): 对于这样的代码,可以有很多种。其实NioEven...

  • 关系,关系,都是关系

    在一个故事里面,我觉得关系是最值得探讨的话题。人与人之间有那么多复杂的、微妙的、千奇百怪千回百转的关系。如果说一个...

  • 关系关系还是关系

    一直在强调关系。今天的咨询约练再次证明了关系的重要性。今天是承接上一次没有做完的咨询,当时在咨询的过程中,突发了一...

  • 关系关系还是关系

    怎么办

  • 关系,不关系

    自从上次通用网站平台失败后,我静下心来,好好寻思好的办法。考虑到公司的投资人背景,或许我们可以从“关系”那层出发。...

  • 关系、关系|读书

    和《重塑心灵》一起买的还有李中莹老师的另外两本书:《亲子关系全面技巧》和《亲密关系全面技巧》。后两本书可以说是重塑...

  • UML图的基础

    一、 基础关系 依赖关系依赖关系 实现关系实现关系 继承关系继承关系 组合关系组合关系 聚合关系聚合关系 关联关系...

  • 关系=有关系=发生关系

    前几天看到一篇很有寓意的故事和大家一起分享。 一个农场主在他的粮仓里放了老鼠夹子,老鼠发现了去告诉了母...

  • 关系=有关系=发生关系

    一个农场主在他的粮仓里放了老鼠夹子 老鼠发现了去告诉母鸡。 母鸡看了看老鼠说:“这和我有什么关系,你的事,自己小心...

  • 伪关系、假关系、真关系

    所有文章均为原创,我已委托“维权骑士”为我的文章进行维权行动,需要转载请加微信获取授权。 关系,存在于人和人之间,...

网友评论

      本文标题:bootstrap-boss-worker关系

      本文链接:https://www.haomeiwen.com/subject/anhvkqtx.html