Any difference between ctx.write () and ctx.channel (). Write () in netty?

I noticed that ctx differs from a handler in a handler, even these handlers are in the same pipeline, for example

p.addLast("myHandler1", new MyHandler1()); p.addLast("myHandler2", new MyHandler2()); 

at 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); } 

in MyHandler2

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

and conclusion:

 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] 

I noticed that ctx is different, but the channel is the same

So, is there a difference between invoke ctx.write () and ctx.channel (). write ()?

+8
java netty
source share
1 answer

Yes, there is ... Channel.write (..) always starts at the tail of the ChannelPipeline and thus goes through all ChannelOutboundHandlers. ChannelHandlerContext.write (...) starts at the current ChannelHandler position, which is tied to the ChannelHandlerContext and therefore only passes the ChannelOutboundHandlers in front of it.

+22
source share

All Articles