Netty handle方法周期 (四)

写了一个练习以后,发现自定义的助手类每次确定是必须的,对于不一样的业务逻辑需求,会写相对应的逻辑java

最简单的查看Handle生命周期的方式,就是重写上级方法,看名字差很少应该能够知道方法的做用服务器

 

 

练习中的类是这个ide

public class HelloHandle extends SimpleChannelInboundHandler<HttpObject>

SimpleChannelInboundHandler 又继承了 ChannelInboundHandlerAdapter学习

因此重写底层的方法就能够,还有两个是add和remove看着,也重写一个.net

接着就能够使用print大法,就能够知道周期点以及其大体的设计做用设计

 

package com.yus.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

/**
 * 自定义的助手类
 * <p>
 * 客户端向服务端发起请求以后,数据存放在缓冲区
 * 而后服务端从缓冲区中读取,总体操做是一个入栈
 * <p>
 * SimpleChannelInboundHandler 入栈
 * 要往客户端写点东西返回,使用HttpObject
 */
public class HelloHandle extends SimpleChannelInboundHandler<HttpObject> {
    private static Integer INDEX=0;

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        ++INDEX;
        System.out.println("channel 注册方法 : " + INDEX);
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        ++INDEX;
        System.out.println("channel 移除方法 : " + INDEX);
        super.channelUnregistered(ctx);
    }

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

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        ++INDEX;
        System.out.println("channel 服务端和客户端断开,不活跃方法 : " + INDEX);
        super.channelInactive(ctx);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ++INDEX;
        System.out.println("channel 读取完触发方法 : " + INDEX);
        super.channelReadComplete(ctx);
    }

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

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        ++INDEX;
        System.out.println("channel 可写或改动 方法 : " + INDEX);
        super.channelWritabilityChanged(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ++INDEX;
        System.out.println("异常处理方法 : " + INDEX);
        super.exceptionCaught(ctx, cause);
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        ++INDEX;
        System.out.println("助手处理方法添加 : " + INDEX);
        super.handlerAdded(ctx);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        ++INDEX;
        System.out.println("助手处理方法移除 : " + INDEX);
        super.handlerRemoved(ctx);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
        Channel channel = ctx.channel();

        System.out.println("远程地址:" + channel.remoteAddress());

        //操做 缓冲区 参数(返回自定义字符串,字符集) 此处设置的字符集仅供ByteBuf使用
        ByteBuf buf = Unpooled.copiedBuffer("Hello,Netty,会乱码吗?!", CharsetUtil.UTF_8);

        //构建响应,将buf数据返回 参数(http版本号,http返回状态,)
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);

        //设置响应头部的数据类型以及长度,返回须要设置charset=UTF-8,针对response设置
        response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain;charset=UTF-8");
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH,buf.readableBytes());

        //把response响应到客户端
        //write只将response写到缓冲区,writeAndFlush将response写到缓冲区,并刷到客户端
        ctx.writeAndFlush(response);
    }
}

 

在控制台能够观察输出的序号以及猜想方法的功能代理

channelRead0方法中能够作Http判断逻辑,那么就能够作一个服务器的代理路由的效果netty

源码中的注释也还算比较友好的,比较容易看懂,继续学习code

---------------------------------------------------blog

相关文章
相关标签/搜索