netty初探ServerBootstrap主要类和起动流程

菜鸡学习netty源码(一)——ServerBootStrap启动 我们先进行来看AbstractBootStrap这个类。 阅读详情

几个关键类:

1.NioServerSocketChannel

 

2.NioSocketChannel

两个继承和实现的接口都差不多,功能也差不多

3.ServerBootstrap 和 Bootstrap都继承自AbstractBootstrap功能都是作为入口,构建服务端和客户端

4.NioEventLoopGroup

5.NioEventLoop

6.DefaultChannelPipeline

 

二.初始化流程

创建一个服务端代码

EventLoopGroup bossgroup = new NioEventLoopGroup();
EventLoopGroup workgroup = new NioEventLoopGroup();
try{
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossgroup,workgroup)
     .channel(NioServerSocketChannel.class)
     .option(ChannelOption.SO_BACKLOG,100)
     .handler(new LoggingHandler(LogLevel.INFO))
     .childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel socketChannel) throws Exception {
            socketChannel.pipeline().addLast(new ProtobufVarint32FrameDecoder());
            socketChannel.pipeline().addLast(new                                 
                   ProtobufDecoder(SubscribeReqProto.SubscribeReq.getDefaultInstance()));

            socketChannel.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
            socketChannel.pipeline().addLast(new ProtobufEncoder());
            socketChannel.pipeline().addLast(new SubReqHandler());

        }
     });

    ChannelFuture f = b.bind(port).sync();
    f.channel().closeFuture().sync();
}finally {
    bossgroup.shutdownGracefully();
    workgroup.shutdownGracefully();
}

group()、channel()、option() 、handler() 、childHandler()都是配置属性信息

整个创建初始化注册是在bind()方法内


public ChannelFuture bind(int inetPort) {
    return bind(new InetSocketAddress(inetPort));
}

public ChannelFuture bind(SocketAddress localAddress) {
    validate();
    if (localAddress == null) {
        throw new NullPointerException("localAddress");
    }
    return doBind(localAddress);
}

private ChannelFuture doBind(final SocketAddress localAddress) {

    final ChannelFuture regFuture = initAndRegister();
    final Channel channel = regFuture.channel();

    if (regFuture.cause() != null) {
        return regFuture;
    }

    if (regFuture.isDone()) {
        // At this point we know that the registration was complete and successful.
        ChannelPromise promise = channel.newPromise();
        doBind0(regFuture, channel, localAddress, promise);
        return promise;
    } else {
        // Registration future is almost always fulfilled already, but just in case it's not.
        final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel);
        regFuture.addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                Throwable cause = future.cause();
                if (cause != null) {
                    // Registration on the EventLoop failed so fail the ChannelPromise directly to not cause an
                    // IllegalStateException once we try to access the EventLoop of the Channel.
                    promise.setFailure(cause);
                } else {
                    // Registration was successful, so set the correct executor to use.
                    // See https://github.com/netty/netty/issues/2586
                    promise.registered();
                    doBind0(regFuture, channel, localAddress, promise);
                }
            }
        });
        return promise;
    }
}

再看一下initAndRegister() 主要是创建server channel和注册selector

final ChannelFuture initAndRegister() {

    Channel channel = null;

    try {
        //创建channel对象
        channel = channelFactory.newChannel();
        //初始化配置
        init(channel);
    } 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 GlobalEventExecutor
        return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE).setFailure(t);
    }

    //
别再搞混了!MATLAB通信仿真中EbN0转SNR的保姆级公式推导与避坑指南 本文深入解析MATLAB通信仿真中EbN0与SNR转换的核心公式,揭示数字仿真与理论系统的关键差异。通过真实案例展示错误参数设置导致的隐蔽问题,并提供完整的参数自查清单和正确实现代码,帮助工程师和学生避免常见错误,确保仿真结果准确可靠。 阅读详情

相关推荐

OpenCV2.4.9+vs2012配置

