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

相關文章
相關標籤/搜索