Reactor mode vs. Java NiO

Source: Internet
Author: User

?? Reactor is a pattern proposed by Schmidt, Douglas C, which is widely used in high concurrency server implementations.

The change mode uses event-driven mode, and when the event occurs, the corresponding event-handling code (Handler) is called.


This pattern is the basis for high concurrency server. such as Nginx and LIGHTTPD.

These two scenarios are ideal for large concurrency, but each with a very fast handling of requests.

The usual web interview is this feature.


structureContains 5 parts, handle and synchronous Event demultiplexer have an OS implementation.

Handles:is the network connection (connection), each network connection is represented by a handle.

Event (events)Accept_event: Indicates receipt of connection request Read_event: Indicates receipt of data write_event: Indicates that the socket can have enough buffers to write data to. Timeout_event: Timed out.

Java NIO does not support this event.

Signal_event: Signal.

Java NIO does not support this event.

Close_event: The socket is closed. Java NIO does not support this event.
Event HandlerThe event-handling code that is associated with the handle object instance (instance).

Each event handler handles events on a handle. There are at least two kinds of event handling code in a generic server: One is used to receive connection requests (accept event handler), in response to Accept_event. A request to handle the acceptance (Connection event handler), in response to Read_event and write_event.


Synchronous Event Demultiplexer (Incident multiplexing system)The core of the pattern waits for events on the handles. When no event occurs, it is blocked (blocking) and returned when there is time on some (some) handles.

This is implemented generally by the operating system provided. such as the Select, poll and Epoll of Linux.


Initiation DispatcherThe object is responsible for managing the event handlers (add, delete, etc.), and when an event appears on a handle, the corresponding internal use synchronous event is called Demultiplexer
In reactor's paper, the instance code logging_acceptor is the event handler that handles receiving requests.

Logging_handler is the event handlerinitiation_dispatcher::instance that handles the actual request is initiation Dispatcher of simple interest mode
Threading Model in this paper, a single-threaded model is used. That is, one thread handles both accepting and processing requests at the same time. In the model. Assume that each request has a longer processing time. Will affect the throughput rate of the system.
It is more common to use multithreaded models. The basic way of multithreading is 1:n.

1 threads are used to accept connection requests and run the Accept event handler. Other threads are used to process connection requests and execute connection event handler. The number of n can be adjusted dynamically according to the performance. Assume that each connection is handled very quickly. Maybe one thread is enough. Assuming that the processing time is very long, it is possible that a connection will have one thread.
Java NIOJava NIO is designed in accordance with the reactor pattern.

The main abstraction in Java NIO:

Selector: Equivalent to synchronous event Demultiplexerselectionkey: equivalent to event, and a Socketchannel association
Socketchannel: Equivalent to Handlejava NIO does not provide initial dispatcher abstraction, this part of the function needs to be implemented by users themselves. Java NIO does not provide an abstraction of event handler, which is required to be implemented by users themselves.
NettyAs Api,java NiO is not designed to be very comprehensive. will only provide the main functions. For the average user, Java NIO provides a programming interface that is relatively primitive (pirmitive) and not very easy to use. Netty is a network programming framework. Provides a higher level of abstraction on the basis of Java NIO. Netty's programming is considerably simpler than NIO.

All packages of reactor are provided in Netty, and the user only needs to implement the event handler in use.


Netty also provides a layer of abstraction, Channelpipeline and Channelhandler on the event handler. Channelpipeline is similar to the original event handler, which is used to process connection requests. The channelpipeline is made up of several channelhandler, and Channelpipeline calls the Channelhandler to process the connection request.

This design is similar to the filter chain in the servlet.

All the channelhandler together, forming the processing link.


Netty provides support for threading models in the same way, with detailed reference to Netty documentation.
the difference between Proactor and reactor The main difference is in how the connection request is handled: Reactor: After receiving the event, synchronize processing. After the request has not been completed. The thread will no longer process other requests. Proactor: Received event, asynchronous processing.

The request runs an asynchronous call (such as an asynchronous read-write file, and so on), and then the request call returns, and the thread continues to run other requests. However, the first request is not processed, and when the asynchronous operation is complete, there is time to notify the first request. Finished processing. The programming of proactor is more complicated.


Reactor mode vs. Java NiO

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.