下载OpenCV2.4.9。 下载地址:http://opencv.org/downloads.html2.下载后,解压。 请记住解压路径,后面根据自己的解压路径进行配置。我的路径是E:\ 3.配置环境变量 win7系统:(计算机-属性-高级系统设置-高级-环境变量) 1)编辑系统变量Path:添加E:\opencv\build\x86\vc11\bin; 2)添加/编辑用户变量P

sybil_w的博客 975

使用-flat.vmdk文件恢复ESXI虚拟机的vmdk文件

解决flat.vmdk文件丢失恢复问题

神秘人的博客 6780

拓竹P1S封神玩法|AI图生3D+精准修改全攻略,打孔/挖盖再也不用瞎琢磨!

AI图生3D建模结合后期修改,让拓竹P1S用户轻松实现创意打印。文章详细介绍了从照片拍摄、AI模型生成到后期修改的全流程:使用腾讯混元3D或Tripo3D快速生成3D模型,通过Blender或Fusion360进行精准打孔、挖盖子等修改,最后在OrcaSlicer中优化切片参数适配P1S打印。重点解决了AI模型无法直接打印的问题,提供了壁厚控制、孔径调整等实用技巧,并针对常见打印问题给出解决方案,帮助新手实现从AI生成到成品打印的无缝衔接。

2501_93651177的博客 1951

SFT看这一篇就够了!万字长文:监督微调(Supervised Fine-Tuning, SFT)技术全解析【从理论到实战】

大家好,我是大 F,深耕AI算法十余年,互联网大厂技术岗。分享AI算法干货、技术心得。欢迎关注,一起探索技术的无限可能!

大F子的智能小课 721

双馈风机分段控制策略的一次调频模型深度解析

双馈风机分段调频控制策略研究 摘要:针对高比例风电并网导致的系统频率稳定问题,本研究提出一种基于风速分区的双馈风机一次调频控制策略。该策略根据风速区间(低、中、高)特性差异,分别采用:1)低风速区以转子动能控制为主;2)中速区结合超速备用与动能释放;3)高速区引入桨距角协调控制。通过构建包含空气动力学、轴系动态的完整数学模型,验证了分段控制在调频能力(最大提升40%功率支撑)、转速安全范围(控制在0.7-1.2pu)和动态响应速度(200ms内快速响应)等方面的优势。

huanghm88的专栏 351

如何让C++程序生成dump文件?生成dump文件的方式有哪些?如何使用Windbg分析dump文件?

本文详细介绍了如何让C++程序生成dump文件、生成dump文件的多种方式以及如何使用Windbg分析dump文件,并给出了对应的实战分析案例,有一定的实战参考价值。

dvlinker的技术专栏 5万+

OMAPL138使用UPP读写测试

UPP测试代码 #include "hw_types.h" // 宏命令 #include "hw_syscfg0_OMAPL138.h" // 系统配置模块寄存器 #include "soc_OMAPL138.h" // DSP C6748 外设寄存器 #include "psc.h" // 电源与睡眠控制宏及设备抽象层函数声明 #include "interrupt.h" // DSP C6748 中断相关应用程序接...

wangjie36的博客 1205

【MATLAB-app】csdn上第一套关于matlab appdesigner界面设计 系列“视频课”来啦~~

写作时间: 2020-12-10 标题: 01_为什么推荐你学习appdesigner? 目录: 1.什么是appdesigner? 2.appdesigner能干什么,有什么好处? 3.怎么学习?保证你一周内能够学会! 4.我的课程总目录 5.重磅来袭(如何获取学习资料代码+文档+视频) 正文: 1.什么是appdesigner? 2.appdesigner能干什么,有什么好处? 3.怎么学习?保证你一周内能够学会! 4.我的课程总目录 5.重磅来袭(如何获取学习资料代码+文档+视频) THE END

wind 2万+

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

下一篇: openstck执行subprocess出现'pid not in children'的问题
Azure_kk
博客等级 码龄13年 6粉丝 17原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值