一个基于AIO实现的简单web服务器
来源:互联网 发布:招商银行数据分析 编辑:程序博客网 时间:2024/04/30 20:40
一下是一个基于AIO实现的简单web服务器,这是一个简单例子
/** * 一个简单的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 str = "<html><head><title>test</title></head><body><p>this is a socketserver test</p></body></html>";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:" + str.length()+ CRLF;@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也这样的成品的产品也是9000左右而已,都是以静态相同页面为标注
不得不说,AIO的异步处理还是很强大的,不过可能在负载均衡处理控制上要比NIO差
0 0
- 一个基于AIO实现的简单web服务器
- 基于epoll实现的一个简单web服务器
- 基于Socket编程实现一个简单的Web服务器
- 基于erlang的简单Web服务器实现
- 一个简单web服务器的实现
- 一个简单web服务器的java实现
- Java 实现一个简单的web服务器
- java实现一个简单的Web服务器
- java实现一个简单的Web服务器
- 实现一个简单的web服务器
- 实现一个简单的Web服务器
- Java实现一个简单的Web服务器
- 一个简单的Java web服务器实现
- 一个简单的静态web服务器实现
- 基于tcp/ip的简单web服务器实现
- 自己实现一个简单的支持并发的Web服务器
- 一个简单的Web服务器
- 一个简单的web服务器
- File类的基本操作之读出全部文件夹路径
- Python内置的字符串处理函数整理
- ScrollView和ListView嵌套
- 关于html页面上的DOCTYPE
- 使用ATmega8L-8PU 在面包板上搭建自己的Arduino ,并闪亮一个LED
- 一个基于AIO实现的简单web服务器
- HDU 4707 Pet(搜索+乱搞,两种做法)
- 对于老站分析:影响网站排名不稳定的原因及其解决方法
- MFC中用ADO连接数据库
- Python之os.walk和os.path.walk
- java stream 学习笔记
- Android 常用开源控件收集
- <?php echo "<script language=javascript>alert('".$var['id']."!');</script>"; ?>
- css设置各种中文字体,雅黑,黑体,宋体,楷体等等