HttpClient easy to ignore details--connection off
Blog Category:
Java code
- HttpClient client = new HttpClient ();
- HttpMethod method = New GetMethod ("http://www.apache.org");
- try {
- Client.executemethod (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 like the case code above, including the official Apache example. Recently I was using httpclient to find that a cycle of sending a large number of requests to the server would cause the Apache server's link to be full and subsequent requests queued.
Configuration of my server-side Apache
Java code
- Timeout
- KeepAlive on #表示服务器端不会主动关闭链接
- Maxkeepaliverequests
- KeepAliveTimeout
So this configuration will cause each link to at least 180S to be released, so that when the large number of requests to access will inevitably cause the link to be full, the request waits for the situation.
After passing Debuh, HttpClient is not closed after method.releaseconnection (), this method simply returns the link to Connection Manager. If you instantiate a HttpClient connection manager by using HttpClient client = new HttpClient (), the default implementation is to use Simplehttpconnectionmanager. Simplehttpconnectionmanager has a constructor as follows
Java code
- /**
- * The connection manager created with this constructor would try to keep the
- * Connection open (alive) between consecutive requests if the Alwaysclose
- * parameter is set to <tt>false</tt>. Otherwise the Connection Manager would
- * Always close connections upon release.
- *
- * @param alwaysclose if set <tt>true</tt>, the connection manager would always
- * Close connections upon release.
- */
- Public Simplehttpconnectionmanager (boolean alwaysclose) {
- super ();
- this.alwaysclose = alwaysclose;
- }
Look at the method note and we can see if Alwaysclose is set to true Connection Manager closes the chain after the link is released. Connection Manager is instantiated when we instantiate a client in our HttpClient client = new HttpClient ()
Java code
- This.httpconnectionmanager = new Simplehttpconnectionmanager ();
So alwaysclose default is that False,connection is not actively shut down, so we have a way for clients to close the link.
method One:
Change the first line instantiation code in the case code to the following, Method.releaseconnection (), and then Connection Manager closes connection.
Java code
- HttpClient client = new HttpClient (new Httpclientparams (),new Simplehttpconnectionmanager (true));
Method Two:
Instantiation code using: HttpClient client = new HttpClient ();
In Method.releaseconnection ();
Java code
- ((Simplehttpconnectionmanager) Client.gethttpconnectionmanager ()). Shutdown ();
Shutdown source code is very simple, looked at a glance
Java code
- Public void Shutdown () {
- Httpconnection.close ();
- }
Method Three:
Instantiation code using: HttpClient client = new HttpClient ();
In Method.releaseconnection ();
Client.gethttpconnectionmanager (). closeidleconnections (0); The source code for this method is as follows:
Java code
- Public void Closeidleconnections (long idleTimeout) {
- Long maxidletime = System.currenttimemillis ()-idleTimeout;
- if (idlestarttime <= maxidletime) {
- Httpconnection.close ();
- }
- }
Set IdleTimeout to 0 to ensure that the link is closed.
All of the above three methods are the way the client actively shuts down the TCP link. The following describes the method of automatically closing links by the server side.
method Four:
The code implementation is simple, and all the code is the same as the top case code. You only need to HttpMethod method = new GetMethod ("http://www.apache.org"), plus a line of HTTP header settings
Java code
- Method.setrequestheader ("Connection", "close");
Take a look at the definition of this attribute in the HTTP protocol:
http/1.1 defines the "Close" connection option for the sender of signal that the connection would be closed after Completio N of the response. For example,
Connection:close
Now again, the difference between a client close link and a server-side close link. Using the Netstat–an command on the client machine will see many time_wait TCP links if the client closes the link method. This occurs on the server side if the server-side actively closes the link.
Refer to the instructions on the wiki http://wiki.apache.org/HttpComponents/FrequentlyAskedConnectionManagementQuestions
The TIME_WAIT state was a protection mechanism in TCP. The side that closes a socket connection orderly would keep the connection in state time_wait for some time, typically BETW Een 1 and 4 minutes.
The status of the time_wait appears at the end of the active close link. The TIME_WAIT state in the TCP protocol is mainly to guarantee the complete transmission of the data. Refer to this document for details:
http://www.softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/unix-socket-faq-2.html#ss2.7
It is also emphasized that using these methods to close a link is a way to release a resource by actively closing the link in our application if you do not need to reuse the link explicitly. If your app needs to reuse links, there's no need to do so, and using legacy links can also provide performance.
[Go] Application Server Apachesocketunixthread