Netty's introduction to the topic

Source: Internet
Author: User
Tags ack epoll message queue

This article describes the four IO scenarios of Java BIO (synchronous blocking IO), pseudo-asynchronous Io,nio (non-blocking IO), AIO (asynchronous IO), and compares the different IO models.

Directory

1.BIO

2. Pseudo-Asynchronous IO

3.NIO

4.AIO

5. Four IO comparisons

6.bio\ Pseudo-Asynchronous Io\nio\aio source Download

1.BIO

A server that uses the Bio communication model, typically a separate acceptor thread that listens for client connections, creates a new thread process link connection processing for each client after it receives a client connection request, and after processing, returns an answer to the client through the output stream, which the thread destroys.

The model is the biggest problem performance problem, when the client concurrent access increases, the service thread increases, when the number of threads expands, the performance of the system decreases, with the increase in concurrency, the system will have a thread stack overflow, create a new thread failure and so on, resulting in thread downtime or zombie, can not provide services externally. And the thread has a lot of overhead, affecting server performance.

The source code under the Src/main/java/bio, divides into the client and the service side, the Simple network, the thread processing.

2. Pseudo-Asynchronous IO

In order to solve a link that the synchronous blocking IO faces requires a threading situation, the concept of "pooling" is now introduced and the thread pool is added.

When there is a new client connection, the client's socket is encapsulated as a task (the Java Runnable Interface is implemented) and posted to the backend thread pool for processing. Because the thread pool can set the size of the message queue and the maximum number of threads, its resources are controllable, regardless of how many clients are concurrently accessed, without causing resource exhaustion and downtime.

The pseudo-asynchronous IO Communication framework employs the thread pool implementation, thus avoiding the problem of threading resource exhaustion caused by creating a separate thread for each request. However, due to its underlying communication still uses the synchronous blocking model, it is unable to fundamentally solve the problem.

Java output stream InputStream: when the input stream of the socket is read, it blocks until the following three events occur.

    • have data to read;
    • The available data has been read;
    • A null pointer or IO exception occurred.

This means that when the other side sends the data request or the response message is slow (the network transmission is slow), the read write stream side of the communication thread will be blocked for a long time, if the other party to 100s before the completion of the message, the read side of the IO thread will also block synchronization 100s, in this time, the other access messages can only be queued

Java input stream OutputStream: When calling OutputStream's Write method writes the output stream, it will be blocked until all bytes sent are written, or an exception occurs. The TCP/IP know that when the receiver processing of the message is slow, the data will not be read from the TCP buffer in time, which will cause the sender's TCP window size to decrease until it is 0, the two sides are in the keep-alive State, The message sender can no longer write the TCP buffers to the data, and the io,write operation with synchronous blocking will block indefinitely until the TCP window size is greater than 0 or an IO exception occurs.

The source code under Src/main/java/pseudoasynchronousio, divides into the client and the service side. The client and the bio client, the service side joins the thread pool Executorservice, the related constructor requests the reader to consult itself.

3.NIO

The NIO library, introduced in JDK1.4, makes up for the lack of synchronous blocking IO. In all data, NIO is processed by buffers, and any time access to the data in NiO is done through buffers. The buffer is actually an array. Java NiO is based on the multiplexer selector, which simply means that the selector will constantly poll the channels (channel, full duplex) registered on it, if there is a new TCP connection access, read-write event on a channel, The channel will be in a ready state, be polled by selector, and then be able to obtain a ready select collection for subsequent IO operations through Selectionkey.

A multiplexer can poll multiple channel simultaneously, and because the JDK uses epoll instead of the select implementation, there is no limit to the maximum connection handle. (Off-topic, here said EOPLL, select is said that the Linux under the IO multiplexing, and select, Epoll, clear process concept, please see the source directly).

NIO service-side sequence diagram

1. Open Serversocketchannel, which listens to the client's connection, which is the parent pipe for all client connections.

Serversocketchannel accptorsvr = Serversocketchannel.open ();

2. Bind the listening port and set the connection to non-blocking mode.

acceptorsvr.socket (). Bind (  new inetsocketaddress (Inetaddress.getbyname ("IP"), port)); Acceptorsvr.configureblocking (false);

3. Create a reactor thread, create a multiplexer, and start the thread.

Selector Selectot = selector.open (); New Thread (new rectortask ()). Start ();

4. Register the Selectsocketchannel on the reactor thread's multiplexer selector to listen for the Accept event.

Selectionkey key = Acceptorsvr.register (Selector,selectionkey.op_accept,iohandler);

5. Multiplexer in the thread run method in the wireless loop to poll the ready key.

int num == = Selectkeys.iterator ();  while (it.hasnext) {      = (Selectionkey) it.next;       /*      deal with  IO event    */    }

6. The multiplexer hears a new user access, processes a new access request, completes a TCP three handshake, and establishes a physical connection.

Socketchannel sc = ssc.accept ();

7. Set the client link to non-blocking mode

Sc.configureblocking (false); Sc.socket (). setreuseaddress (true); ..

8. Register the newly accessed client connection to the reactor thread's multiplexer and listen for read operations to read the network messages sent by the client.

Selectionkey key = Sc.register (Selector,selectionkey.op_read,iohangler);

