標籤:java nio aio web伺服器 伺服器
一下是一個基於AIO實現的簡單web伺服器,這是一個簡單例子
/** * 一個簡單的網頁伺服器<br/> * 通過瀏覽器輸入localhost:8080/訪問 * * @author Joeson * @since 2014/05 * */public class AioServer implements Runnable{private AsynchronousChannelGroup asyncChannelGroup;private AsynchronousServerSocketChannel server;public AioServer(int port) throws Exception{// 建立線程池ExecutorService executor = Executors.newFixedThreadPool(20);// 非同步通道管理器asyncChannelGroup = AsynchronousChannelGroup.withThreadPool(executor);// 建立 用在服務端的非同步Socket.以下簡稱伺服器socket。// 非同步通道管理器,會把服務端所用到的相關參數server = AsynchronousServerSocketChannel.open(asyncChannelGroup).bind(new InetSocketAddress(port));}public void run(){try{// 為服務端socket指定接收操作對象.accept原型是:// accept(A attachment, CompletionHandler<AsynchronousSocketChannel,// ? super A> handler)// 也就是這裡的CompletionHandler的A型參數是實際調用accept方法的第一個參數// 即是listener。另一個參數V,就是原型中的用戶端socketserver.accept(server,new CompletionHandler<AsynchronousSocketChannel, AsynchronousServerSocketChannel>(){String CRLF = "\r\n";// 回應標頭的參數String serverLine = "Server:a simple java WebServer";String statusLine = "HTTP/1.1 200 OK" + CRLF;String contentTypeLine = "Content-type:text/html"+ CRLF;String contentLengthLine = "Content-Length:" + 300+ CRLF;String str = "<html><head><title>test</title></head><body><p>this is a socketserver test</p></body></html>";@Overridepublic void completed(AsynchronousSocketChannel result,AsynchronousServerSocketChannel attachment){// TODO Auto-generated method stub// writeChannel(result, statusLine);// writeChannel(result, serverLine);// writeChannel(result, contentTypeLine);// writeChannel(result, contentLengthLine);// writeChannel(result, CRLF);writeChannel(result, statusLine + serverLine+ contentTypeLine + contentLengthLine+ CRLF + str);// writeChannel(result, str);try{result.shutdownOutput();result.shutdownInput();} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}try{result.close();} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}attachment.accept(attachment, this);}@Overridepublic void failed(Throwable exc,AsynchronousServerSocketChannel attachment){// TODO Auto-generated method stub}public void writeChannel(AsynchronousSocketChannel channel, String s){Future<Integer> future = channel.write(ByteBuffer.wrap(s.getBytes()));try{future.get();} catch (InterruptedException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (ExecutionException e){// TODO Auto-generated catch blocke.printStackTrace();}}});Thread.sleep(400000);} catch (Exception e){e.printStackTrace();} finally{System.out.println("finished server");}}public static void main(String... args) throws Exception{AioServer server = new AioServer(8080);new Thread(server).start();}}
基於AIO實現的web伺服器並發效能要比NIO以及Netty實現的伺服器並發要高,這最主要的還是他是基於Proactor的IO處理模型,把讀寫操作轉交右作業系統負責
(當然這裡靜態頁面比較小,傳輸上比較節約時間,但不會有很大影響)
基於Netty以及NIO的實現的伺服器並發可以達到每秒處理6-7千request,但是用AIO實現的話,那足可以上9000+(以我的機器為標註),而tomcat也只是3-4000而已,都是以靜態相同頁面為標註
不得不說,AIO的非同步處理還是很強大的,不過可能在負載平衡處理控制上要比NIO差