Netty event monitoring and processing (on)

Through the introduction, you will learn:

  • Event monitoring, NIO, threading model and other related concepts;
  • Netty overall structure;
  • Event monitoring and processing;
  • Project practice summary;

This article will introduce the first two sections first, and the next one will introduce the last two sections.

At the end of this article, we will explain the rules for the extraction of benefits, and everyone will actively participate >_<

Related concepts

Netty is a NIO framework, which abstracts the status changes of IO channel establishment, readability, and writability into events, and transmits them in a chain of responsibility. You can insert custom Handlers on the processing chain to respond to interesting events. Monitor and process.

Therefore, first introduce the basic concepts of event monitoring, responsibility chain model, socket interface, IO model, thread model, etc., which will be of great help to understand Netty's event monitoring and processing later.

event listener

The JDK listener pattern mainly includes the following elements:

  • EventObject event object
  • EventListener event listener interface
  • custom event source
  • event trigger

The pattern is very simple, the user can customize the event source, save the relevant data of the triggering object, and after the event is triggered, it is passed to the handler who registered the event. The event listener interface is for unifying handler methods.

Take a well-understood button click event, where ActionListener is the event listener, and ActionEvent is the event object, which contains the event source:
Netty event monitoring and processing (on)

The specific process of implementing a set of event monitoring:

  • determine the source of the event;
  • Clarify possible events and define them as different event objects or event methods;
  • Provide a storage structure for saving the object listening for the event, and when the event occurs, the listener will be notified;
  • Execute the callback method for business processing;

event listener

Chain of Responsibility Model

It mainly refers to the organization of event handlers. Through the chain of responsibility model, it is very convenient to add custom handlers at any processing node.

Regarding the concept of chain of responsibility, here is a brief description: allow multiple objects to have the opportunity to process the request, so as to avoid the coupling relationship between the sender and receiver of the request, connect the object into a chain, and follow the chain Pass the request until an object handles it.

Chain of Responsibility Model

socket

Generally speaking, NIO is mainly for network IO, reading data from the network card, and writing data to the network card. This is the data source of the listener mode.

Network programming is mainly carried out through the socket interface provided by the operating system. By understanding the socket interface, the events can be summarized.

A socket is a unified interface between user processes and kernel network protocols.
Socket is also a special kind of file. Network communication can be regarded as reading the file, which makes the control of the network as convenient as the control of the file.

sokcet interface

NIO and IO model

NIO refers to non-blocking IO. We generally refer to IO as blocking IO. If you want to fully understand these concepts, you will talk a lot. Here is a brief summary.

The memory occupied by a process includes user mode and kernel mode. For security, user code cannot directly operate the kernel mode memory. It interacts through system calls, such as reading network data. The interaction process is as follows:

synchronous blocking

After the user thread initiates a read request, it needs to wait for the data to arrive before returning. During this period, the user thread cannot do anything. If it is network programming, there may be many Socket objects listening, which will create a large number of threads to be blocked, resulting in waste of resources and performance. decline.

In response to this situation, the concept of the IO model has emerged, in several ways:

  • Synchronous non-blocking IO;
  • IO multiplexing;
  • Asynchronous IO;

For specific introduction, there is a lot of information on the Internet, so I won't go into details. Here I only mention IO multiplexing. Let me talk about my understanding. This method is used in our project.

The so-called multiplexing is mainly because the operating system provides us with this development mode: interested IO events (established, readable, writable, etc.) can be registered in advance, and multiple socket objects can be registered to a selector selector In this way, multiple socket objects can be monitored by one user thread. When an event occurs, the corresponding socket will be searched for reading, writing and other operations.

Friends who have done NIO development before, you can look at the following example to review the whole process:

java NIO example

threading model

The above mentioned my understanding of multiplexing and mentioned that one thread listens to multiple sockets, but if there are many sockets, one thread cannot handle it. In addition, the reception and judgment of events and the reading, processing, and writing of data can be performed in different threads.

This leads to the concept of threading models, such as Reactor and Proactor models. The specific details will not be introduced. There are many materials on the Internet, and the ultimate purpose is to improve the performance of IO event processing.

Netty overall structure

This part is mainly to understand Netty, and do not delve into its implementation principle first.

Overview

Netty is an asynchronous event-driven network application framework that enables rapid development of maintainable, high-performance protocol-oriented servers and clients;
it harnesses the power of Java's high-level API and hides it in an easy-to-use API Later;

Netty overall structure

  • Core (core part), is some general abstraction of the underlying network communication, this part is the key.
  • Transport Services (transmission services), the definition of specific network transport capabilities and some implementations.
  • Protocol Support, netty's implementation of encoding and decoding for some common protocols.
zero copy

In a broad sense, zero-copy means that the CPU does not need to consume resources for copying data between memories during computer operations. The sendfile() in Linux and the FileChannel.transferTo() method in Java NIO both implement the zero-copy function, and in Netty, it also implements zero-copy by wrapping the NIO's FileChannel.transferTo() method in FileRegion.

The zero copy referred to in Netty is completely in user mode, and it is more inclined to optimize data operations. Netty allows us to combine multiple pieces of data into a whole piece of virtual data for users to use without copying the data.

Netty zero copy

Unified Communication Model

The traditional JAVA IO API needs to use different types and methods when dealing with different transport protocols, such as: java.net.Socket and java.net.DatagramSocket, they do not have the same supertype; Java's new IO API and The original blocking IO API is also incompatible;

Netty provides a unified API programming interface, which abstracts all point-to-point communication operations, and can switch between different transmission implementations by adjusting only a few lines of code:

  • TCP/IP transmission based on NIO
  • TCP/IP transmission based on OIO
  • UDP/IP transmission based on OIO
  • local transmission
event model

That is to say, event monitoring and processing provides a good way to handle various events.

Netty event model

The general processing process is as shown in the figure above, and the details will be introduced in the next article.

Welcome to scan the QR code below and follow my personal WeChat public account~

love story

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325585255&siteId=291194637