Reactor模式詳解

什么是Reactor模式

要回答這個問題,首先當然是求助Google或Wikipedia,其中Wikipedia上說:“The reactor design pattern is an event handling pattern for handling service requests delivered concurrently by one or more inputs. The service handler then demultiplexes the incoming requests and dispatches them synchronously to associated request handlers.”。從這個描述中,我們知道Reactor模式首先是事件驅(qū)動的,有一個或多個并發(fā)輸入源,有一個Service Handler,有多個Request Handlers;這個Service Handler會同步的將輸入的請求(Event)多路復(fù)用的分發(fā)給相應(yīng)的Request Handler。如果用圖來表達:


Reactor_Simple.png

從結(jié)構(gòu)上,這有點類似生產(chǎn)者消費者模式,即有一個或多個生產(chǎn)者將事件放入一個Queue中,而一個或多個消費者主動的從這個Queue中Poll事件來處理;而Reactor模式則并沒有Queue來做緩沖,每當一個Event輸入到Service Handler之后,該Service Handler會主動的根據(jù)不同的Event類型將其分發(fā)給對應(yīng)的Request Handler來處理。

更學術(shù)的,這篇文章(Reactor An Object Behavioral Pattern for Demultiplexing and Dispatching Handles for Synchronous Events)上說:“The Reactor design pattern handles service requests that are delivered concurrently to an application by one or more clients. Each service in an application may consistent of several methods and is represented by a separate event handler that is responsible for dispatching service-specific requests. Dispatching of event handlers is performed by an initiation dispatcher, which manages the registered event handlers. Demultiplexing of service requests is performed by a synchronous event demultiplexer. Also known as Dispatcher, Notifier”。這段描述和Wikipedia上的描述類似,有多個輸入源,有多個不同的EventHandler(RequestHandler)來處理不同的請求,Initiation Dispatcher用于管理EventHander,EventHandler首先要注冊到Initiation Dispatcher中,然后Initiation Dispatcher根據(jù)輸入的Event分發(fā)給注冊的EventHandler;然而Initiation Dispatcher并不監(jiān)聽Event的到來,這個工作交給Synchronous Event Demultiplexer來處理。

Reactor模式結(jié)構(gòu)

在解決了什么是Reactor模式后,我們來看看Reactor模式是由什么模塊構(gòu)成。圖是一種比較簡潔形象的表現(xiàn)方式,因而先上一張圖來表達各個模塊的名稱和他們之間的關(guān)系:


Reactor_Structures.png

Handle:即操作系統(tǒng)中的句柄,是對資源在操作系統(tǒng)層面上的一種抽象,它可以是打開的文件、一個連接(Socket)、Timer等。由于Reactor模式一般使用在網(wǎng)絡(luò)編程中,因而這里一般指Socket Handle,即一個網(wǎng)絡(luò)連接(Connection,在Java NIO中的Channel)。這個Channel注冊到Synchronous Event Demultiplexer中,以監(jiān)聽Handle中發(fā)生的事件,對ServerSocketChannnel可以是CONNECT事件,對SocketChannel可以是READ、WRITE、CLOSE事件等。

Synchronous Event Demultiplexer:阻塞等待一系列的Handle中的事件到來,如果阻塞等待返回,即表示在返回的Handle中可以不阻塞的執(zhí)行返回的事件類型。這個模塊一般使用操作系統(tǒng)的select來實現(xiàn)。在Java NIO中用Selector來封裝,當Selector.select()返回時,可以調(diào)用Selector的selectedKeys()方法獲取Set<SelectionKey>,一個SelectionKey表達一個有事件發(fā)生的Channel以及該Channel上的事件類型。上圖的“Synchronous Event Demultiplexer ---notifies--> Handle”的流程如果是對的,那內(nèi)部實現(xiàn)應(yīng)該是select()方法在事件到來后會先設(shè)置Handle的狀態(tài),然后返回。不了解內(nèi)部實現(xiàn)機制,因而保留原圖。

