nettynetty4

What is the difference between operations on Channel vs operations on ChannelHandlerContext


I am just starting out with Netty and I have a question about Channel and ChannelHandlerContext

From what I have read so far, Channel can be seen as an abstraction over underlying socket where IO happens, while ChannelHandlerContext is an object used for passing information amongst channel handlers within a channel pipeline.

Is the above accurate?

Because now I am looking at a some code base that uses Netty and examples online, I find same operations that seems to be both on Channel and also on ChannelHandlerContext. For example:

ctx.channel().write();
vs
ctx.write();

or

ctx.channel().alloc()
vs
ctx.alloc()

Question is, why are these similar operations defined both on Channeland also onChannelHandlerContext`? Are they different in any ways? Under which conditions should one be preferred over the other?


Solution

  • Channel.* starts at the tail of the pipeline while Channel.* starts from the handler that the context belongs too. Usually in a ChannelHandler you always want to use ChannelHandlerContext while outside you mostly want to use Channel.