Ensuring that the HTTP connection is closed when the rest call is finished.
This commit is contained in:
parent
1a4bea7334
commit
ed415f692f
@ -65,12 +65,13 @@ public class RestClient {
|
|||||||
|
|
||||||
public <R> RestResponse<R> getResponse(
|
public <R> RestResponse<R> getResponse(
|
||||||
RestCallSpec<R> callSpec, RestRequest<R> request, Gson gson) {
|
RestCallSpec<R> callSpec, RestRequest<R> request, Gson gson) {
|
||||||
|
HttpURLConnection conn = null;
|
||||||
try {
|
try {
|
||||||
URL webServiceUrl = getWebServiceUrl(callSpec);
|
URL webServiceUrl = getWebServiceUrl(callSpec);
|
||||||
if (logger != null) {
|
if (logger != null) {
|
||||||
logger.log(logLevel, "Opening connection to " + webServiceUrl);
|
logger.log(logLevel, "Opening connection to " + webServiceUrl);
|
||||||
}
|
}
|
||||||
HttpURLConnection conn = (HttpURLConnection) webServiceUrl.openConnection();
|
conn = (HttpURLConnection) webServiceUrl.openConnection();
|
||||||
RestRequestSender requestSender = new RestRequestSender(gson, logLevel);
|
RestRequestSender requestSender = new RestRequestSender(gson, logLevel);
|
||||||
requestSender.send(conn, request);
|
requestSender.send(conn, request);
|
||||||
RestResponseReceiver<R> responseReceiver =
|
RestResponseReceiver<R> responseReceiver =
|
||||||
@ -80,6 +81,14 @@ public class RestClient {
|
|||||||
throw new WebServiceSystemException(e);
|
throw new WebServiceSystemException(e);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
throw new WebServiceSystemException(e);
|
throw new WebServiceSystemException(e);
|
||||||
|
} finally {
|
||||||
|
closeIgnoringErrors(conn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void closeIgnoringErrors(HttpURLConnection conn) {
|
||||||
|
if (conn != null) {
|
||||||
|
conn.disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user