背景:今天有个需求是在发布之前检查一下模板中引用的静态资源是否被发布,然后用正则取出被引用的静态资源的url,然后用httpclient去请求,看返回码是否是404,如果是404,则表明这个静态资源是不存在的,即有问题,该模板不能发布。
验证的核心代码如下:
if (urls.size() > 0) {
HttpClient httpClient = null;
try {
httpClient = SimpleHttpsTrustClientTemplate.createHttpClient();
HttpGet httpGet = new HttpGet();
HttpResponse response = null;
for (Iterator<String> iter = urls.iterator(); iter.hasNext();) {
httpGet.setURI(URI.create(iter.next()));
response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NOT_FOUND) {
iter.remove();
}
}
} catch (Exception e) {
LOGGER.error(e);
}
}
测试就出现了“ConnectionPoolTimeoutException: Timeout waiting for connection”异常,应该是说连接没有释放,后来查看API,发现没有直接释放连接的方法,后来查了一下httpclient4中文版帮助文档(http://www.docin.com/p-564016244.html),说是要将响应流的内容消耗掉就会关闭连接,于是加了一行response.getEntity().getContent().close();重试,发现问题得以解决。
完整代码如下:
if (urls.size() > 0) {
HttpClient httpClient = null;
httpClient = SimpleHttpsTrustClientTemplate.createHttpClient();
HttpGet httpGet = new HttpGet();
HttpResponse response = null;
for (Iterator<String> iter = urls.iterator(); iter.hasNext();) {
httpGet.setURI(URI.create(iter.next()));
try {
response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NOT_FOUND) {
iter.remove();
}
} catch (ClientProtocolException e) {
LOGGER.error(e);
} catch (IOException e) {
LOGGER.error(e);
} finally {
try {
if (response != null) {
response.getEntity().getContent().close();
}
} catch (IllegalStateException e) {
LOGGER.error(e);
} catch (IOException e) {
LOGGER.error(e);
}
}
}
}