Netty, Java NIO working principle and introduction

Netty, Java NIO working principle and introduction

Essence: a Jar package made by JBoss
Purpose : Rapid development of high-performance, high-reliability network server and client programs
Advantages: Provide asynchronous, event-driven network application frameworks and tools
Popular Said: an easy to handle Socket stuff

1. Netty is a Java NIO framework program based on SocketChannel (TCP network IO) and ServerSocketChannel (TCP network IO) implemented on the basis of Java NIO, which is convenient and fast Develop high-performance, high-reliability network server and client programs.
2. To put it bluntly, it is a working mode of Java NIO, but it is packaged in a jar package that is more convenient to use.


Java NIO
1. NIO is an application-level asynchronous process.
2. The io method will block the application. The application will block until the result is returned. To achieve multi-connection processing, you must use multi-threading (one connection corresponds to one thread), but creating threads consumes memory. , which affects performance.
3. NIO is to complete many connections through a single thread or several threads, reducing the memory consumption of creating a large number of threads.
4. When NIO receives a connection, the connection becomes a model (Channels) and registers with the manager (Selectors) that manages this model (Channels). When there is data to interact with this connection, the manager (Selectors) will The model (Channels) will be notified, and the handler function or interface defined by the callback model (Channels) will be notified.
5. In this way, a working mode in which one thread handles multiple connections can be implemented on the server side.
6. NIO is generally used when the connection is a small amount of data interaction. If the connection is for a large amount of data interaction, it is not suitable for this mode (data conference and callback take a long time (affecting interactive response performance))
7. NIO support There are many Channels: FileChannel (file IO), DatagramChannel (UDP network IO), SocketChannel (TCP network IO), ServerSocketChannel (TCP network IO)


Java NIO provides different IO working methods from standard IO:
1.Channels and Buffers ( Channels and buffers): Standard IO operates on byte streams and character streams, while NIO operates on channels and buffers, and data is always read from channels into buffers, or Write from the buffer to the channel.
2. Asynchronous IO (Asynchronous IO): Java NIO allows you to use IO asynchronously, for example: when the thread reads data from the channel to the buffer, the thread can still do other things. While data is being written to the buffer, the thread can continue processing it. Writing to a channel from a buffer is similar.
3.Selectors: Java NIO introduces the concept of selectors, which are used to listen to events on multiple channels (such as connection open, data arrival). Therefore, a single thread can listen to multiple data channels.


Java NIO consists of the following core parts: Channels, Buffers, Selectors
Channel and Buffer
1. All IO in NIO starts from a Channel. Channel is a bit like a stream. Data can be read from Channel to Buffer, or written from Buffer to Channel.

Selector
1.Selector allows a single thread to process multiple Channels. Using Selector is handy if your app has multiple connections (channels) open, but each connection has low traffic.
2. To use Selector, register Channel with Selector, and then call its select() method. This method will block until a registered channel has an event ready. Once this method returns, the thread can process these events. The


working principle is as follows:
1. NIO is a working mode based on event notification implemented by the application layer. The application layer listens to the port. When data arrives at the port, the system will call the application layer. callback to notify the application.
2. The application implements the data parsing in the notification processing and notifies the data to the notification processing interface (that is, the callback) registered in the application.
3. Selectors is the callback function of the application (system-level), Channels and Buffers are to be registered with Selectors (of course, it also has its callback (application-level)). When Selectors receives data, it polls the Channels registered in it. Call the callback (application-level) function for processing if required.


Reference text (Netty application example): http://lippeng.iteye.com/blog/1907279
Reference text (Netty application example): http://blog.csdn.net/kobejayandy/article/details/11493717
Reference text (Java NIO): http://www.iteye.com/magazines/132-Java-NIO
reference original text (java nio usage example): http://www.jb51.net/article/48520.htm
Refer to the original text (example of the use of java nio): http://www.cnblogs.com/gaotianle/p/3325451.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326931389&siteId=291194637