Netty中Channel的生命周期(SimpleChannelInboundHandler)

本文详细解析了Netty中Channel的生命周期,包括从注册、激活到读取、写入及异常处理等关键阶段,深入探讨了每个阶段的具体事件及其处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自:https://blog.youkuaiyun.com/u014131617/article/details/86476522

 

类的说明

public class CustomHandler extends SimpleChannelInboundHandler<HttpObject>{}

 

1.channelRegistered

channel注册事件

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channel注册了");
        super.channelRegistered(ctx);
    }

2.channelUnregistered

channel取消注册事件

@Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channel移除了");
        super.channelUnregistered(ctx);
    }

3.channelActive

channel是否是活跃

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channel 活跃");
        super.channelActive(ctx);
    }

4.channelInactive

channel不活跃

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channel 不活跃,断开连接");
        super.channelInactive(ctx);
    }

5.channelReadComplete

channel读取完毕事件

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

6.userEventTriggered

channel 用户事件触发

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        System.out.println("channel 用户事件触发");
        super.userEventTriggered(ctx, evt);
    }

7.channelWritabilityChanged

channel 可写更改

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channel 可写更改");
        super.channelWritabilityChanged(ctx);
    }

8.exceptionCaught

channel 捕获到异常

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("channel 捕获到异常了,关闭了");
        super.exceptionCaught(ctx, cause);
    }

9.handlerAdded

channel 助手类(拦截器)的添加

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channel 助手类添加");
        super.handlerAdded(ctx);
    }

10.handlerRemoved

channel 助手类(拦截器)移除

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channel 助手类移除");
        super.handlerRemoved(ctx);
    }

11.channelRead0

channel读取数据

@Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg)
            throws Exception {
        Channel channel = ctx.channel();//获取当前channel
        System.out.println(channel.remoteAddress()); //显示客户端的远程地址
        //定义发送的数据消息
        ByteBuf content = Unpooled.copiedBuffer("Hello cxl",CharsetUtil.UTF_8);
        //构建一个http响应
        FullHttpResponse response =
                new DefaultFullHttpResponse
                (HttpVersion.HTTP_1_1,
                        HttpResponseStatus.OK,
                        content);
        
        response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH,content.readableBytes());
        
        //把响应刷到客户端
        ctx.writeAndFlush(response);
    }

12.执行顺序


---------------------
作者:小龙哒
来源:优快云
原文:https://blog.youkuaiyun.com/u014131617/article/details/86476522
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值