Netty: data processing process, netty Data Processing Process

Source: Internet
Author: User

Netty: data processing process, netty Data Processing Process

Netty, as an asynchronous and event-driven network communication framework, helps us quickly develop high-performance and high-reliability network services.

To better use Netty to solve development problems, it is necessary to learn Netty.

Currently, Netty has three mainstream versions: Netty3, Netty4, and Netty5. Among the three versions, the thread model with the greatest variation is the same. However, the Channel model does not change much. Therefore, the data processing process does not change much. So this article describes the Netty data processing process. The thread models of different versions will be described later.

 

Channel Model

The Netty Channel model is shown in a simple figure:

 

 

A Channel contains a Socket and a ChannelPipeline. One ChannelPipeline has one ChannelSink and multiple ChannelHandler. ChannelHandler can be divided into UpstremHandler and DownstreamHandler.

 

Both read and write data must go through ChannelPipeline in the Channel. The Data Reading Process is from Socket to ChannelPipeline, Which is handed over to the UpstreamHandler (or InBoundHandler) in the ChannelPipeline for processing from bottom to top. When writing data, it is processed from top to bottom by DownStreamHandler (or OutBoundHandler) in ChannelPipeline.

 

 

Channel Creation

There are two main types of channels: ServerSocketChannel and SocketSocketChannel. Whether it is NIO is not distinguished here.

The creation of ServerSocketChannel is automatically completed at bind, and the creation of SocketChannel is completed by Netty. Whether the client creates a connection or the server receives the client connection, SocketChannel creation is completed by Netty.

When creating a Channel, the corresponding ChannelPipeline producer is automatically called to create the Channel. When creating a ChannelPipeline, you can configure related ChannelHandler. The ServerSocketChannel can be customized by the user to create a ChannelHandler, while the SocketChannel can be customized by the user to create multiple ChannelHandler.

 

In each version, the methods used for customization may be different.

 

 

 

ChannelSink

This component is mainly found in V3Medium

ChannelSink has two functions:

1. When an exception occurs, use predictioncaught to pass the predictionevent along the Pipeline, (channel. getPipeline (). sendUpsteam (new DefaultExceptionEv ent (channel, ex )));

2. When a Message or an Events executes all ChannelHandler processing from top to bottom along the Pipeline, it enters the eventSunk method of ChannelSink.

 

 

The following describes the functions of Sink on the Server and Client:

ServerSocketPipelineSink eventSunk:

  • Submit the Boss task when creating the ServerSocket.
  • When a Socket is received, create a SocketChannel and hand over a Worker task to the Worker Executor.

 

ClientSocketPipelineSink eventSunk:

1) when a ChannelEvent is transferred on Pipeline:

· If a connection event is created, a worker task is handed over to the worker executor.

· If it is an event of interest, register the event of interest.

2) when the message is transferred on the Pipeline:

Put the data to be written into the write queue, and then call the writeFromUserCode method of NioWorker.

 

 

ChannelHandler

Netty is an event-driven framework. Any operation is event-driven.

ChannelHandler is an Event Hander (Event processor). Its function is as follows:

· Process IO events (read/write), such as reading and decoding data.

· Handle events of interest.

· Hand it to the next ChannelHandler for processing.

 

 

Data Writing can be triggered in two ways.

 

 

 

 

The above briefly introduces the functions and processing processes of various components in the Channel. Knowing these things can help us understand and use Netty.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.