【问题标题】:What will happened without shutting down the Apache HttpClient connectionManager?不关闭 Apache HttpClient connectionManager 会发生什么?
【发布时间】:2017-03-26 05:00:47
【问题描述】:

这个问题是在使用 apache HttpClients Connection Manager 发送 http 请求后收到result 出现的,如下所示。返回结果发生的次数很少。

private static final CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(configForRequest).build();
httpClient.execute...

这种情况下socket会发生什么情况,是不是每次收到请求后都要关闭Connect Manager呢?或者是否有适当的时间检查并关闭它?

【问题讨论】:

    标签: java apache http


    【解决方案1】:

    通常建议每个通信组件甚至每个应用程序都有一个 HttpClient 实例。但是,如果应用程序很少使用 HttpClient,并且不保证在内存中保留一个空闲的 HttpClient 实例,那么强烈建议在释放 HttpClient 实例之前显式关闭多线程连接管理器。这将确保正确关闭连接池中的所有 HTTP 连接。

    因此,如果您的应用程序遵循此标准,那么您只需要在启动应用程序时初始化 HttpClient 和 ConnectionManager,并在停止应用程序时将其关闭。

    每次调用方法时都创建这样的资源,不仅会降低应用程序的效率,而且容易出错,导致内存泄漏。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多