Netty, netty

Source: Internet
Author: User
Tags epoll

Netty, netty

This article introduces four types of IO: Java BIO (synchronous blocking IO), pseudo asynchronous IO, NIO (non-blocking IO), and AIO (asynchronous IO), and compares different IO models.

Directory

1. BIO

2. pseudo asynchronous IO

3. NIO

4. AIO

5. Comparison of four types of IO

6. BIO \ pseudo asynchronous IO \ NIO \ AIO source code download

 

1. BIO

A server using BIO communication model is usually monitored by an independent Acceptor thread for client connection. After receiving client connection requests, it creates a new thread process link connection process for each client, after processing, the output stream is returned to the client and the thread is destroyed.

2. pseudo asynchronous IO

To solve the problem of synchronous blocking I/O, a link requires a thread to process. Now, the concept of "pool" is introduced and the thread pool is added.

When a new client connection is established, the client Socket is encapsulated as a Task (implemented by the java Runnable interface) and delivered to the backend thread pool for processing. Because the thread pool can set the Message Queue size and the maximum number of threads, its resources are controllable. No matter how many clients access the queue concurrently, it will not cause resource depletion or downtime.

3. NIO

NIO library is introduced in JDK1.4, and nio makes up for the shortcomings of synchronous blocking IO. In all the data, NIO is processed using a Buffer. when accessing the data in NIO at any time, it is operated through the Buffer. The buffer is actually an array. The basis of Java NIO is the multipleaser Selector. In short, selector will continuously round the Channel registered on it (Channel, full duplex ), if a Channel has a new TCP connection access and read/write event, the Channel will be in the ready state and will be poll by Selector. Then, the select set can be obtained through SelectionKey, perform subsequent IO operations.

One multiplexing can round-robin multiple channels at the same time, and because jdk uses epoll instead of select implementation, there is no limit on the maximum connection handle. (In other words, eopll and select are the IO multiplexing in linux. Similar to select and epoll, you can directly view the source code for clear process concepts ).

NIO server sequence diagram

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 multiplexing instance, and start the thread.

Selector selectot = Selector.open();new Thread(new RectorTask()).start();

4. register the SelectSocketChannel to the selector of the Reactor thread to listen for the accept event.

SelectionKey key = acceptorSvr.register(selector,SelectionKey.OP_ACCEPT,ioHandler);

5. In the wireless loop of the thread run method, the multiplexing round-robin 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    */    }

6. multiplexing monitors new user access, processes new access requests, completes TCP three-way handshakes, and establishes physical connections.

SocketChannel sc = ssc.accept();

7. Set the client link to non-blocking mode

sc.configureBlocking(false);sc.socket().setReuseAddress(true);...

8. register the newly connected client connection to the Reactor thread multiplexing and listen for read operations to read network messages sent by the client.

SelectionKey key = sc.register(selector,SelectionKey.OP_READ,ioHangler);

9. asynchronously read client request messages to the buffer zone

int readNumber = channel.read(receivedBuffer);

10. codec the bytebuffer. If there is a half packet message pointer reset, continue to read the subsequent packets, encapsulate the decoded messages into tasks, and deliver them to the business thread pool for 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);        }

11. encode the pojo object into bytebuffer, call the asynchronous write interface of SocketChannel, and send messages to the client asynchronously.

socketChannel.wite(buffer);

Note: If the sending zone's TCP buffer is full, half a packet will be written. At this time, you need to register the write operation bit and write it cyclically until the entire packet message is written to the TCP buffer zone.

 

NIO client sequence diagram (mostly similar to the server)

SocketChannel clientChannel = SocketChannel. open ();

2. Set SocketChannel to non-blocking mode and set TCP Parameters for connection.

SocketChannel.configureBlocking(false);
socket.setReuseAddress(true);
socket.setReceiveBufferSize(BUFFER_SIZE);
socket.setSendBufferSize(BUFFER_SIZE);

3. Connect to the server asynchronously.

boolean connected = clientChannel.connect(new InetSocketAdress("ip",port));

4. determine whether the connection is successful. If the connection is successful, the read status bit is directly registered to the multiplexing. If the connection fails (asynchronous connection, false is returned, indicating that the client has sent the sync package, the server does not return ack packets, and physical connections have not yet been established. For ack and sync packets, read the three-way handshake of TCP in TCP/IP and the process of breaking up four times)

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 multiplexing to listen to the tcp ack response of the server.

clientChannel.register(selector,SelectionKey.OP_CONNECT,ioHandler);

6. Create a Reactor thread, create a multiplexing instance, and start the thread.

Selector selectot = Selector.open();new Thread(new RectorTask()).start();

7. Multiplex round-robin in the wireless loop of the thread run method to round-robin 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 handling

if(key.isConnectable())
  //handlerConnect();

9. Determine the connection result. If the connection is successful, register the read event to the multiplex.

if(channel.finishConnect())
  registerRead();

10. Register read events to multiplex

clientChannel.register(selector,SelectionKey.OP_READ,ioHandler);

11. asynchronously read client request messages to the buffer zone

int readNumber = channel.read(receivedBuffer);

12. codec the bytebuffer. If there is a half packet message pointer reset, continue to read the subsequent packets, encapsulate the decoded messages into tasks, and deliver them to the business thread pool for 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, call the asynchronous write interface of SocketChannel, and send messages to the client asynchronously.

socketChannel.wite(buffer);

 

Note: You can understand the above client and server processes. The upper-Layer Code may not be written in this way. For details, refer to the code that can be run.

The source code is in src/main/java/NIO and is divided into client and server.

4. AIO

NIO2.0 introduces the concept of a new asynchronous channel and provides the implementation of the asynchronous file channel h-amount asynchronous socket channel.

Asynchronous channel provides two methods to obtain operation results:

  • Use the java. util. concurrent. Futurn class to represent the result of asynchronous operations;
  • Input a java. nio. channels.

The implementation class of the CompletionHandler interface serves as the backtracking of Operation completion.

The asynchronous socket channel of NIO2.0 corresponds to the event-driven IO (AIO) in UNIX network programming. It does not need to round-robin The registered channel through the Selector.

The source code is in src/main/java/AIO and can be divided into client and server.

 

5. Comparison of four types of IO

6. BIO \ pseudo asynchronous IO \ NIO \ AIO source code download

GitHub address: https://github.com/orange1438/Netty_Course

 

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.