Understand Netty from the perspective of Java NIO

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.
How Java NIO works
Java NIO code example

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 ServerSocketChannelregistering and Selectorlistening to each event, after Selectorreceiving 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 Selectorthe ServerSocketChannelwords ", ", then how these codes are reflected in Netty, let's take a look at them: take
insert image description here
the example of the HTTP server built by Netty above as an example, and directly focus on ServerSocketChannelwhat " 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 EventLoopthat is responsible for handling various events, so you can blindly guess that Selector it should be NioEventLoopGroupcreated in , look

insert image description here
In NioEventLoopGroupthe construction method of the JDK is called to SelectorProvidercreate Selector, that is, the code of Java NIO.

Creation of ServerSocketChannel

Regarding ServerSocketChannelthe creation of , directly find the method of binding the port, as shown in the figure below
insert image description here

Also, see the code for Java NIO in NioServerSocketChannelNetty 's code .newChannel()

Register Selector for ServerSocketChannel

In the figure below, ServerSocketChannel a is allocated to it after creation EventLoopand 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 Selectorand listens to events. selectionKeyIt should be clear when you see .
insert image description here

Handling of events

Now that ServerSocketChannelyou have registered and listened to the event, the next step is to process it Selectorwhen 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.EventLoopNioEventLooprun()
insert image description here
processSelectedKeysPlain()
insert image description here
ChannelPipeline

insert image description here

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.

Guess you like

Origin blog.csdn.net/qq_28314431/article/details/132343223