First knowledge of Netty

Source: Internet
Author: User

We have learned the socket communication/io/nio/aio programming, for the communication model has a preliminary understanding, in fact, what we have learned before is only a model, if you want to put these real work in the real job, in fact, we have learned just a model, If you want to use these real things for real work, then you need to constantly improve, expand and optimize. For example, the classic TCP read packet write packet problem, or the size of the data received, the actual intentions of reading and answering the processing logic and other details need us to seriously think, and these we all need a lot of time and experience, and rich experience. So want to learn socket communication is not an easy thing, then now, we have to learn a new technology Netty, Why we choose Netty, reason without him, simple! we no longer have to write complex code logic to achieve communication, we no longer need to consider performance as a problem, do not need to consider coding problems, half-packet read and write problems, these powerful Netty has helped us to achieve, we only need to use.

Netty is also the most left of the NIO framework, and his robustness, functionality, performance, customisation, and scalability are among the best in its kind. It has been validated by hundreds of commercial/commercial projects, such as the RPC framework Avro for Hadoop, the JMS framework we learned later, the powerful ROCKETMQ, the mainstream distributed communication framework Dubbox, and so on.

Netty is a Web application framework based on Java NIO.

Netty is a NIO client-server (client server) framework that enables rapid development of network applications, such as server and client protocols, using Netty. Netty provides a new way to develop Web applications, a new way to make it easy to use and highly scalable. The internal implementation of Netty is complex, but Netty provides an easy-to-use API to decouple business logic from network processing code. Netty is entirely based on NIO, so the entire netty is asynchronous.

Network applications often require a high degree of scalability, whether Netty or other Java NIO-based frameworks, providing a scalable solution. A key component of Netty is its asynchronous nature, which discusses synchronous (blocking) and asynchronous (non-blocking) Io to illustrate why asynchronous code is used to address extensibility issues and how to use Asynchrony.

Netty Architecture Composition

Netty characteristics

Here's a look at the Getting started code, before using Netty, review the NIO communication steps:

1. Create a Serversocketchannel, configure it for non-blocking mode

2. Binding monitoring, configuring TCP parameters, input bacllog size, etc.

3. Create a separate IO thread, and the user polls the multiplexer selector

4. Create the selector, register the previously created Serversocketchannel on the selector, and set the listener ID bit

5. Start the IO thread, execute the Selector.select () method in the loop body, and poll the Ready channel

6. When the polling is in the Ready channel, you need to determine the operation bit, if it is the accept state, the new client access, then call the Accept method accepts the new client.

7. Set some parameters of the new access client, such as non-blocking, and continue to register their channels in the selector, set the monitoring ID bit, etc.

8. If the polling channel operation bit is read, it reads, constructs the buffer object, etc.

9. More details of the data can not be sent to complete the issue of continuing to send ...

A simple NIO server-side program that is so replicating, take a look at the steps that Netty needs to accomplish this:

Netty steps to implement communication:

1. Create two NIO thread groups, one dedicated to network event processing (accepting client connections), and the other to read and write network communications.

2. Create a Serverbootstrap object, configure a series of parameters for Netty, such as the cache size to accept outgoing data, and so on.

3. Create a class channellnitializer that actually processes the data, preparing for initialization, such as setting the character set to accept outgoing data, the format, and the interface that has actually processed the data.

4. Bind the port and perform a synchronous blocking method to wait for server-side startup.

In just 4 steps, here's an implementation demo:

The above is the server side of the code, mainly set up a configuration, the implementation of specific code, etc., are in the serverhandler inside the execution. Here's a look at the Serverhandler code:

Here is the code for Serverhandler, note that when the Writeandflush () method is called, the last stream is automatically freed without calling Referencecountutil.release (msg); Here's a look at the client-side code:

It is important to note that the channel on this side is using the Niosocketchannel object. At the time of initialization, go to call ClientHandler, see below ClientHandler

Look at the results of the operation:

Look at the server side:

Receives the 777 that the client sends, and then the server echoes a response

Look at the client side

The server-side response was received and the corresponding data was printed

Highly recommended go to http://ifeve.com/netty5-user-guide/. Check out the User Guide

First knowledge of Netty

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.