This is the 7th day of my participation in the August Gwen Challenge.


preface

This article is really into the Netty study of the first, learn Netty before, to understand the system interruption, IO multiplexing, NIO these essential basic knowledge, otherwise learn Netty is more difficult, so if partners have not read my first few basic knowledge can first learn to look back at this article.

What is an OPERATING system interrupt? [core Netty series]IO multiplexing basic principle of detail, Java interview large company will ask Java NIO want to thoroughly understand? The most heartfelt EXPLANATION of NIO for you


concept

Netty is an asynchronous event-driven network application framework based on Java NIO. It encapsulates the complexity of network programming, users do not need to worry about how to make complex network calls, just through Netty can be completed, thus making development easier and convenient. Netty is the most popular NIO framework. Many frameworks use Netty for their underlying RPC, such as Dubbo and Elasticearch.

Why is Netty so popular

I think Netty’s popularity is mainly due to the following advantages

  • Unified API that supports multiple transport types, blocking and non-blocking.
  • Simple and powerful threading model
  • True connectionless datagram socket (UDP) support
  • Link logical components to support reuse
  • Has higher throughput and lower latency than Java’s core apis
  • Have lower resource consumption, minimal memory replication
  • There are no OutofMemoryErrors caused by slow, fast, or overloaded connections

Netty core components

The core components of Netty can be roughly divided into channels, callbacks, futures, events, and ChannelHandlers. These constructs represent different types of constructs: resources, logic, and advice.

Channel

It represents an open connection to an entity, such as a hardware device, a file, a network socket, or a program component that can perform one or more different I/O operations, such as read and write operations. The following channels are commonly used:

  • EmbeddedChannel

  • LocalServerChannel

  • NioDatagramChannel

  • Compared with native NIO channels, NioSctpChannel has the following advantages

  • At the Channel interface layer, the Facade mode is used to encapsulate network I/O operations and other operations associated with network I/O and provide them externally in a unified manner.

  • The Channel interface is defined as large and complete as possible to provide a unified view for SocketChannel and ServerSocketChannel. Different subclasses implement different functions. Common functions are implemented in abstract parent classes to maximize the reuse of functions and interfaces.

  • The specific implementation adopts aggregation rather than inclusion, aggregating related function classes in Channel, which is responsible and scheduled uniformly by Channel, making function implementation more flexible.

The callback

A callback is simply a method that refers to a method that has been provided to another method. This is also one of the most common ways to notify interested parties when an operation is completed.

Callbacks are used to handle events in Netty, and when a callback is triggered, the associated event can be handled by the implementation of ChannelHandler. When a new connection has been established, the ChannelHandler’s channelActive() callback method is called.

ChannelHandler

The main component of Netty is the ChannelHandler, which acts as a container for all the application logic that handles inbound and outbound data.

Netty uses different events to notify us of state changes or the status of operations, each of which can be sent to a user-implemented method in the ChannelHandler class. Netty provides a number of predefined ChannelHandler implementations ready out of the box, including ChannelHandler for various protocols. ChannelPipeline is used if the ChannelHandler does not return the processing directly to the client, but instead passes it to the next ChannelHandler to continue processing

ChannelPipeline

The ChannelPipeline provides a container for the ChannelHandler chain and defines the API used to propagate the flow of inbound and outbound events on the chain. The ChannelPipeline provides a container for the ChannelHandler chain and defines an API for propagating a stream of inbound and outbound events on that chain. It is the job of the ChannelHandler to make events flow through the ChannelPipeline, which is installed during the initialization or boot phase of the application. These objects receive events, execute the processing logic they implement, and pass the data to the next ChannelHandler in the chain:

  • An implementation of ChannelInitializer is registered with ServerBootstrap.
  • When ChannelInitializer. InitChannel () method is invoked, ChannelInitializer will install a set of custom in the ChannelPipeline ChannelHandler.
  • ChannelInitializer removes itself from the ChannelPipeline.

ChannelFuture

The Future provides another way to notify the application when an operation is complete. It will be completed at some point in the future and provide access to its results.

A Future provided in the JDK and shipped only allows you to manually check that the corresponding action has completed, or to block until it does. This is tedious, and Netty provides its own implementation, ChannelFuture, for use when performing operations asynchronously.

ChannelFuture provides several additional methods that enable us to register one or more instances of ChannelFutureListener. The listener’s callback method, operationComplete(), will be called when the corresponding operation completes. The listener can then determine if the operation completed successfully or if it went wrong. The notification mechanism provided by ChannelFutureListener eliminates the need to manually check that the corresponding operation has completed. Each Netty outbound I/O operation returns a ChannelFuture;

EventLoop

EventLoop defines Netty’s core abstraction for handling events that occur during the lifetime of a connection. Internally, an EventLoop is assigned to each Channel.

EventLoop defines Netty’s core abstraction for handling events that occur during the lifetime of a connection. Internally, an EventLoop is assigned to each Channel.

The EventLoop itself is driven by a single thread that handles all I/O events for a Channel and does not change for the entire life of the EventLoop. This simple and powerful design eliminates any concerns you might have about needing to synchronize in your ChannelHandler implementation.

EventLoop is managed by the EventLoopGroup. Also note that the client Bootstrap class is Bootstrap and only an EventLoopGroup is required. The ServerBootstrap class is ServerBootstrap, which typically requires two eventloopgroups, one for receiving client connections and one for handling I/O events (you can also use just one EventLoopGroup, It will share the same EventLoopGroup in both scenarios.

  • An EventLoopGroup contains one or more Eventloops;
  • An EventLoop is bound to only one Thread during its lifetime;
  • All I/O events processed by EventLoop will be processed on its proprietary Thread;
  • A Channel registers with only one EventLoop during its lifetime;
  • In NIO, an EventLoop is assigned to multiple channels (one EventLoop is triggered and executed sequentially for multiple channels). In OIO, an EventLoop is assigned to a Channel.

conclusion

We first introduce the concept of Netty in the beginning, it is based on the asynchronous NIO event-driven network application framework, and then listed the advantages of Netty, high concurrency, support for multiple transport type, etc., finally introduces the Netty several big core components, by letting you know there is an integral part of the simple, you might be in these components concept is not very clear, I’ll go through each component in detail in future articles.

If you take small steps each day and add them up, the results will be bigger than you think. Welcome to pay attention to me, progress together, grow together.