Netty入门P13

ChannelHandler的声明周期

示例客户端代码:

/**
 * @program: learnnetty
 * @description: 测试客户端
 * @create: 2020-05-11 15:22
 **/
public class Client {
    public static void main(String[] args) {
        NioEventLoopGroup worker = new NioEventLoopGroup();

        Bootstrap bootstrap = new Bootstrap();
        bootstrap
                .group(worker)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        nioSocketChannel
                                .pipeline()
                                .addLast(new LifeCycleTestHandler());
                    }
                });

        bootstrap.connect("127.0.0.1", 8080);
    }
}

示例处理器代码:

/**
 * @program: learnnetty
 * @description: 生命周期测试类
 * @create: 2020-05-11 15:15
 **/
public class LifeCycleTestHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Channel 绑定到线程组:channelRegistered");
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Channel 取消绑定到线程组:channelUnregistered");
        super.channelUnregistered(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Channel 准备就绪:channelActive");
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Channel 被关闭:channelInactive");
        super.channelInactive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("Channel 有可读数据:channelRead");
        super.channelRead(ctx, msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Channel 数据读取完毕:channelReadComplete");
        super.channelReadComplete(ctx);
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        System.out.println("逻辑处理器被添加:handlerAdded");
        super.handlerAdded(ctx);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        System.out.println("逻辑处理器被移除:handlerRemoved");
        super.handlerRemoved(ctx);
    }
}

无法连接时客户端输出:

逻辑处理器被添加:handlerAdded
Channel 绑定到线程组:channelRegistered
Channel 取消绑定到线程组:channelUnregistered
逻辑处理器被移除:handlerRemoved

正常连接且收到消息最后服务端断开客户端输出:

逻辑处理器被添加:handlerAdded
Channel 绑定到线程组:channelRegistered
Channel 准备就绪:channelActive
Channel 有可读数据:channelRead
Channel 数据读取完毕:channelReadComplete
Channel 数据读取完毕:channelReadComplete
Channel 被关闭:channelInactive
Channel 取消绑定到线程组:channelUnregistered
逻辑处理器被移除:handlerRemoved

可以得出ChannelHandler回调方法的顺序为:

  1. handlerAdded()
  2. channelRegistered()
  3. channelActive()
  4. channelRead()
  5. channelReadComplete()
  6. channelInactive()
  7. channelUnregistered()
  8. handlerRemoved()

对上述方法的解释:

方法 解释
handlerAdded() 在检测到新连接之后,调用NioSocketChannel.pipline().addLast()之后的回调,表示在当前Channel中成功添加了一个Handler;
channelRegistered() 表示当前的Channel的所有逻辑处理已经和某个NIO线程建立起了绑定关系,NIO线程一般是在线程组中;
channelActive() 表示当前Channel的所有的业务逻辑链已经准备完成以及与某一条NIO线程建立起绑定关系,即当前Channel已经准备就绪;
可以在此处设置连接IP地址的黑白名单;
可以在此处统计连接的单机数目;
channelRead() 表示当前连接收到可读的数据;
channelReadComplete() 表示已经读取完成一次数据;
可以在此处使用ChannelHandlerContext.channel().flush(),一次性将所有通过write()写出而不是通过writeAndFlush()写出的数据输出,这样可以提高效率;
channelInactive() 表示此链接已经被关闭,在TCP层面此链接已经不是ESTABLISH;
channelUnregistered() 表示与此链接绑定的NIO线程解绑;
handlerRemoved() 表示移除与此链接有关的所有处理器;

你可能感兴趣的:(学习笔记,netty,java)