【转】反应器(Reactor)模式

前端之家收集整理的这篇文章主要介绍了【转】反应器(Reactor)模式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Java NIO非堵塞技术实际是采取反应器模式,或者说是观察者(observer)模式为我们监察I/O端口,如果有内容进来,会自动通知我们,这样,我们就不必开启多个线程死等,从外界看,实现了流畅的I/O读写,不堵塞了。

同步和异步区别 : 有无通知(是否轮询)
堵塞和非堵塞区别 : 操作结果是否等待(是否马上又返回值),只是设计方式的不同。

NIO 有一个主要的类Selector,这个类似一个观察者,只要我们把需要探知的SocketChannel告诉Selector,我们接着做别的事情,当有事件发生时,他会通知我们,传回一组SelectionKey,我们读取这些Key,就会获得我们刚刚注册过的SocketChannel,然后,我们从这个Channel中读取数据,接着我们可以处理这些数据。

反应器模式与观察者模式在某些方面极为相似:当一个主体发生改变时,所有依属体都得到通知。不过,观察者模式与单个事件源关联,而反应器模式则与多个事件源关联 。


一般模型

我们想象以下情形:长途客车在路途上,有人上车有人下车,但是乘客总是希望能够在客车上得到休息。

传统的做法是:每隔一段时间(或每一个站),司机或售票员对每一个乘客询问是否下车。

反应器模式做法是:汽车是乘客访问的主体(Reactor),乘客上车后,到售票员(acceptor)处登记,之后乘客便可以休息睡觉去了,当到达乘客所要到达的目的地后,售票员将其唤醒即可。

  1. import java.io.IOException;
  2. import java.net.InetAddress;
  3. import java.net.InetSocketAddress;
  4. import java.nio.channels.SelectionKey;
  5. import java.nio.channels.Selector;
  6. import java.nio.channels.ServerSocketChannel;
  7. import java.util.Iterator;
  8. import java.util.Set;
  9.  
  10. /**
  11. * 反应器模式 用于解决用户访问并发问题
  12. *
  13. * 举个例子:餐厅服务问题
  14. *
  15. * 传统线程池做法:来一个客人(请求)去一个服务员(线程)
  16. * 反应器模式做法:当客人点菜的时候,服务员就可以去招呼其他客人了,等客人点好了菜,直接招呼一声:服务员
  17. *
  18. * @author linxcool
  19. */
  20. public class Reactor implements Runnable {
  21. public final Selector selector;
  22. public final ServerSocketChannel serverSocketChannel;
  23.  
  24. public Reactor(int port) throws IOException {
  25. selector = Selector.open();
  26. serverSocketChannel = ServerSocketChannel.open();
  27. InetSocketAddress inetSocketAddress = new InetSocketAddress(
  28. InetAddress.getLocalHost(),port);
  29. serverSocketChannel.socket().bind(inetSocketAddress);
  30. serverSocketChannel.configureBlocking(false);
  31.  
  32. // 向selector注册该channel
  33. SelectionKey selectionKey = serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);
  34.  
  35. // 利用selectionKey的attache功能绑定Acceptor 如果有事情,触发Acceptor
  36. selectionKey.attach(new Acceptor(this));
  37. }
  38.  
  39. @Override
  40. public void run() {
  41. try {
  42. while (!Thread.interrupted()) {
  43. selector.select();
  44. Set<SelectionKey> selectionKeys = selector.selectedKeys();
  45. Iterator<SelectionKey> it = selectionKeys.iterator();
  46. // Selector如果发现channel有OP_ACCEPT或READ事件发生,下列遍历就会进行。
  47. while (it.hasNext()) {
  48. // 来一个事件 第一次触发一个accepter线程
  49. // 以后触发SocketReadHandler
  50. SelectionKey selectionKey = it.next();
  51. dispatch(selectionKey);
  52. selectionKeys.clear();
  53. }
  54. }
  55. } catch (IOException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59.  
  60. /**
  61. * 运行Acceptor或SocketReadHandler
  62. *
  63. * @param key
  64. */
  65. void dispatch(SelectionKey key) {
  66. Runnable r = (Runnable) (key.attachment());
  67. if (r != null) {
  68. r.run();
  69. }
  70. }
  71.  
  72. }

  1. import java.io.IOException;
  2. import java.nio.channels.SocketChannel;
  3.  
  4. public class Acceptor implements Runnable {
  5. private Reactor reactor;
  6.  
  7. public Acceptor(Reactor reactor) {
  8. this.reactor = reactor;
  9. }
  10.  
  11. @Override
  12. public void run() {
  13. try {
  14. SocketChannel socketChannel = reactor.serverSocketChannel.accept();
  15. if (socketChannel != null)// 调用Handler来处理channel
  16. new SocketReadHandler(reactor.selector,socketChannel);
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }
  1. public class SocketReadHandler implements Runnable {
  2. private SocketChannel socketChannel;
  3.  
  4. public SocketReadHandler(Selector selector,SocketChannel socketChannel)
  5. throws IOException {
  6. this.socketChannel = socketChannel;
  7. socketChannel.configureBlocking(false);
  8.  
  9. SelectionKey selectionKey = socketChannel.register(selector,0);
  10.  
  11. // 将SelectionKey绑定为本Handler 下一步有事件触发时,将调用本类的run方法
  12. // 参看dispatch(SelectionKey key)
  13. selectionKey.attach(this);
  14.  
  15. // 同时将SelectionKey标记为可读,以便读取。
  16. selectionKey.interestOps(SelectionKey.OP_READ);
  17. selector.wakeup();
  18. }
  19.  
  20. /**
  21. * 处理读取数据
  22. */
  23. @Override
  24. public void run() {
  25. ByteBuffer inputBuffer = ByteBuffer.allocate(1024);
  26. inputBuffer.clear();
  27. try {
  28. socketChannel.read(inputBuffer);
  29. // 激活线程池 处理这些request
  30. // requestHandle(new Request(socket,btt));
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }

猜你在找的React相关文章