服務(wù)器上用 HttpClient 遠(yuǎn)程調(diào)用另一臺(tái)服務(wù)器的一些資源,但是用 netstat 查看經(jīng)常出現(xiàn)了很多的 CLOSE_WAIT 的連接,最后追查原因,是因?yàn)?HttpClient 的 method.releaseConnection() 并不是強(qiáng)制釋放連接,為了減小連接數(shù),使用了如下解決方案。在 HttpClient 完成請(qǐng)求后的 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