You have probably run out of ephemeral ports . This occurs under load when many short-lived joints quickly open and close. The standard Java HttpURLConnection
will not give you the flexibility you need to set the correct socket options. I recommend going to the Apache HttpComponents project and setting options like ...
...
HttpGet httpGet = new HttpGet (uri);
HttpParams params = new BasicHttpParams ();
params.setParameter (CoreConnectionPNames.CONNECTION_TIMEOUT, 16 * 1000); // 16 seconds
params.setParameter (CoreConnectionPNames.SO_REUSEADDR, true); // <- teh MOJO!
DefaultHttpClient httpClient = new DefaultHttpClient (connectionManager, params);
BasicHttpContext httpContext = new BasicHttpContext ();
HttpResponse httpResponse = httpClient.execute (httpGet, httpContext);
StatusLine statusLine = httpResponse.getStatusLine ();
if (statusLine.getStatusCode ()> = HTTP_STATUS_CODE_300)
{
...
I missed some code like setupManager, but you can get it from your docs.
[Update] You can also add params.setParameter(CoreConnectionPNames.SO_LINGER, 1);
to keep the ephemeral ports from dragging on until regeneration.
source share