问题描述
我们有一个Spring Boot应用程序,该应用程序对一堆后端进行RESTFul调用,其中一个有时会返回空响应,并且由于这些在RestTemplate类中的代码,我们观察到在这些实例中连接没有释放:>
protected <T> T doExecute(URI url,HttpMethod method,RequestCallback requestCallback,ResponseExtractor<T> responseExtractor) throws RestClientException {
Assert.notNull(url,"'url' must not be null");
Assert.notNull(method,"'method' must not be null");
ClientHttpResponse response = null;
try {
ClientHttpRequest request = createRequest(url,method);
if (requestCallback != null) {
requestCallback.doWithRequest(request);
}
response = request.execute();
handleResponse(url,method,response);
if (responseExtractor != null) {
return responseExtractor.extractData(response);
}
else {
return null;
}
}
catch (IOException ex) {
String resource = url.toString();
String query = url.getRawQuery();
resource = (query != null ? resource.substring(0,resource.indexOf('?')) : resource);
throw new ResourceAccessException("I/O error on " + method.name() +
" request for \"" + resource + "\": " + ex.getMessage(),ex);
}
finally {
if (response != null) {
response.close();
}
}
}
有没有办法在响应为空或出错时释放连接或使用内容?
MyHttpClientClass{
private X getResponseBody(RestClient client,URI uri,httpentity<T> entity,Class<R> responseType,MyErrorHandler errorHandler) {
try
{ String host = this.getHost();
ResponseEntity<X> resp = client.exchange(uri,entity,responseType);
return resp.getBody();
} catch (HttpServerErrorException | HttpClientErrorException e)
{ handleHttpException(e,errorHandler);
throw e;
} catch (Exception e) {
log(e);
throw e; } } }
-----------
Class1 implements Callable<T>
{
@Override public T doCall() throws Exception {
try
{ return this.getRestClient().exchange(this.getUri(),this.getHttpMethod(),this.getEntity(),getResponseType()).getBody(); } catch (HttpClientErrorException ex) { throw ex; } catch (HttpStatusCodeException ex) { if(this.isNeededRetry(ex)) { throw ex; }else { return generateErrorResponse(ex).getBody(); } } catch (RestClientException ex) { throw ex; } catch (Exception ex) { throw ex; } } }
----------
MySpringApplicationClass{
public X get(String api,String params,String path,List<String> pathVariables,MyErrorHandler errorHandler)
{
return getResponseBody(...);
}}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)