Introduction to "Netty" Netty core components

Source: Internet
Author: User

First, preface

The previous blog post experienced the first example of Netty, followed by learning about Netty components and their design.

Second, the core components

  2.1. Channel, EventLoop and Channelfuture

The core components in Netty include channel, EventLoop, Channelfuture. This is described below.

  1. Channel interface

Basic IO operations, such as binding, connection, reading and writing, depend on the primitives provided by the underlying network transport, and in Java's network programming, the base core class is the socket, while the Netty Channel provides a set of APIs that greatly simplifies the complexity of working directly with the socket. And the channel is the parent of many classes, such as Embeddedchannel, Localserverchannel, Niodatagramchannel, Niosctpchannel, Niosocketchannel, and so on.

  2. EventLoop interface

EventLoop defines the core abstraction that handles events that occur during a connection, and is further discussed where the relationship between channel, EventLoop, Thread, and Eventloopgroup is shown

  

Can see.

· A eventloopgroup contains one or more eventloop.

· A eventloop is bound to a thread in the life cycle.

· EventLoop uses its corresponding thread to handle IO events.

· A channel is registered using EventLoop.

· A eventloop can be assigned to one or more channel.

  3. Channelfuture interface

All IO operations in the Netty are asynchronous and do not return immediately, and you need to determine the result of the operation later. So Netty provides Channelfuture, whose AddListener method can register a channelfuturelistener, which is notified when the operation is complete, whether successful or unsuccessful. Channelfuture stores the results of subsequent operations and cannot predict when the operation will be executed, and the actions submitted to the channel are executed in the order in which they are awakened.

  2.2. Channelhandler and Channelpipeline

Channelhandler and Channelpipeline are closely related to user logic and data flow.

  1. Channelhandler interface

From the application developer's point of view, Channelhandler is the most important component that holds the user logic to handle inbound and outbound data. The Channelhandler method is triggered by network events, and Channelhandler can be used for almost any type of operation, such as converting data from one format to another or handling thrown exceptions. For example, its sub-interface is Channelinboundhandler, accepting incoming events and data for user-defined logic processing, or refreshing channelinboundhandler data when responding to a connected client.

  2. Channelpipeline interface

Channelpipeline provides a container for the Channelhandler chain and defines an API for propagating inbound and outbound event streams along a chain. When the channel is created, an attached channelpipeline is automatically created. Channelhandlers follow the steps below to install in Channelpipeline.

· A Channelinitializer implementation is registered in the Serverbootstrap.

· When the Channelinitializer Initchannel method is called, Channelinitializer installs a custom set of Channelhandlers in the pipeline.

· Channelinitializer to remove itself from the channelpipeline.

Channelhandler can be used as a container for placing any code to handle events or data arriving and passing through Channelpipeline, and data can be passed along the processing chain.

  

When an event is moved from the client to the server, it is called an outbound, which is called an inbound. and the inbound processor and the outbound processor can coexist in the same pipeline, when the inbound data or event is read, it will be passed from the head of the pipe to the first inbound processor, and then to the next processor until the end of the pipeline, when data processing ends. When the outbound, along the processing chain until the head of the pipeline, and then network transmission.

  2.3. Bootstrapping

The Netty Boot class application Network layer configuration provides a container that involves binding a process to a given port or connecting a process to another process running on a specified port on a specified host. The boot class is divided into the client boot bootstrap and the service-side boot Serverbootstrap, the two boot links and differences as shown.

    

Where Serverbootstrap binds to the specified port to listen for client connection requests, Bootstrap connects to the remote server. And Serverbootstrap contains two eventloopgroup, and Bootstrap contains only one eventloopgroup. The serverbootstrap contains two sets of channels, the first of which contains a serverchannel that represents the listener socket that the server binds to the local port, and the second group contains all the channels created to handle the client connection, creating a channel each time a connection is accepted. Shows the two eventloopgroup of Serverbootstrap.

  

Eventloopgroup assigns an eventloop to the associated Serverchannel, which creates a channel for the connection request (belonging to the second eventloopgroup), and once a connection request is accepted, The second eventloopgroup will create a eventloop for the channel.

Iii. Summary

This blog post briefly introduces some of the core components and their role in Netty, as well as the association between the components, the following will be explained in more detail, thank you for watching the Garden friends ~

Introduction to "Netty" Netty core components

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.