Article Directory
foreword
The previous article "Netty Getting Started Guide" mainly covered the introductory knowledge of Netty, including Netty's development history, core functions and components, and demonstrated how to use Netty to build an HTTP server through examples. Due to the high level of abstraction of Netty, it may be more complicated and challenging to understand, so this article compares the processing flow of Java NIO with the overall process of Netty, and combines the source code of Netty to understand Netty more clearly.
How Java NIO works
First of all, we know that Netty is a network application framework based on Java NIO, which is encapsulated and expanded on the basis of it (so before understanding Netty in depth, it is recommended to have a certain understanding of Java NIO), so the two have a good understanding of the network connection, The operation mode of reading and writing is similar.
As shown in the figure above, the processing flow of Java NIO is combined with the Java NIO code example. It can be seen that after ServerSocketChannel
registering and Selector
listening to each event, after Selector
receiving the event request, our business code judges it and handles it accordingly. When using Netty We don't seem to need to write these codes, and we don't even see Selector
the ServerSocketChannel
words ", ", then how these codes are reflected in Netty, let's take a look at them: take
the example of the HTTP server built by Netty above as an example, and directly focus on ServerSocketChannel
what " Selector
" is . When it was created, when the event was registered and processed.
Selector creation
In fact, after reading the introduction to Netty in the previous article, you can know EventLoop
that is responsible for handling various events, so you can blindly guess that Selector
it should be NioEventLoopGroup
created in , look
In NioEventLoopGroup
the construction method of the JDK is called to SelectorProvider
create Selector
, that is, the code of Java NIO.
Creation of ServerSocketChannel
Regarding ServerSocketChannel
the creation of , directly find the method of binding the port, as shown in the figure below
Also, see the code for Java NIO in NioServerSocketChannel
Netty 's code .newChannel()
Register Selector for ServerSocketChannel
In the figure below, ServerSocketChannel
a is allocated to it after creation EventLoop
and a new thread is started (this is also the embodiment of Netty's multi-threaded asynchrony). Finally, it doRegister()
is registered on the interface that calls the JDK Selector
and listens to events. selectionKey
It should be clear when you see .
Handling of events
Now that ServerSocketChannel
you have registered and listened to the event, the next step is to process it Selector
when an event comes, and directly look at the code, because it is started through a new thread, so you should be familiar with the code you are looking at , because it has been monitored An event can be processed. The following is the processing of the read event. In the figure, the chain of responsibility mode is used to process the event, which is convenient for expansion. So the relationship between the read event in Netty and Java NIO is shown in the figure below.EventLoop
NioEventLoop
run()
processSelectedKeysPlain()
ChannelPipeline
Summarize
So before getting in touch with Netty, you must first master Java NIO. This article only introduces the embodiment of Java NIO in Netty, and Netty's encapsulation of Java NIO, so that everyone can understand Netty more conveniently, and does not involve Netty's efficient and powerful design. , which will be introduced below.