zoukankan      html  css  js  c++  java
  • the reactor pattern and java nio

     在《java NIO》作者PPT《How to Build a Scalable Multiplexed Server With NIO》 和 Doug Lea 《Scalable IO in Java》PPT中
     都有java nio的实现是通过reactor pattern 来实现的有说明。java nio作为一种跨平台IO操作。

    在不同平台上面封装了相应平台的IO模型。


     在reactor pattern 作者中已经提及,通过reactor pattern 模式能够来实现跨平台操作。

    所以,java nio通过reactor pattern模式就是这样完毕的。
     java nio在window 平台以下是使用Select 模型。

    对于java nio源码的分析。对于假设理解reactor pattern的设计模式意义不大。

    由于java nio实现中

     须要对JNI的封装。假设要了解对于不同平台的封装,能够通过ZThead库来了解会有更大的意义。

    这样能够避免对JNI 的干扰。

    由于JNI涉及到脚本语言java和C/C++交互的知识。

    How to Build a Scalable Multiplexed Server With NIO

    Reactor Pattern Mapped to NIO
    Handle
    SelectionKey
    Event
    SelectionKey.OP_READ, etc
    Demultiplexer
    Selector
    Dispatcher
    Selector.select() + iterate Selector.selectedKeys()
    Handler
    An instance of Runnable or Callable

    最简单样例:TestReactor.java


    public class TestReactor
    {
    	
       public static void main(String[] args) throws Exception 
       {
    	 
    	  //创建serversocketchannel通道.
    	  ServerSocketChannel serversocketchannel =ServerSocketChannel.open();
    	  //设置非堵塞,异步模式
    	  serversocketchannel.configureBlocking(false);
    	  //关联的serversocket
    	  ServerSocket serversocket = serversocketchannel.socket();
    	  SocketAddress endpoint =new InetSocketAddress("127.0.0.1", 8888);
    	  //绑定指定的port
    	  serversocket.bind(endpoint);
    	  //创建Selector。

    在Reactor Pattern模式中,相当于Demultiplexer 作用,用来多路复用器 Selector sel = Selector.open(); //在select中注冊链接事件。 //在reactor 模式中SelectionKey 相当于event事件。 //在SectionKey中存在OP_READ,OP_WRITE,OP_CONNECT,OP_ACCEPT 事件类型。此时与OP_ACCEPT 关联的Channel为ServerSocketChannel SelectionKey selKey = serversocketchannel.register(sel, SelectionKey.OP_ACCEPT); while(true) { //进行堵塞操作,等待事件的到来。返回值在select 模型中表示完毕操作的数目 int selCount = sel.select(); if(selCount>0) { System.out.println("selCount=>>"+selCount); } //返回能够操作的键集合。在window select 模型中,返回能够操作的fd_set集合 Set<SelectionKey> selKeySet = sel.selectedKeys(); for(SelectionKey key:selKeySet) { //在SelectionKey中,存在链接能够接受事件,则调用accept()函数就不会存在堵塞现象。

    //select if(key.isAcceptable()) { //获取与SelectionKey.OP_ACCEPT关联的通道。即ServerSocketChannel. ServerSocketChannel serverChannel = (ServerSocketChannel)key.channel(); //调用ServerSocketChannel 不会发生堵塞。

    获取到客户链接 SocketChannel socketchannel = serverChannel.accept(); //设置堵塞模式 socketchannel.configureBlocking(false); //关联SocketChannel的读和写事件 socketchannel.register(sel, SelectionKey.OP_READ|SelectionKey.OP_WRITE); //同一时候能够在SelectionKey中关联其它对象。在Select 模式中。Selectionkey 相当于Completionkey參数 } if(key.isWritable()) { SocketChannel socketchannel = (SocketChannel)key.channel(); ByteBuffer src =ByteBuffer.allocate(100); src.putInt(100); src.flip(); socketchannel.write(src); //关联SocketChannel的读和写事件 socketchannel.register(sel, SelectionKey.OP_READ); //同一时候能够在SelectionKey中关联其它对象。

    在Select 模式中,Selectionkey 相当于Completionkey參数 } if(key.isReadable()) { SocketChannel socketchannel = (SocketChannel)key.channel(); InetSocketAddress remote = (InetSocketAddress)socketchannel.getRemoteAddress(); String remotestring = remote.getHostString()+remote.getPort(); //关联SocketChannel的读和写事件 socketchannel.register(sel, SelectionKey.OP_WRITE); //同一时候能够在SelectionKey中关联其它对象。在Select 模式中,Selectionkey 相当于Completionkey參数 } selKeySet.remove(key); } } } }




  • 相关阅读:
    Docker最全教程之使用Tencent Hub来完成CI(九)
    程序员十大热门flag,有你的吗?
    互联网寒冬,阿里Ant Design还开坑,程序员该何去何从?
    Docker最全教程——从理论到实战(八)
    开源库Magicodes.Storage正式发布
    Docker最全教程——从理论到实战(七)
    开源库支付库Magicodes.Pay发布
    产品经理如何避免被程序员打?
    Docker最全教程——从理论到实战(六)
    如何解决input file 选取相同文件后,change事件不起作用解决方法
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6764182.html
Copyright © 2011-2022 走看看