注:本文基于Netty 5.x版本
ChannelHandlerAdapter 处于io.netty.channel包下
探究执行顺序
- 服务器启动,等待客户端连接;
- 客户端连接 发送一条消息;
- 此时服务器接收到消息,发送“Received your message !\n”表示已接收到;
- 客户端关闭链接
public class HelloServerHandler extends ChannelHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.err.println("channelActive");
ctx.writeAndFlush("Welcome to "
+ InetAddress.getLocalHost().getHostName() + " service!\n");
super.channelActive(ctx);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
System.err.println("channelRead");
// 返回客户端消息 - 我已经接收到了你的消息
ctx.writeAndFlush("Received your message !\n");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.err.println("channelInactive");
super.channelInactive(ctx);
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
System.err.println("handlerAdded");
super.handlerAdded(ctx);
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
System.err.println("handlerRemoved");
super.handlerRemoved(ctx);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
System.err.println("exceptionCaught");
super.exceptionCaught(ctx, cause);
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
System.err.println("channelRegistered");
super.channelRegistered(ctx);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.err.println("channelReadComplete");
super.channelReadComplete(ctx);
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
throws Exception {
System.err.println("userEventTriggered");
super.userEventTriggered(ctx, evt);
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx)
throws Exception {
System.err.println("channelWritabilityChanged");
super.channelWritabilityChanged(ctx);
}
@Override
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress,
ChannelPromise promise) throws Exception {
System.err.println("bind");
super.bind(ctx, localAddress, promise);
}
@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
SocketAddress localAddress, ChannelPromise promise)
throws Exception {
System.err.println("connect");
super.connect(ctx, remoteAddress, localAddress, promise);
}
@Override
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise)
throws Exception {
System.err.println("disconnect");
super.disconnect(ctx, promise);
}
@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise)
throws Exception {
System.err.println("close");
super.close(ctx, promise);
}
@Override
public void read(ChannelHandlerContext ctx) throws Exception {
System.err.println("read");
super.read(ctx);
}
@Override
public void write(ChannelHandlerContext ctx, Object msg,
ChannelPromise promise) throws Exception {
System.err.println("write");
super.write(ctx, msg, promise);
}
@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
System.err.println("flush");
super.flush(ctx);
}
}
服务器控制台打印结果:
handlerAdded
channelRegistered
channelActive
read
channelRead
channelReadComplete
read
channelReadComplete
read
exceptionCaught
channelInactive
handlerRemoved
To Be Continued