Ensuring that the HTTP connection is closed when the rest call is finished.

This commit is contained in:
Inderjeet Singh 2010-09-30 12:28:11 +00:00
parent 1a4bea7334
commit ed415f692f

View File

@ -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();
} }
} }