In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about why ChannelOutboundHandler declares a read method in the foundation of Netty. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
ChannelOutboundHandler is supposed to focus only on outbound events, but it declares a read method:
/ * Intercepts {@ link ChannelHandlerContext#read ()}. * / void read (ChannelHandlerContext ctx) throws Exception
Someone asked this question at stackoverflow, and trustin answered:
Inbound handlers are supposed to handle inbound events. Events are triggered by external stimuli such as data received from a socket. Outbound handlers are supposed to intercept the operations issued by your application.
So the read method on ChannelOutboundHandler, as stated in its comments, is to intercept the ChannelHandlerContext.read () operation. That is, ChannelOutboundHandler can block reading more data to inbound when necessary through the read () method. This design is very useful when dealing with handshakes for protocols.
I have a question in Netty4, An I/O event is handled by either a ChannelInboundHandler or a ChannelOutboundHandler
The first question is why read and write method both in ChannelOutboundHandler?
Why trigger read () method in the fireChannelReadComplete ()? What is the design philosophy?
@ Overridepublic ChannelPipeline fireChannelReadComplete () {head.fireChannelReadComplete (); if (channel.config () .isAutoRead ()) {read ();} return this;}
- -
Inbound handlers are supposed to handle inbound events. Events are triggered by external stimuli such as data received from a socket.
Outbound handlers are supposed to intercept the operations issued by your application.
Re: Q1) read () is an operation you can issue to tell Netty to continue reading the inbound data from the socket, and that's why it's in an outbound handler.
Re: Q2) You don't usually issue a read () operation because Netty does that for you automatically if autoRead property is set to true. Typical flow when autoRead is on:
Netty triggers an inbound event channelActive when socket is connected, and then issues a read () request to itself (see DefaultChannelPipeline.fireChannelActive ())
Netty reads something from the socket in response to the read () request.
If something was read, Netty triggers channelRead ().
If there's nothing left to read, Netty triggers channelReadComplete ()
Netty issues another read () request to continue reading from the socket.
If autoRead is off, you have to issue a read () request manually. It's sometimes useful to turn autoRead off. For example, you might want to implement a backpressure mechanism by keeping the received data in the kernel space.
Shareimprove this answer
Answered Mar 13'14 at 8:19
Trustin
9,63666 gold badges3333 silver badges4747 bronze badges
This reasoning is not convincing. Inbound & Outbound are generally defined based on the direction of data flow, not based on the direction of who is calling whom. It looks to me like a bad design-Ashok Koyi Jul 6'18 at 9:24
Why would you ask a channel outbound handler for reading? Completely counter intuitive/also known as bad design-Ashok Koyi Jul 6'18 at 9:28
This is why ChannelOutboundHandler declares a read method in the Netty foundation. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.