6

HttpClient连接的强制释放

  服务器上用 HttpClient 远程调用另一台服务器的一些资源,但是用 netstat 查看经常出现了很多的 CLOSE_WAIT 的连接,最后追查原因,是因为 HttpClient 的 method.releaseConnection() 并不是强制释放连接,为了减小连接数,使用了如下解决方案。在 HttpClient 完成请求后的 finally 块里面这么写。

 

    } finally {

           if (method != null) {

              try {

                  method.releaseConnection();

              } catch (Exception e) {

                  logger.error(“——-> Release HTTP connection exception:”, e);

              }

           }

           if (client != null) {

              try {

                  ((SimpleHttpConnectionManager) client.getHttpConnectionManager()).shutdown();

              } catch (Exception e) {

                  logger.error(“——-> Close HTTP connection exception:”, e);

              }

              client = null;

           }

       }

引用地址:http://www.steadyxp.com/archives/832.html

要说点啥就在这吧