zoukankan      html  css  js  c++  java
  • Netty 的 inbound 与 outbound, 以及 InboundHandler 的 channelInactive 与 OutboundHandler 的 close

    先看一个例子.

    有一个简单 Server

    public class SimpleServer {
    
        public static void main(String[] args) throws Exception {
    
            EventLoopGroup bossGroup = new NioEventLoopGroup(1);
            EventLoopGroup workerGroup = new NioEventLoopGroup();
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_REUSEADDR, true)
                    .childOption(ChannelOption.SO_SNDBUF, 1024 * 1024)
                    .childHandler(new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        protected void initChannel(NioSocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new SimpleDuplex1());
                            ch.pipeline().addLast(new SimpleDuplex2());
                            ch.pipeline().addLast(new SimpleServerHandler());
                        }
                    });
            b.bind(8090).sync().channel().closeFuture().sync();
        }
    }

    Handler 详情如下

    public class SimpleDuplex1 extends ChannelDuplexHandler {
    
        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("---- write 1 ----");
            super.write(ctx, msg, promise);
        }
    
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("---- read 1 ----");
            super.channelRead(ctx, msg);
        }
    }
    
    public class SimpleDuplex2 extends ChannelDuplexHandler {
    
        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("---- write 2 ----");
            super.write(ctx, msg, promise);
        }
    
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            System.out.println("---- read 2 ----");
            super.channelRead(ctx, msg);
        }
    }
    public class SimpleServerHandler extends ChannelDuplexHandler {
    
        @Override
        public void channelRead(ChannelHandlerContext ctx, final Object msg) throws Exception {
            ctx.channel().writeAndFlush(ByteBufAllocator.DEFAULT.buffer().writeBytes("OK".getBytes())).addListener(ChannelFutureListener.CLOSE);
        }
    
        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            System.out.println("----- INACTIVE -----");
            super.channelInactive(ctx);
        }
    
        @Override
        public void close(ChannelHandlerContext ctx, ChannelPromise future) throws Exception {
            System.out.println("----- CLOSE -----");
            super.close(ctx, future);
        }
    }

    启动 Server 以后, 使用 telnet 发送数据查看执行结果

    ---- read 1 ----
    ---- read 2 ----
    成功
    ---- write 2 ----
    ---- write 1 ----
    ----- CLOSE -----
    ----- INACTIVE -----

    1. 先来看看执行顺序, 可见, inbound 的顺序是跟 add 顺序一致的, 而 outbound 的顺序是跟 add 顺序相反的

    以及, read 的 IO 触发顺序是 "socketChannel.read() -> 顺序 handler -> TailContext.channelRead().releaseMsg"

    而 write 的 IO 触发顺序是 "逆序 handler -> HeadContext.socketChannel.write()"

    也就是说 read 是先触发 socket 的 read IO 时间, 再进入 handler, 而如果我们最后一个 handler 未能完全处理消息, 调用了 super.channelRead, 则会进入 TailContext. 此时TailContext 会打出 debug 消息告诉你消息进入了最后一个 Handler 而未被处理. 因为一般来讲都应该在自己的 handler 里把消息处理掉. 而不是让他进入到默认 handler 里.

    而对于 write 来说, 则是先进入自定义 handler, 最后在进入 HeadContext 触发 IO 时间

    2. 再来说说 close 与 channelInactive

    前面说到了. Outbound 的顺序是最后才执行到 HeadContext 来执行实际的 IO 操作, close 也是一样, 当你调用 channle.close 的时候, 先会经过你的 handler . 最后调用 HeadContext.socketChannel.close(). 所以, 在我们的 Handler 中, 先会打印 "---- CLOSE ----" 然后再调用实际的 socketChannel.close. 最后, 当 close 成功时, 触发 ChannelInactive 时间.

    所以说 close 与 channelInactive 的关系是 close 是主动关闭 channel 的动作, 而 channelInactive 是关闭成功后收到通知的事件.

  • 相关阅读:
    UART中的硬件流控RTS与CTS
    Yii smsto短信接口的函数
    分布式选主 利用Mysql ACID和Lease协议实现选主和高可用
    Extjs GridPanel 合计功能 解决滚动条滚动问题和页面刷新滚动条回到初始位置问题。
    基于ARM+LINUX的无线视频采集系统设计搭建嵌入式web服务器
    C++里父类的析构函数为什么声明为virtual
    重量过载 = 重载
    Spread Studio中文支持图解
    Android用户如何FQ访问被封锁的媒体资源
    将myeclipse 10.x以下版本web project的导入到myeclipse blue 2013 部署没有项目名
  • 原文地址:https://www.cnblogs.com/zemliu/p/4423786.html
Copyright © 2011-2022 走看看