注:黑色部分代碼與netty-action-01一致,請看netty-01部分
1、netty - channel 生命週期的描述
package com.renxiaobo.wechat.demo01;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.;
import io.netty.util.CharsetUtil;
/**
-
Author:rxb
-
Date:2020-06-10 20:44
-
Description:客戶端發送一個請求,服務端返回hello netty
*/
public class HelloServer {public static void main(String[] args) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(“HttpServerCodec”, new HttpServerCodec());//netty自己提供的 助手類,當請求到服務端,需要-解碼,響應到客戶-做編碼
pipeline.addLast(new ChildHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(8081).sync();
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}}
}
/***************************************************************
** 這是一個漂亮的分隔符
***************************************************************/
class ChildHandler extends SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
Channel channel = ctx.channel();
if(msg instanceof HttpRequest && !((HttpRequest) msg).uri().contains("/favicon.ico")){
System.out.println("當前客戶端地址:" + channel.remoteAddress());
ByteBuf content = Unpooled.copiedBuffer("哈哈大hello netty~~~", CharsetUtil.UTF_8);
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);
}
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
System.out.println(" channel註冊 -->");
super.channelRegistered(ctx);
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
System.out.println(" channel移除 -->");
super.channelUnregistered(ctx);
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(" channel活躍 -->");
super.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println(" channel不活躍(客戶端與服務器斷開連接) -->");
super.channelInactive(ctx);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println(" channel數據讀取完畢 -->");
super.channelReadComplete(ctx);
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
System.out.println(" 用戶事件觸發 -->");
super.userEventTriggered(ctx, evt);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
System.out.println(" channel可寫事件更改 -->");
super.channelWritabilityChanged(ctx);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
System.out.println(" 異常信息(關閉資源) -->");
super.exceptionCaught(ctx, cause);
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
System.out.println(" 助手類添加 -->");
super.handlerAdded(ctx);
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
System.out.println(" 助手類移除 -->");
super.handlerRemoved(ctx);
}
}
2、netty - channel 生命週期的控制檯結果展示(這邊直接doc命令curl localhost:8081訪問得到)
助手類添加 -->
channel註冊 -->
channel活躍 -->
當前客戶端地址:/0:0:0:0:0:0:0:1:58512
channel數據讀取完畢 -->
channel數據讀取完畢 -->
channel不活躍(客戶端與服務器斷開連接) -->
channel移除 -->
助手類移除 -->