【发布时间】:2023-03-09 19:45:02
【问题描述】:
Netty 的流水线(即ctx.foo() vs ctx.channel.foo())之前在 StackOverflow 上已经解释过两次:
- Any difference between ctx.write() and ctx.channel().write() in netty?
- In Netty 4, what's the difference between ctx.close and ctx.channel.close?
但是,我不明白 Netty 的示例背后的直觉是什么时候使用不同的方法:
public void channelActive(ChannelHandlerContext ctx) {
ctx.read(); // <----- HERE
}
public void channelRead(final ChannelHandlerContext ctx, Object msg) {
inboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) {
if (future.isSuccess()) {
ctx.channel().read(); // <----- HERE
} else {
future.channel().close();
}
}
});
}
为什么在代理impl的channelActive处理程序中使用'从这个处理程序向下'样式read,而在channelRead中使用'从顶部'样式read?
【问题讨论】: