Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

netty

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());

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

in MyHandler2

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

and the output:

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 the ctx is different but the channel is the same one

So it there any difference between invoke ctx.write() and ctx.channel().write() ?

like image 585
Alexis Avatar asked Dec 04 '13 03:12

Alexis


1 Answers

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.

like image 64
Norman Maurer Avatar answered Sep 27 '22 22:09

Norman Maurer