A simple Web server based on AIO implementation

Source: Internet
Author: User

is a simple Web server based on AIO implementation, which is a simple example


/** * A simple Web server <br/> * Access via browser input localhost:8080/* * @author Joeson * @since 2014/05 * */public class Aioserver Implements Runnable{private Asynchronouschannelgroup Asyncchannelgroup;private Asynchronousserversocketchannel Server;public aioserver (int port) throws exception{//Create thread pool Executorservice executor = Executors.newfixedthreadpool (20) ;//Asynchronous Channel Manager Asyncchannelgroup = Asynchronouschannelgroup.withthreadpool (executor);//Create an asynchronous socket for use on the server. hereinafter referred to as the servers socket. The asynchronous channel Manager will use the server = Asynchronousserversocketchannel.open (asyncchannelgroup) for the relevant parameters used by the service side. Bind (New Inetsocketaddress (port));} public void Run () {try{//Specifies the receive operation object for the service-side socket. Accept prototype is://Accept (A attachment, completionhandler< asynchronoussocketchannel,//? Super a> handler)//This is the Completionhandler type a parameter is actually called the first parameter of the Accept method//IS listener. Another parameter, V, is the client socketserver.accept in the prototype (Server,new Completionhandler<asynchronoussocketchannel, Asynchronousserversocketchannel> () {string CRLF = "\ r \ n";//parameter of the response header String serverline = "Server:a siMple java WebServer "; String statusline = "http/1.1 OK" + CRLF; String contenttypeline = "content-type:text/html" + CRLF; String contentlengthline = "Content-length:" + 300+ CRLF; String str = "


The Web server concurrency performance based on AIO is higher than that of NiO and Netty implementations, and the main thing is that he is based on the Proactor IO processing model, which forwards the read and write operations to the right operating system.



(Of course, the static pages here are relatively small, transmission is relatively time-saving, but will not have a significant impact)

Server concurrency based on the implementation of Netty and NIO can be up to 67,000 request per second, but with Aio it can be 9000+ (labeled with my machine), and Tomcat is just 3-4000, all labeled as static pages


It has to be said that the asynchronous processing of AIO is still very powerful, but it may be worse than NIO in load balancing processing control




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.