9. Asynchronously reads a client request message to a buffer

int readnumber = Channel.read (Receivedbuffer);

10. Encode and decode the bytebuffer, if there is a half-packet message pointer Reset, continue to read the subsequent messages, the decoding of the successful message encapsulated into a task, posted to the business thread pool, business logic processing.

Object message =NULL;  while(Buffer.hasremain ()) {Bytebuffer.mark (); Object message=decode (Bytebuffer); if(message==NULL) {bytebuffer.reset ();  Break;        } messagelist.add (message); }        if(!Bytebuffer.hasremain ())        {bytebuffer.clear (); }        Elsebytebuffer.compact (); if(messagelist!=NULL&!Messagelist.isempty ()) {             for(Object messagef:messagelist) handletask (Messagee); }

11. Encode the Pojo object into Bytebuffer and invokes the asynchronous write interface of Socketchannel to send the message asynchronously to the client.

Socketchannel.wite (buffer);

Note: If the Send zone TCP buffer is full, it causes the write half packet, at which point the write operation needs to be registered, and the write is cycled until the entire package message is written to the TCP buffer.

NIO client sequence diagram (mostly similar to server)

1. Open Socketchannel, bind client-local address (optional, default system randomly assigns an available local address)

Socketchannel Clientchannel = Socketchannel.open ();

2. Set the Socketchannel to non-blocking mode while setting the TCP parameters for the connection.

Socketchannel.configureblocking (FALSE);
Socket.setreuseaddress (TRUE);
Socket.setreceivebuffersize (buffer_size);
Socket.setsendbuffersize (buffer_size);

3. Connect the server asynchronously.

Boolean connected = Clientchannel.connect (new inetsocketadress ("IP", port));

4. Determine if the connection is successful, if successful, then directly register the read status bit to the multiplexer, if not successful (asynchronous connection, return False, indicating that the client has already sent the sync packet, the server does not return an ACK packet, the physical connection has not been established-about ACK, Sync packet, Readers are asked to check the TCP three handshake in TCP/IP, four breakup process)

if (connect)
Clientchannel.register (Selector,selectionkey.op_read,iohandler);
Else
Clientchannel.register (Selector,selectionkey.op_connect,iohandler);

5. Register the Op_connect status bit with the reactor thread's multiplexer and listen for the server's TCP ACK response.

Clientchannel.register (Selector,selectionkey.op_connect,iohandler);

6. Create a reactor thread, create a multiplexer, and start the thread.

Selector Selectot = selector.open (); New Thread (New Rectortask ()). Start ();

7. Multiplexer in the thread run method in the wireless loop to poll the ready key.

int num = selector.select (); Set Selectkeys = Selector.selectedkeys (); Iterator it = selectkeys.iterator (); while(It.hasnext) {      Selectionkey key = (Selectionkey) it.next;      /*     deal with  IO event    */ }     

8. Receive the Connect event for processing

if (key.isconnectable ())
Handlerconnect ();

9. Determine the connection result, if the connection is successful, register the read event to the Multiplexer

if (Channel.finishconnect ())
Registerread ();

10. Registering a read event to a multiplexer

Clientchannel.register (Selector,selectionkey.op_read,iohandler);

11. Asynchronously reads a client request message to a buffer

int readnumber = Channel.read (Receivedbuffer);

12. Encode and decode the bytebuffer, if there is a half-packet message pointer Reset, continue to read the subsequent messages, the decoding of the successful message encapsulated into a task, posted to the business thread pool, business logic processing.

Object message = null;        while (Buffer.hasremain ()) {            bytebuffer.mark ();            Object message = decode (bytebuffer);            if (Message==null) {                bytebuffer.reset ();                Break;            }            Messagelist.add (message);        }        if (!  Bytebuffer.hasremain ()) {bytebuffer.clear ();} else bytebuffer.compact (); if (Messagelist!=null &!  Messagelist.isempty ()) {for(Object messagef:messagelist) handletask (Messagee);}      

13. Encode the Pojo object into Bytebuffer and invokes the asynchronous write interface of Socketchannel to send the message asynchronously to the client.

Socketchannel.wite (buffer);

Note: The above client and server process, understand the line, the upper layer of code does not necessarily write, the specific reference can be run code.

The source code under Src/main/java/nio, divides into the client and the service side.

4.AIO

The concept of the new asynchronous channel is introduced in NIO2.0, and the implementation of the asynchronous socket channel of the async file channel H is provided.

The asynchronous channel provides 2 ways to get the result of the operation:

    • The result of an asynchronous operation is represented by the Java.util.concurrent.Futurn class;
    • A java.nio.channels is passed in when an asynchronous operation is performed.

The implementation class of the Completionhandler interface is the backtracking of the completion of the operation.

The NIO2.0 asynchronous socket channel, which corresponds to event-driven IO (AIO) in UNIX network programming, does not require a multiplexer (Selector) to poll the registered channel.

The source code under Src/main/java/aio, divides into the client and the service side.

5. Four IO comparisons

6.bio\ Pseudo-Asynchronous Io\nio\aio source Download

GitHub Address: Https://github.com/orange1438/Netty_Course

Netty's introduction to the topic

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.