In the last lesson, we have a preliminary understanding of Netty. In this lesson, we will look at the entire Netty context so as to have an overall understanding of Netty's architecture and principles! The following figure is the main framework of Netty: (Welcome to pay attention to the wx public number: [source code apprentice] to explore various open source code implementations together!)
Basic understanding of EventLoopGroup
What we need to know about EventLoopGroup , Netty has made a lot of extensions to EventLoopGroup, the following picture is his family map:
The case we used in the previous lesson was NioEventLoopGroup
, which is an implementation of NIO. It can be seen that it is MultithreadEventLoopGroup
. As you can see from the name, NioEventLoopGroup is a multi-threaded event loop group. Here you can put It is regarded as a thread pool, there are multiple threads (NioEventLoop) inside, and each client connected SocketChannel
corresponds to a thread (NioEventLoop)!
We have been talking about NioEventLoop above. Based on the above figure, we can see that it is SingleThreadEventLoop
. As can be seen from the name, it is a single-threaded executor! We can see that his parent interface is in fact inherited from EventLoopGroup
, that is to say, although NIoEventLoop is a single-threaded event loop, we can also regard it as a thread pool based on the interface, but the internal thread pool Only one thread!
\## Netty communication channel
We need to understand the types of pipelines in Netty. Here we focus on the implementation of NIO:
We can see that there are two main implementations of Netty's Socket communication pipeline, NioServerSocketChannel
and NioSocketChannel
. These two implementations are Netty's different implementations of the server channel and the client channel. We are developing Netty's server and client At the end, the type of pipe used will be specified! There is a more important point here, that is, the parent implementation of NioServerSocketChannel is AbstractNioMessageChannel , and the parent implementation of is 160dc40b06f208 AbstractNioByteChannel . These two implementations are important to determine whether to process the connection or process the data in the subsequent NIO event loop. Means, leave an impression here, we will talk about it in detail later!
Netty's pipeline flow
We need to understand the Netty business execution chain, also called pipeline flow ChannelPipeline
:
Our Netty code will consist of code similar to this logic:
It is the main way that Netty allows us to focus on the business. His main implementation is a doubly linked list. Here is to add a Handler service processor at the end of the linked list. There are two types of Handlers:
As shown in the figure above, it has two implementation methods, one is ChannelInboundHandlerAdapter
other is ChannelOutboundHandlerAdapter
. The execution sequence of their calls in a business flow is as follows, we borrow an illustration to illustrate:
When we call the Socket's data reading API, that is, when reading data from the Socket pipe, the Pipeline will execute Inbound Handler order of your addition. When we finish reading the event, call the write method to write data into the channel At that time, the pipeline flow starts to call the Outbound Handler method, in reverse order! This reverse call may be difficult to understand, but we still use a diagram to illustrate:
When the read method is called, the Inbound nodes are called sequentially! When calling the write method, call the outbount method in reverse order!
Netty's Handler event callback types
1. ChannelInboundHandler
Method name | Method effect |
---|---|
handlerAdded | Channel is added 1 |
channelRegistered | Method to call back all Handlers after successful JDK registration 2 |
channelActive | Callback after jdkChannel is activated 3 |
channelRead | There is data readable in the channel 4 |
channelReadComplete | Data read 5 |
channelInactive | Call back this method after the channel is closed 6 |
channelUnregistered | The channel is unregistered back to call the method 7 |
handlerRemoved | Channel is deleted 8 |
userEventTriggered | If a user event is triggered, this method is called. |
channelWritabilityChanged | Writable state change |
exceptionCaught | An exception occurred |
2. ChannelOutboundHandler
Method name | Method effect |
---|---|
bind | Called after binding operation. |
connect | Called after connection operation. |
disconnect | Called after disconnecting operation. |
close | Called after closing operation. |
deregister | Called after logout operation from the currently registered EventLoop. |
read | Read data |
write | Write data |
flush | Flush to pipeline |
to sum up
Through this article, we can learn about several important concepts in Netty, the basic concepts of EventLoopGroup, the channel concept in Netty, and the concept of pipeline flow in Netty!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。