HttpClient shutdown with TIME_WAIT
----------------------------------
Key: HTTPCLIENT-993
URL: https://issues.apache.org/jira/browse/HTTPCLIENT-993
Project: HttpComponents HttpClient
Issue Type: Bug
Affects Versions: 4.0.1
Environment: OS: OS X 10.6, RHE-4 (x86)
Java: 1.6
Reporter: Thomas Huang
I am new to HttpClient. I took the example directly from the HttpClient
javadoc by invoking it 10 times. The program ran fine, but I see 10 TIME_WAIT.
This suggests the example code is not closing the socket gracefully. The
problem with TIME_WAIT is the program will eventually hang if I increase the
number of iterations.
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.*;
public class TimeWait {
public static void test (String url) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response = httpclient.execute(httpget);
// Examine the response status
System.out.println(response.getStatusLine());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
InputStream instream = entity.getContent();
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(instream));
// do something useful with the response
System.out.println(reader.readLine());
} catch (IOException ex) {
// In case of an IOException the connection will be released
// back to the connection manager automatically
throw ex;
} catch (RuntimeException ex) {
// In case of an unexpected exception you may want to abort
// the HTTP request in order to shut down the underlying
// connection and release it back to the connection manager.
httpget.abort();
throw ex;
} finally {
// Closing the input stream will trigger connection release
instream.close();
}
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
public static void main (String[] args) throws Exception {
for (int i=0; i<10; ++i) {
TimeWait.test("http://www.apache.org");
}
}
}
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]