在netty中ctx.write()和ctx.channel().write()之间有什么区别?

Ale*_*xis 9 java netty

我注意到ctx与处理程序不同,即使这些处理程序位于同一个管道中,例如

    p.addLast("myHandler1", new MyHandler1());
    p.addLast("myHandler2", new MyHandler2());
Run Code Online (Sandbox Code Playgroud)

在MyHander1

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.err.println("My 1 ctx: " + ctx + " channel: " + ctx.channel());
    super.channelRead(ctx, msg);
}
Run Code Online (Sandbox Code Playgroud)

在MyHandler2中

@Override
protected void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.err.println("My 2 ctx: " + ctx + " channel: " + ctx.channel());
}
Run Code Online (Sandbox Code Playgroud)

和输出:

My 1 ctx: io.netty.channel.DefaultChannelHandlerContext@ba9340 channel: [id: 0xdfad3a16, /127.0.0.1:60887 => /127.0.0.1:8090] 
My 2 ctx: io.netty.channel.DefaultChannelHandlerContext@1551d7f channel: [id: 0xdfad3a16, /127.0.0.1:60887 => /127.0.0.1:8090]
Run Code Online (Sandbox Code Playgroud)

我注意到ctx是不同的,但通道是相同的

那么调用ctx.write()和ctx.channel().write()之间有什么区别吗?

Nor*_*rer 27

是的... Channel.write(..)总是从ChannelPipeline的尾部开始,因此传递所有的ChannelOutboundHandlers.ChannelHandlerContext.write(...)从ChannelHandler的当前位置开始,该位置绑定到ChannelHandlerContext,因此只传递位于它前面的那些ChannelOutboundHandler.