Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...