Reactor模式

文章来源于http://www.blogjava.net/DLevin/archive/2015/09/02/427045.html
Reactor模式实现

这是一个简单的例子来理解Reactor。

Client连接到Logging Server

  1. Logging Server注册LoggingAcceptor到InitiationDispatcher。
  2. Logging Server调用InitiationDispatcher的handle_events()方法启动。
  3. InitiationDispatcher内部调用select()方法(Synchronous Event Demultiplexer),阻塞等待Client连接。
  4. Client连接到Logging Server。
  5. InitiationDisptcher中的select()方法返回,并通知LoggingAcceptor有新的连接到来。
  6. LoggingAcceptor调用accept方法accept这个新连接。
  7. LoggingAcceptor创建新的LoggingHandler。
  8. 新的LoggingHandler注册到InitiationDispatcher中(同时也注册到Synchonous Event Demultiplexer中),等待Client发起写log请求。

Client向Logging Server写Log

  1. Client发送log到Logging server。
  2. InitiationDispatcher监测到相应的Handle中有事件发生,返回阻塞等待,根据返回的Handle找到LoggingHandler,并回调LoggingHandler中的handle_event()方法。
  3. LoggingHandler中的handle_event()方法中读取Handle中的log信息。
  4. 将接收到的log写入到日志文件、数据库等设备中。
    3.4步骤循环直到当前日志处理完成。
  5. 返回到InitiationDispatcher等待下一次日志写请求。
原文地址:https://www.cnblogs.com/noor/p/6195281.html