在《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);
} } }
}

最新文章

  1. C++ 基础知识复习(六)
  2. 【WEB API项目实战干货系列】- 导航篇(十足干货分享)
  3. 详细解读Jquery各Ajax函数
  4. MySQL内置函数
  5. 向左对齐的Gallery
  6. Spring Data MongoDB example with Spring MVC 3.2
  7. Windows Phone 8初学者开发—第8部分:理解编译和部署
  8. dtree实现上下级关系的显示
  9. 设计模式--命令模式(Command)
  10. unity下跨平台excel读写
  11. CSS Sprites的优缺点
  12. 变量内容的删除、取代与替换 (Optional)
  13. 劫持 Opengl32.dll 实现游戏MOD
  14. Mysql常用命令 详细整理版
  15. 觉得一篇讲SPFA还不错的文章
  16. P1514 引水入城 DFS
  17. error 1044 (42000):access denied for user &#39;&#39;@&#39;localhost&#39; to database &#39;quickapp&#39; 解决方法
  18. 某浪PHP面试题及答案优化
  19. Zepto自定义模块打包构建
  20. Spring Cloud Zuul 1(API 网关服务)

热门文章

  1. linux题目整理(一)
  2. pythontips(1):打印模块的属性并执行
  3. RANSAC中迭代次数的计算
  4. CF978C Letters【前缀和+二分查找/几房几号】
  5. 树链剖分【p1505】[国家集训队]旅游
  6. 新建一个兼容eclipse和myeclipse、IDEA都兼容的项目结构(maven)
  7. 【spring boot】集成了druid后,同样的mybatis模糊查询语句出错Caused by: com.alibaba.druid.sql.parser.ParserException: syntax error, error in :&#39;name LIKE &#39;%&#39; ? &#39;%&#39;
  8. 今天在CSDN看懂这个帖子,也是我的困惑,记录一下(过了三十的码农,你选择的是哪个,说出你的想法)
  9. 去除自定义Toolbar中左边距
  10. [置顶] zabbix通过lykchat发送告警信息配置过程