如何解决netty中的ctx.write和ctx.channelwrite有什么区别?
是的,有… Channel.write(..)始终从ChannelPipeline的尾部开始,因此通过所有ChannelOutboundHandlers。ChannelHandlerContext.write(…)从绑定到ChannelHandlerContext的ChannelHandler的当前位置开始,因此仅传递在其前面的ChannelOutboundHandlers。
解决方法
我注意到ctx在处理程序之间是不同的,即使这些处理程序位于同一管道中,例如
p.addLast("myHandler1",new MyHandler1());
p.addLast("myHandler2",new MyHandler2());
在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中
@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不同,但频道相同
所以在调用ctx.write()和ctx.channel()。write()之间有什么区别吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。