我注意到即使这些处理程序在同一个管道中,ctx在不同的处理程序之间是不同的,例如:
p.addLast("myHandler1", new MyHandler1());
p.addLast("myHandler2", new MyHandler2());
在 MyHandler1 中
@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不同,但通道是相同的
那么在使用invoke ctx.write()和ctx.channel().write()之间有什么区别吗?