没有警告:
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
String apiUrl = ...;
HttpPost httpPost = new HttpPost(apiUrl);
CloseableHttpResponse response = null;
HttpEntity entity;
try {
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
logger.warn("invoke failed, response status={},key={}",statusCode,key);
httpPost.close();
if (response != null) {
response.close();
}
return null;
}
}
警告:潜在的资源泄漏:“此位置可能无法关闭响应”
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build();
String apiUrl = ...;
HttpPost httpPost = new HttpPost(apiUrl);
CloseableHttpResponse response = null;
HttpEntity entity;
try {
response = httpClient.execute(httpPost);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
logger.warn("invoke failed, response status={},key={}",statusCode,key);
closeBoth(httpClient, response)
return null;
}
}
private void closeBoth(CloseableHttpClient client, CloseableHttpResponse resp) {
org.apache.poi.util.IOUtils.closeQuitely(client);
org.apache.poi.util.IOUtils.closeQuitely(resp);
}
为什么第二种方法引起警告?
谢谢!
答案 0 :(得分:0)
Eclipse关闭警告代码仅查看当前方法。它不会分析closeBoth
方法来查看它的作用(在这种情况下,它也必须同时查看closeQuitely
)。