In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
What is the principle and structure of Reactor? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Although there is no real IO multiplexing model in Java, Reactor is a mode for NIO to implement multiplexing.
What is Reactor?
The Reactor design pattern is an event handling pattern for handling service requests that are delivered to a service handler at the same time through one or more inputs. The service handler then demultiplexes the incoming requests and distributes them synchronously to the associated request handler.
From the above statement, we can roughly sum up:
Based on the event driver, one or more input sources can be processed by ServiceHandler and the input events can be multiplexed and distributed to the corresponding RequestHandler (multiple) for processing.
Corresponding to the implementation in our NIO:
Synchronously wait for multiple event sources (Event) to arrive (using select () implementation) to demultiplex events and assign corresponding event services for processing. This dispatch adopts server centralized processing (Dispatch) decomposed events and corresponding event service applications separated from dispatching services (RequestHandler) Reactor structure
The main classes involved in the above Reactor model are:
The container of InitiationDispatcher:EventHandler, which is used to register, remove EventHandler, etc.; in addition, it invokes the select method of SynchronousEventDemultiplexer as the entry of Reactor mode to block the return of waiting events, and when the blocking events return, distribute the Handle of the events to the corresponding EventHandler processing. EvenHandler: defines the method of event handling. Handle: the handle in the operating system is an abstraction of resources at the operating system level. It can be an open file, a connection Socket, and so on. SynchronousEventDemultiplexer: use an event loop to block all resources. When resources on a synchronous operation can be started without blocking, the demultiplexer sends resources to the distributor Reactor sequence diagram initializes InitiationDispatcher and initializes a Handler to EventHandler Map to register EventHandler to InitiationDispatcher, each EventHandler contains a reference to Handler, thus establishing a Handler-to-EventHandler mapping calling InitiationDispatcher's handle_events () method to start EventLoop, calling select () method (SynchronousEventDemultiplexer) blocking in EventLoop to wait for Event to occur after the Event of one or some Handler occurs The select () method returns, InitiationDispatcher finds the registered EventHandler based on the returned Hanler, and calls back the handler_event method of the EventHandler to register a new EventHandler with the InitiationDispatcher in the EventHandler's handler_event () method. For example, for AcceptorEventHandler, when there is a new client connection, it generates a new EventHandler to process the new connection and registers it with the InitiationDispatcher. ReactorReactor single thread model
The most basic single-threaded Reactor model, Reactor is responsible for demultiplexing sockets (blocking and distributing when events are triggered). When new connections are triggered to CONNECT events, they are handed over to Accptor and IO read and write events to Handler.
Accceptor is responsible for obtaining the SocketChannel established by the client and binding the corresponding EventHandler. When an event occurs on the SocketChannel, the corresponding EventHandler can be obtained and processed.
Single Reactor multithreading model
Compared with the single Reactor model, after we get the IO events, we can hand it over to the thread pool, which can reduce the consumption of Reactor threads, and Reactor can pay more attention to the distribution of events.
The multi-Reactor multithreading model mainReactor is responsible for listening on the ServerSocket and is used to handle the establishment of new connections, which can usually be handled by a single thread, registering the established SocketChannel with subReactor. SubReactor (its number is usually the same as the number of CPU) to maintain its own selector, based on the mainReactor registered socketChannel demultiplexing IO read and write events, read and write network data, business processing functions, throw it to the worker thread pool to complete. The answer to the question about the principle and structure of Reactor is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.