您的位置:首页 > 理论基础 > 计算机网络

知识库--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:

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  socket
相关文章推荐