When I recently studied NiO and looked at the Netty authoritative guide, JDK1.5 's NIO referred to the reactor model.
I took a look on the internet and found that the reactor pattern was raised quite a bit in the first place in the ACE(adaptivecommunication environment is a cross-platform C + + framework for concurrent communication). Used to synchronize non-blocking network traffic simplifies the development of event drivers, allowing event-driven applications to respond to events originating from many different event sources, such as I/O handles, timers , and signals.
When the JDK has not yet introduced NIO , only BIO(synchronous blocking) can be used in network programming, and the server will accept requests from multiple clients and then assign a thread to process a request. As Tomcat5.0 is using this approach to handle client requests (Connector/processor), just use the line pool to further optimize.
After the introduction of NIO, the reactor mode is used to realize the non-blocking socket communication via multiplexer selector.
Reactor mode
In a distributed system, especially a server-type event-driven application, multiple simultaneous requests need to be processed, and these requests have several different states, and how to handle these requests in an orderly and efficient manner requires that we meet the following points:
In order to improve the system's measurable and reaction time, the application can not block for a long time on an event source and stop processing other events, which can seriously reduce the responsiveness to the client.
To improve throughput, avoid any unnecessary context switches, synchronizations, and data movement between CPUs.
The introduction of new services or the improvement of existing services to the existing event separation and scheduling mechanism to bring the smallest possible impact.
• A large number of application code needs to be hidden behind complex multithreading and synchronization mechanisms.
To solve these problems, we need to separate the different events , different events are handled by different handler, a handler only deal with one type of event, and separate the dispatch and processing of the event , Distributed and dispatched centrally by a central administration.
Reactor (reactor) is this unified Management Center, responsible for receiving customer requests, separation ( Demultiplex ) different events, and dispatch ( Dispatch ) to the appropriate application ( Handler ) to handle.
In reactor mode, there are 5 key players:
1. Descriptor/operand (Handle): Provided by the operating system to identify each event, such as a socket descriptor, file descriptor, and so on. In Linux, it is represented by an integer. Events can come from outside, such as connection requests from the client, data, and so on. Events can also come from inside, such as timer events.
2. Synchronous Event Splitter (demultiplexer): is a function that waits for one or more events to occur. The caller is blocked until an event occurs on the descriptor set that separates the splitter. The Select function of Linux is a separator that is often used.
3. Event handler Interface (EventHandler): an interface consisting of one or more template functions. These template functions describe an application-related action on an event.
4. Specific event handler (Handler): is an implementation of the event handler interface. It implements a service provided by the application. The sum of each specific event handler is associated with a descriptor. It uses descriptors to identify events and identify the services provided by the application.
5. Reactor Manager (Reactor): Defines interfaces for application control event scheduling, as well as application registration, removal of event handlers, and associated descriptors. It is the dispatch core of the event handler. The reactor manager uses a synchronous event splitter to wait for an event to occur. Once the event occurs, the reactor manager separates each event, dispatches the event handler, and finally invokes the associated template function to handle the event.
From the above analysis, we note that it is the reactor manager rather than the application that is responsible for waiting for events, separating events, and dispatching events. In fact, the reactor manager is not called by a specific event handler, but rather the manager dispatches a specific event handler that is handled by the event handler for the event that occurs. This is the " reverse control " similar to the Hollywood principle. The only thing the application has to do is implement a specific event handler and register it with the reactor manager. The next work is done by the manager.
Reactor mode in Java
In Java, the implementation of the reactor pattern should be combined with the selector of NIO. For example, in a scenario where a server corresponds to multiple clients, first initialize the Serversocketchannel bound port and set it to non-blocking mode, then register the Serversocketchannel with selector and listen for SELECTIONKEY.OP_ The accept operation bit, and finally loops through the selector in run (), and takes out the ready Channel to dispatch to the handler processing of the response.
public class Reactor implements Runnable {public final Selector Selector; Public final Serversocketchannel Serversocketchannel; public Reactor (int port) throws IOException {selector = Selector.open (); Serversocketchannel =serversocketchannel.open (); Inetsocketaddress inetsocketaddress = new Inetsocketaddress (Inetaddress.getlocalhost (), port); Serversocketchannel.socket (). bind (inetsocketaddress); Serversocketchannel.configureblocking (FALSE); Serversocketchannel.register (selector, selectionkey.op_accept); } public void Run () {try {while (! Thread.interrupted ()) {selector.select (); set<selectionkey> Selectionkeys = Selector.selectedkeys (); Iterator<selectionkey> it = Selectionkeys.iterator (); while (It.hasnext ()) {Selectionkey Selectionkey = It.next (); Dispatch (Selectionkey); SelectiOnkeys.clear (); }}} catch (IOException e) {e.printstacktrace ();} } void Dispatch (Selectionkey key) {}}
Above the reactor reactor, through the separator selector to listen to different events (operands), and then the heard events assigned to the corresponding handler processing, as the role of the Management dispatch center.
The reactor pattern is very similar to the observer pattern in some ways: when a subject changes, all the dependent bodies are notified. Only the observer pattern is associated with a single event source, while the reactor pattern is associated with multiple event sources.
Reference: "Netty authoritative guide" and Bowen http://www.cnblogs.com/hzbook/archive/2012/07/19/2599698.html
Reactor reactor mode