知识库--HttpConnector Class
2016-12-01 20:30
141 查看
The HttpConnector Class
What the important is:
how HttpConnector creates a server socket;
how it maintains a pool of HttpProcessor;//降低响应成本消耗
how it serves HTTP requests;
Creating a Server Socket
The initialize method of HttpConnector calls the open private method that returns an instance of java.net.ServerSocket and assigns it to serverSocket. However, instead of calling the java.net.ServerSocket constructor, the open method obtains an instance of ServerSocket from a server socket factory.//Factory design
Maintaining HttpProcessor Instances
In 3.0 the HttpConnector instance had only one instance of HttpProcessor at a time, so it can only process one HTTP request at a time. In the default connector, the HttpConnector has a pool of HttpProcessor objects and each instance of HttpProcessor has a thread of its own. Therefore, the HttpConnector can serve multiple HTTP requests simultaneously.
The HttpConnector maintains a pool of HttpProcessor instances to avoid creating HttpProcessor objects all the time. The HttpProcessor instances are stored in a java.io.Stack called processors:
In HttpConnector, the number of HttpProcessor instances created is determined by two variables: minProcessors and maxProcessors. By default, minProcessors is set to 5 and maxProcessors 20, but you can change their values through the set methods.
Initially, the HttpConnector object creates minProcessors instances of HttpProcessor. If there are more than the HttpProcessor instances can serve at a time, the HttpConnector creates more HttpProcessor instances until the number of instances reaches maxProcessors. After this point is reached and there are still not enough HttpProcessor instances, the incoming HTTP requests will be ignored. You can set maxProcessors to a negative number to kepp creating HttpProcessor instances. In addition, the curProcessors variable keeps the current number of Httpprocessor instances.
Here is the code that creates an initial number of HttpProcessor instances in the HttpConnector class’s start method:
The newProcessor method constructs a new HttpProcessor object and increments curProcessors. The recycle method pushes the HttpProcessor back to the stack.
Each HttpProcessor instance is responsible for parsing the HTTP request line and headers and populates a request object. Therefore, each instance is associated with a request object and a response object. The HttpProcessor class’s constructor contains calls to the HttpProcessor class’s createRequest and createResponse methods.
What the important is:
how HttpConnector creates a server socket;
how it maintains a pool of HttpProcessor;//降低响应成本消耗
how it serves HTTP requests;
Creating a Server Socket
The initialize method of HttpConnector calls the open private method that returns an instance of java.net.ServerSocket and assigns it to serverSocket. However, instead of calling the java.net.ServerSocket constructor, the open method obtains an instance of ServerSocket from a server socket factory.//Factory design
Maintaining HttpProcessor Instances
In 3.0 the HttpConnector instance had only one instance of HttpProcessor at a time, so it can only process one HTTP request at a time. In the default connector, the HttpConnector has a pool of HttpProcessor objects and each instance of HttpProcessor has a thread of its own. Therefore, the HttpConnector can serve multiple HTTP requests simultaneously.
The HttpConnector maintains a pool of HttpProcessor instances to avoid creating HttpProcessor objects all the time. The HttpProcessor instances are stored in a java.io.Stack called processors:
private Stack processors = new Stack();
In HttpConnector, the number of HttpProcessor instances created is determined by two variables: minProcessors and maxProcessors. By default, minProcessors is set to 5 and maxProcessors 20, but you can change their values through the set methods.
protected int minProcessors = 5; protected int maxProcessors = 20;
Initially, the HttpConnector object creates minProcessors instances of HttpProcessor. If there are more than the HttpProcessor instances can serve at a time, the HttpConnector creates more HttpProcessor instances until the number of instances reaches maxProcessors. After this point is reached and there are still not enough HttpProcessor instances, the incoming HTTP requests will be ignored. You can set maxProcessors to a negative number to kepp creating HttpProcessor instances. In addition, the curProcessors variable keeps the current number of Httpprocessor instances.
Here is the code that creates an initial number of HttpProcessor instances in the HttpConnector class’s start method:
while(curProcessors < minProcessors){ if((maxProcessors > 0 ) && (curProcessors >= maxProcessors)) break;//丢弃后面的连接 HttpProcessor processor = newProcessor(); recycle(processor); }
The newProcessor method constructs a new HttpProcessor object and increments curProcessors. The recycle method pushes the HttpProcessor back to the stack.
Each HttpProcessor instance is responsible for parsing the HTTP request line and headers and populates a request object. Therefore, each instance is associated with a request object and a response object. The HttpProcessor class’s constructor contains calls to the HttpProcessor class’s createRequest and createResponse methods.
相关文章推荐
- 知识库--The HttpProcessor Class
- Unknown default host [localhost] for connector [Connector[HTTP/1.1-8080]]
- HttpMessageNotWritableException: Could not write JSON: No serializer found for class ****
- java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be
- java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.spri
- java.lang.ClassCastException: org.apache.shiro.web.servlet.ShiroHttpServletRequest cannot be cast to
- Eclipse运行报错:HTTP Status 500 - javax.servlet.ServletException: java.lang.UnsupportedClassVersionError
- SEVERE: Error configuring application listener of class com.sun.xml.ws.transport.http.servlet.WSServ
- servlet程序HTTP Status 500 - Error instantiating servlet class 解决
- servlet程序HTTP Status 500 - Error instantiating servlet class 解决
- ClassNotFoundException:MappingJacksonHttpMessageConverter
- ClassLoader 转自---http://www.webspherechina.net/club/thread-1588-1-1.html WebSphere JVM classloader 内存问题的解决方法 --作者:好运北京
- Search Class - http://www.findjar.com/
- org.apache.catalina.connector.RequestFacade cannot be cast to MultipartHttpServletRequest 异常处理
- java.lang.UnsupportedClassVersionError: com/shiyi/http/Launcher : Unsupported major.minor version 52
- Tomcat7出现HTTP Status 500 - java.lang.ClassCastException: org.apache.jasper.el.ELContextImpl cannot b
- 继承AFHTTPSessionManager出现的_OBJC_CLASS错误解决
- 问题之HTTP Status 500 - Unable to compile class for JSP更优雅的解决方案
- http://www.net130.com/class/tech_handbook/tech_handbook01.html
- servlet程序HTTP Status 500 - Error instantiating servlet class 解决