HttpClient-close connection

Source: Internet
Author: User

 
HttpClient client = new HttpClient ();
HttpMethod method = new GetMethod ("http://www.apache.org ");
Try {
Client.exe cuteMethod (method );
Byte [] responseBody = null;

ResponseBody = method. getResponseBody ();

} Catch (HttpException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Finally {
Method. releaseConnection ();

}
 
 

Most people use HttpClient to use code similar to the preceding example, including the official Apache example. Recently, when I used HttpClient, I found that when a large number of requests are sent to the server in a loop, the link of the APACHE server is full, and subsequent requests are waiting in queue.
 
APACHE configuration on my server
 
Timeout 30
KeepAlive On # indicates that the server does not close the link.
MaxKeepAliveRequests 100
KeepAliveTimeout 180
Therefore, this configuration will cause each link to be released after at least seconds. In this way, when a large number of requests are accessed, the link will inevitably be full and the request will wait.
After using DEBUH, we found that HttpClient did not close the link after method. releaseConnection (). This method only returned the link to connection manager. If you use HttpClient client = new HttpClient () to instantiate an HttpClient connection manager, SimpleHttpConnectionManager is used by default. SimpleHttpConnectionManager has the following constructor:
/**
* The connection manager created with this constructor will try to keep
* Connection open (alive) between consecutive requests if the alwaysClose
* Parameter is set to <tt> false </tt>. Otherwise the connection manager will
* Always close connections upon release.
*
* @ Param alwaysClose if set <tt> true </tt>, the connection manager will always
* Close connections upon release.
*/
Public SimpleHttpConnectionManager (boolean alwaysClose ){
Super ();
This. alwaysClose = alwaysClose;
}
 
You can see that if alwaysClose is set to true, connection manager will close the link after the link is released. When we instantiate a client like HttpClient client = new HttpClient (), connection manager is instantiated like this.
 
1 this. httpConnectionManager = new SimpleHttpConnectionManager ();
 
Therefore, alwaysClose defaults to false, and connection is not automatically closed. Therefore, we have a client to close the link.
 
Method 1:
 
Change the first line of Instantiation code in the case code to the following code. After method. releaseConnection (); connection manager closes the connection.
 
1 HttpClient client = new HttpClient (new HttpClientParams (), new SimpleHttpConnectionManager (true ));
 
Method 2:
 
Use the instantiation code: HttpClient client = new HttpClient ();
Add
 
1 (SimpleHttpConnectionManager) client. getHttpConnectionManager (). shutdown ();
 
Shutdown source code is very simple and clear at a glance
Public void shutdown (){
HttpConnection. close ();
}
 
Method 3:
 
Use the instantiation code: HttpClient client = new HttpClient ();
Add
Client. getHttpConnectionManager (). closeIdleConnections (0); the source code of this method is as follows:
Public void closeIdleConnections (long idleTimeout ){
Long maxIdleTime = System. currentTimeMillis ()-idleTimeout;
If (idleStartTime <= maxIdleTime ){
HttpConnection. close ();
}
}
 
Setting idleTimeout to 0 ensures that the link is closed.
The preceding three methods are the methods by which the client proactively closes the TCP link. Next we will introduce how to automatically close the link on the server.
 
Method 4:
 
Code implementation is very simple, and all the code is the same as the top case code. You only need to add a line of HTTP header settings in HttpMethod method = new GetMethod ("http://www.apache.org ");
 
1 method. setRequestHeader ("Connection", "close ");
 
Let's take a look at the definition of this attribute in HTTP:
HTTP/1.1 defines the "close" connection option for the sender to signal that the connection will be closed after completion of the response. For example,
Connection: close
 
Now let's talk about the difference between the client closing link and the server closing link. If you use the client to close the link, use the netstat-an command on the client machine to view many TCP connections of TIME_WAIT. If the server closes the link, this may occur on the server.
Refer to the instruction http://wiki.apache.org/HttpComponents/FrequentlyAskedConnectionManagementQuestions on WIKI
The TIME_WAIT state is a protection mechanic in TCP. The side that closes a socket connection orderly will keep the connection in state TIME_WAIT for some time, typically between 1 and 4 minutes.
The TIME_WAIT status appears at the end of the link that is closed. In TCP, The TIME_WAIT status is mainly used to ensure full data transmission. For details, refer to this document:
Http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-2.html#ss2.7
 
In addition, we emphasize that using the above methods to close the Link means that we can proactively close the link to release resources when we explicitly know that we do not need to reuse the link. If your application needs to reuse the link, there is no need to do so. Using the original link can also provide performance.

Author: ERDP Technical Architecture"

Related Article

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.