“This article has participated in the call for good writing activities, click to view: [back end, big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!

Last lesson, we had a preliminary understanding of Netty. In this lesson, we will overlook the whole context of Netty, so as to have a general understanding of the architecture principle of Netty! The diagram below is the main framework of Netty: (welcome to pay attention to wX public number: [source code apprentice] explore all kinds of open source code implementation!)

Basic knowledge of EventLoopGroup

Netty has implemented a number of extensions to the EventLoopGroup.

Our lesson use case, use NioEventLoopGroup, he is the realization of the NIO, can see he is a subclass of MultithreadEventLoopGroup, can be seen from the name, NioEventLoopGroup is a multi-threaded event loop group, Here you can think of it as a thread pool with multiple threads (NioEventLoop) inside, one thread for each SocketChannel to which the client connects (NioEventLoop)!

NioEventLoop (SingleThreadEventLoop) NioEventLoop (SingleThreadEventLoop) NioEventLoop (SingleThreadEventLoop) As you can see, the parent interface of NIoEventLoop is actually inherited from the EventLoopGroup, which means that although NIoEventLoop is a single-threaded event loop, we can also think of it as a thread pool based on the interface, except that there is only one thread in the thread pool!

## Netty communication channel

What we need to know about the types of pipes in Netty, here we focus on NIO implementation:

We can see that there are two main implementations of Netty’s Socket communication channel, NioServerSocketChannel and NioSocketChannel. These two implementations are different implementations of Netty’s server channel and client channel. We specify the type of pipe to use when developing the Netty server and client! There are one of the more important point, namely NioServerSocketChannel superclass implementation is AbstractNioMessageChannel, NioSocketChannel superclass implementation is AbstractNioByteChannel, These two implementations are important for determining whether to process connections or data during subsequent NIO event loops, so keep an eye on them and we’ll get into more about them later!

Netty pipe flow

What we need to know about Netty’s business execution chain, also called the ChannelPipeline:

Our Netty code will consist of code that looks like this:

It is the main implementation of Netty that allows us to focus on the business. Its main implementation is a two-way linked list, where a business Handler is appendedto the end of the list. There are roughly two types of handlers:

As shown in the above, he has two implementations, one is ChannelInboundHandlerAdapter ChannelOutboundHandlerAdapter, they call in a business flow of execution order as follows, we use a chart to illustrate:

When we call the Socket’s API to read data from the Socket pipe, the Pipeline will execute the Inbound Handler in the order you added it. When we finish reading the event, we call the write method to write data into the channel. The pipe flow starts calling the Outbound Handler method, in reverse order! This reverse order call may not be easy to understand, but let’s use the 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 Handler event callback type

1. ChannelInboundHandler

Method names Methods effect
handlerAdded The channel is added by 1
channelRegistered Method 2 to call back all handlers after successful JDK registration
channelActive Callback 3 after jdkChannel is activated
channelRead There is data readable in the channel 4
channelReadComplete Data is read. 5
channelInactive Call back to method 6 after the channel is closed
channelUnregistered The channel is unregistered back to call this method 7
handlerRemoved The channel is deleted 8
userEventTriggered This method is called if a user event is fired.
channelWritabilityChanged Writable state changes
exceptionCaught Something abnormal happened

2. ChannelOutboundHandler

Method names Methods effect
bind Called after a binding operation.
connect Called after a connection operation.
disconnect Called after a disconnection operation.
close Called after a close operation.
deregister Called after an unlogout operation from a currently registered EventLoop.
read Read the data
write Write the data
flush Flush to pipe

conclusion

Through this article, we can learn some important concepts in Netty, including the basic concept of EventLoopGroup, the concept of channel in Netty, and the concept of pipe flow in Netty.