您的位置:首页 > 编程语言 > Java开发

JDK7新特性<八>异步io/AIO

2015-08-15 17:02 453 查看
概述

JDK7引入了Asynchronous I/O。I/O编程中,常用到两种模式:Reactor 和 Proactor。Reactor就是Java的NIO。当有事件触发时,我们得到通知,进行相应的处理。Proactor就是我们今天要讲的 AIO了。AIO进行I/O操作,都是异步处理,当事件完成时,我们会得到通知。

JDK7的 AIO包括网络和文件操作。两者大同小异,本文通过一个完整的客户端/服务器Sample来详细说明aio的网络操作。

AIO提供了两种异步操作的监听机制。第一种通过返回一个Future对象来事件,调用其get()会等到操作完成。第二种类似于回调函数。在进行异步操作时,传递一个CompletionHandler,当异步操作结束时,会调用CompletionHandler.complete 接口

范例

这个范例功能比较简单,就是客户端向服务端发送一个“test”命令,然后结束。

服务端程序 Sever.java

Java代码

import java.io.IOException;

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.AsynchronousServerSocketChannel;

import java.nio.channels.AsynchronousSocketChannel;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.Future;

import java.util.concurrent.TimeUnit;

import java.util.concurrent.TimeoutException;

public class Server {

private AsynchronousServerSocketChannel server;

public Server()throws IOException{

server = AsynchronousServerSocketChannel.open().bind(new InetSocketAddress(8888));

}

public void start() throws InterruptedException, ExecutionException, TimeoutException{

Future<AsynchronousSocketChannel> future = server.accept();

AsynchronousSocketChannel socket = future.get();

ByteBuffer readBuf = ByteBuffer.allocate(1024);

socket.read(readBuf).get(100, TimeUnit.SECONDS);

System.out.printf("Receiver:%s%n",new String(readBuf.array()));

}

public static void main(String args[]) throws Exception{

new Server().start();

}

}

客户端程序 (Future版本)

Java代码

import java.io.IOException;

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.AsynchronousSocketChannel;

import java.util.concurrent.ExecutionException;

public class AIOClientWithFuture {

private final AsynchronousSocketChannel client;

public AIOClientWithFuture() throws IOException{

client = AsynchronousSocketChannel.open();

}

public void sendMsg() throws InterruptedException, ExecutionException{

client.connect(new InetSocketAddress("localhost",8888));

client.write(ByteBuffer.wrap("test".getBytes())).get();

}

public static void main(String...args) throws Exception{

AIOClientWithFuture client = new AIOClientWithFuture();

client.sendMsg();

}

}

客户端程序(CompleteHandler版本)

Java代码

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.AsynchronousSocketChannel;

import java.nio.channels.CompletionHandler;

public class AIOClientWithHandler {

private final AsynchronousSocketChannel client ;

public AIOClientWithHandler() throws Exception{

client = AsynchronousSocketChannel.open();

}

public void start()throws Exception{

client.connect(new InetSocketAddress("127.0.0.1",8888),null,new CompletionHandler<Void,Void>() {

@Override

public void completed(Void result, Void attachment) {

try {

client.write(ByteBuffer.wrap("test".getBytes())).get();

} catch (Exception ex) {

ex.printStackTrace();

}

}

@Override

public void failed(Throwable exc, Void attachment) {

exc.printStackTrace();

}

});

}

public static void main(String args[])throws Exception{

new AIOClientWithHandler().start();

}

}

相关类说明

AsynchronousSocketChannel 跟 SocketChannel操作类似,只不过改成异步接口了

AsynchronousServerSocketChannel跟ServerSocketChannel操作类似,只不过改成异步接口了

CompletionHandler 接口包括两个方法

void completed(V result, A attachment);

void failed(Throwable exc, A attachment);

总结

本文只是对jdk7的aio使用做了一个简单的说明。至于其性能提升多少,如何改进现有的网络应用程序,还在摸索中。这里推荐一个比较成熟的网络框架Project Grizzly:http://grizzly.dev.java.net。据说已经用aio重新实现了,有兴趣的同学可以去研究一下源码。

参考资料

(以下有些资料使用的jdk7版本太低,很多接口已经更改了,慎入!:)

http://www.ibm.com/developerworks/java/library/j-nio2-1/index.html?

http://www.iteye.com/topic/446298

http://www.iteye.com/topic/472333

本文是jdk7系列的终结了,感谢大家的支持!更多的内容可以访问我的blog

http://www.iamcoding.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: