title: BindException/Too many file open while using HttpClient under load
body: <p>I have got 1000 dedicated Java threads where each thread polls a corresponding url every one second. </p>
  
  <pre><code>public class Poller { 
      public static Node poll(Node node) { 
          GetMethod method =  null; 
          try { 
              HttpClient client = new HttpClient(new SimpleHttpConnectionManager(true)); 
              ......
          } catch (IOException ex) { 
              ex.printStackTrace(); 
          } finally { 
              method.releaseConnection(); 
          } 
      } 
  } 
  </code></pre>
  
  <p>The threads are run every one second: </p>
  
  <pre><code>for (int i=0; i &lt;1000; i++) { 
      MyThread thread = threads.get(i) // threads  is a static field 
      if(thread.isAlive()) { 
          // If the previous thread is still running, let it run. 
      } else { 
          thread.start(); 
      } 
  }
  </code></pre>
  
  <p>The problem is if I run the job every one second I get random exceptions like these: </p>
  
  <pre><code>java.net.BindException: Address already in use 
   INFO httpclient.HttpMethodDirector: I/O exception (java.net.BindException) caught when processing request: Address already in use 
   INFO httpclient.HttpMethodDirector: Retrying request 
  </code></pre>
  
  <p>But if I run the job every 2 seconds or more, everything runs fine. </p>
  
  <p>I even tried shutting down the instance of SimpleHttpConnectionManager() using shutDown() with no effect. </p>
  
  <p>If I do netstat, I see thousands of TCP connections in TIME_WAIT state, which means they are have been closed and are clearing up. </p>
  
  <p>So to limit the no of connections, I tried using a single instance of HttpClient and use it like this:</p>
  
  <pre><code>  public class MyHttpClientFactory { 
          private static MyHttpClientFactory instance = new HttpClientFactory(); 
          private MultiThreadedHttpConnectionManager connectionManager; 
          private HttpClient client; 
  
          private HttpClientFactory() { 
                  init(); 
          } 
  
          public static HttpClientFactory getInstance() { 
                  return instance; 
          } 
  
          public void init() { 
                  connectionManager = new MultiThreadedHttpConnectionManager(); 
                  HttpConnectionManagerParams managerParams = new HttpConnectionManagerParams(); 
                  managerParams.setMaxTotalConnections(1000); 
                  connectionManager.setParams(managerParams); 
                  client = new HttpClient(connectionManager); 
          } 
  
          public HttpClient getHttpClient() { 
                  if (client != null) { 
                          return client; 
                  } else { 
                      init(); 
                      return client; 
                  } 
          } 
  }
  </code></pre>
  
  <p>However after running for exactly 2 hours, it starts throwing 'too many open files' and eventually cannot do anything at all.</p>
  
  <pre><code>ERROR java.net.SocketException: Too many open files
  INFO httpclient.HttpMethodDirector: I/O exception (java.net.SocketException) caught when processing request: Too many open files
  INFO httpclient.HttpMethodDirector: Retrying request
  </code></pre>
  
  <p>I should be able to increase the no of connections allowed and make it  work, but I would just be prolonging the evil. Any idea what is the best practise to use HttpClient in a situation like above?</p>
  
  <p>Btw, I am still on HttpClient3.1.</p>