概述
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版本)
- 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版本)
- 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();
- }
- }
- 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重新實作了,有興趣的同學可以去研究一下源碼。