Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

netty - 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?

question from:https://stackoverflow.com/questions/65936958/what-is-the-difference-between-operations-on-channel-vs-operations-on-channelhan

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...