Initiation Dispatcher:用于管理Event Handler,即EventHandler的容器,用以注冊、移除EventHandler等;另外,它還作為Reactor模式的入口調(diào)用Synchronous Event Demultiplexer的select方法以阻塞等待事件返回,當阻塞等待返回時,根據(jù)事件發(fā)生的Handle將其分發(fā)給對應(yīng)的Event Handler處理,即回調(diào)EventHandler中的handle_event()方法。

Event Handler:定義事件處理方法:handle_event(),以供InitiationDispatcher回調(diào)使用。
Concrete Event Handler:事件EventHandler接口,實現(xiàn)特定事件處理邏輯。

Reactor模式模塊之間的交互

簡單描述一下Reactor各個模塊之間的交互流程,先從序列圖開始:


Reactor_Sequence.png
  1. 初始化InitiationDispatcher,并初始化一個HandleEventHandlerMap。
  2. 注冊EventHandlerInitiationDispatcher中,每個EventHandler包含對相應(yīng)Handle的引用,從而建立HandleEventHandler的映射(Map)。
  3. 調(diào)用InitiationDispatcherhandle_events()方法以啟動Event Loop。在Event Loop中,調(diào)用select()方法(Synchronous Event Demultiplexer)阻塞等待Event發(fā)生。
  4. 當某個或某些HandleEvent發(fā)生后,select()方法返回,InitiationDispatcher根據(jù)返回的Handle找到注冊的EventHandler,并回調(diào)該EventHandlerhandle_events()方法。
  5. EventHandlerhandle_events()方法中還可以向InitiationDispatcher中注冊新的Eventhandler,比如對AcceptorEventHandler來,當有新的client連接時,它會產(chǎn)生新的EventHandler以處理新的連接,并注冊到InitiationDispatcher中。

Reactor模式實現(xiàn)

Reactor An Object Behavioral Pattern for Demultiplexing and Dispatching Handles for Synchronous Events中,一直以Logging Server來分析Reactor模式,這個Logging Server的實現(xiàn)完全遵循這里對Reactor描述,因而放在這里以做參考。Logging Server中的Reactor模式實現(xiàn)分兩個部分:Client連接到Logging Server和Client向Logging Server寫Log。因而對它的描述分成這兩個步驟。
Client連接到Logging Server

Reactor_LoggingServer_connect.png

  1. Logging Server注冊LoggingAcceptorInitiationDispatcher。
  2. Logging Server調(diào)用InitiationDispatcher的handle_events()方法啟動。
  3. InitiationDispatcher內(nèi)部調(diào)用select()方法(Synchronous Event Demultiplexer),阻塞等待Client連接。
  4. Client連接到Logging Server
  5. InitiationDisptcher中的select()方法返回,并通知LoggingAcceptor有新的連接到來。
  6. LoggingAcceptor調(diào)用accept方法accept這個新連接。
  7. LoggingAcceptor創(chuàng)建新的LoggingHandler。
  8. 新的LoggingHandler注冊到InitiationDispatcher中(同時也注冊到Synchonous Event Demultiplexer中),等待Client發(fā)起寫log請求。
    Client向Logging Server寫Log
    Reactor_LoggingServer_log.png
  9. Client發(fā)送log到Logging server。
  10. InitiationDispatcher監(jiān)測到相應(yīng)的Handle中有事件發(fā)生,返回阻塞等待,根據(jù)返回的Handle找到LoggingHandler,并回調(diào)LoggingHandler中的handle_event()方法。
  11. LoggingHandler中的handle_event()方法中讀取Handle中的log信息。
  12. 將接收到的log寫入到日志文件、數(shù)據(jù)庫等設(shè)備中。
    3.4步驟循環(huán)直到當前日志處理完成。
  13. 返回到InitiationDispatcher等待下一次日志寫請求。

尊重原著,轉(zhuǎn)載出:http://www.blogjava.net/DLevin/archive/2015/09/02/427045.html
有關(guān)資料:http://www.dre.vanderbilt.edu/~schmidt/PDF/reactor-siemens.pdf

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容