问题描述
我注意到ctx与处理程序不同,即使这些处理程序在同一个管道中,例如
I noticed that the ctx is different from handler to handler even these handlers are in the same pipeline, for example
p.addLast("myHandler1", new MyHandler1());
p.addLast("myHandler2", new MyHandler2());
MyHander1中的
$ b
in 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);
}
MyHandler2中的
in MyHandler2
@Override
protected void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.err.println("My 2 ctx: " + ctx + " channel: " + ctx.channel());
}
和输出:
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]
我注意到ctx不同但通道是相同的
I noticed that the ctx is different but the channel is the same one
所以调用ctx.write()和ctx.channel()之间有什么区别.write()?
So it there any difference between invoke ctx.write() and ctx.channel().write() ?
推荐答案
是的... Channel.write(..)总是从ChannelPipeline的尾部开始,因此通过所有的ChannelOutboundHandlers。 ChannelHandlerContext.write(...)从ChannelHandler的当前位置开始,该位置绑定到ChannelHandlerContext,因此只传递它前面的那些ChannelOutboundHandler。
Yes there is... Channel.write(..) always start from the tail of the ChannelPipeline and so pass through all the ChannelOutboundHandlers. ChannelHandlerContext.write(...) starts from the current position of the ChannelHandler which is bound to the ChannelHandlerContext and so only pass those ChannelOutboundHandlers that are in front of it.
这篇关于在netty中ctx.write()和ctx.channel()。write()之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!