代码之家  ›  专栏  ›  技术社区  ›  Langali

BindException/在加载下使用HttpClient时打开的文件太多

  •  5
  • Langali  · 技术社区  · 16 年前

    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(); 
            } 
        } 
    } 
    

    线程每秒钟运行一次:

    for (int i=0; i <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(); 
        } 
    }
    

    问题是,如果我每秒钟运行一次作业,就会出现如下随机异常:

    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 
    

    但是如果我每2秒或更长时间运行一次作业,那么一切都会正常运行。

    我甚至尝试使用shutDown()关闭SimpleHttpConnectionManager()的实例,但没有效果。

    如果我使用netstat,我会看到数千个TCP连接处于TIME\u WAIT状态,这意味着它们已经关闭并且正在清除。

    因此,为了限制连接的数量,我尝试使用HttpClient的一个实例,如下所示:

      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; 
                    } 
            } 
    }
    

    然而,在运行了整整2个小时后,它开始抛出“太多打开的文件”,最终什么也做不了。

    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
    

    我应该能够增加允许的连接数量并使其生效,但我只是在延长邪恶。您知道在上述情况下使用HttpClient的最佳实践是什么吗?

    3 回复  |  直到 14 年前
        1
  •  3
  •   Jim Ferrans    15 年前

    几个月前,这件事发生在我们身上。首先,仔细检查以确保每次都在调用releaseConnection()。但即使这样,操作系统也不会一次收回所有TCP连接。解决方案是使用ApacheHTTP客户端的 MultiThreadedHttpConnectionManager

    看见 http://hc.apache.org/httpclient-3.x/performance.html 有关更多性能提示。

    更新:哎呀,我没有读下面的代码示例。如果您正在执行RelaseCeNEngEnter(),并使用MultuthAddidHtpCnnnFutnMeal管理,请考虑您的操作系统是否限制每个进程的打开文件设置为足够高。我们也遇到了这个问题,需要稍微延长限制。

        2
  •  2
  •   ZZ Coder    16 年前

    第一个错误没有错。你只需要一个可用的端口。每个TCP连接可以保持时间等待状态2分钟。生成2000/秒。很快或稍后,套接字找不到任何未使用的本地端口,您将得到该错误。时间等待正是为此目的而设计的。如果没有它,您的系统可能会劫持以前的连接。

    第二个错误意味着打开的套接字太多。在某些系统上,打开的文件限制为1K。也许你只是因为套接字和其他打开的文件延迟而达到了这个极限。在Linux上,您可以使用

      ulimit -n 2048
    

    但这受到系统范围最大值的限制。

        3
  •  0
  •   Otto    14 年前

    *软文件65535 *硬文件65535 这会将打开的文件数设置为无限。