HttpClient needs releaseConnection method call

reviewboard: https://reviews.apache.org/r/8186/

Signed-off-by: Hugo Trippaers <trippie@gmail.com>
This commit is contained in:
Hiroaki Kawai 2012-11-29 20:23:30 +01:00 committed by Hugo Trippaers
parent fefec372a2
commit a28f4cac3c
5 changed files with 57 additions and 38 deletions

View File

@ -231,6 +231,7 @@ public class AgentShell implements IAgentShell {
int response; int response;
response = client.executeMethod(method); response = client.executeMethod(method);
if (response != HttpURLConnection.HTTP_OK) { if (response != HttpURLConnection.HTTP_OK) {
method.releaseConnection();
s_logger.warn("Retrieving from " + url + " gives response code: " s_logger.warn("Retrieving from " + url + " gives response code: "
+ response); + response);
throw new CloudRuntimeException("Unable to download from " + url throw new CloudRuntimeException("Unable to download from " + url
@ -253,6 +254,7 @@ public class AgentShell implements IAgentShell {
s_logger.warn("Exception while closing download stream from " s_logger.warn("Exception while closing download stream from "
+ url + ", ", e); + url + ", ", e);
} }
method.releaseConnection();
} }
private void loadProperties() throws ConfigurationException { private void loadProperties() throws ConfigurationException {

View File

@ -58,6 +58,7 @@ import com.caringo.client.ScspResponse;
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
/** /**
@ -413,23 +414,26 @@ public class S3CAStorBucketAdapter implements S3BucketAdapter {
} }
public class ScspDataSource implements DataSource { public class ScspDataSource implements DataSource {
GetMethod method; String content_type = null;
public ScspDataSource(GetMethod m) { byte content[] = null;
method = m; public ScspDataSource(GetMethod method) {
Header h = method.getResponseHeader("Content-type");
if (h != null) {
content_type = h.getValue();
}
try{
content = method.getResponseBody();
}catch(IOException e){
s_logger.error("CAStor loadObjectRange getInputStream error", e);
}
} }
@Override @Override
public String getContentType() { public String getContentType() {
Header h = method.getResponseHeader("Content-type"); return content_type;
return h==null ? null : h.getValue();
} }
@Override @Override
public InputStream getInputStream() throws IOException { public InputStream getInputStream() {
try { return new ByteArrayInputStream(content);
return method.getResponseBodyAsStream();
} catch (Exception e) {
s_logger.error("CAStor loadObjectRange getInputStream error", e);
return null;
}
} }
@Override @Override
public String getName() { public String getName() {
@ -445,21 +449,27 @@ public class S3CAStorBucketAdapter implements S3BucketAdapter {
@Override @Override
public DataHandler loadObjectRange(String mountedRoot, String bucket, String fileName, long startPos, long endPos) { public DataHandler loadObjectRange(String mountedRoot, String bucket, String fileName, long startPos, long endPos) {
HttpClient httpClient = new HttpClient(s_httpClientManager);
// Create a method instance.
GetMethod method = new GetMethod(castorURL(mountedRoot, bucket, fileName));
method.addRequestHeader("Range", "bytes=" + startPos + "-" + endPos);
int statusCode;
try { try {
HttpClient httpClient = new HttpClient(s_httpClientManager); statusCode = httpClient.executeMethod(method);
// Create a method instance. } catch (HttpException e) {
GetMethod method = new GetMethod(castorURL(mountedRoot, bucket, fileName)); s_logger.error("CAStor loadObjectRange failure", e);
method.addRequestHeader("Range", "bytes=" + startPos + "-" + endPos); throw new FileNotExistException("CAStor loadObjectRange failure: " + e);
int statusCode = httpClient.executeMethod(method); } catch (IOException e) {
if (statusCode < HTTP_OK || statusCode >= HTTP_UNSUCCESSFUL) {
s_logger.error("CAStor loadObjectRange response: "+ statusCode);
throw new FileNotExistException("CAStor loadObjectRange response: " + statusCode);
}
return new DataHandler(new ScspDataSource(method));
} catch (Exception e) {
s_logger.error("CAStor loadObjectRange failure", e); s_logger.error("CAStor loadObjectRange failure", e);
throw new FileNotExistException("CAStor loadObjectRange failure: " + e); throw new FileNotExistException("CAStor loadObjectRange failure: " + e);
} }
if (statusCode < HTTP_OK || statusCode >= HTTP_UNSUCCESSFUL) {
s_logger.error("CAStor loadObjectRange response: "+ statusCode);
throw new FileNotExistException("CAStor loadObjectRange response: " + statusCode);
}
DataHandler ret = new DataHandler(new ScspDataSource(method));
method.releaseConnection();
return ret;
} }
@Override @Override

View File

@ -119,6 +119,8 @@ public class NiciraNvpApi {
throw new NiciraNvpApiException("Nicira NVP API login failed ", e); throw new NiciraNvpApiException("Nicira NVP API login failed ", e);
} catch (IOException e) { } catch (IOException e) {
throw new NiciraNvpApiException("Nicira NVP API login failed ", e); throw new NiciraNvpApiException("Nicira NVP API login failed ", e);
} finally {
pm.releaseConnection();
} }
if (pm.getStatusCode() != HttpStatus.SC_OK) { if (pm.getStatusCode() != HttpStatus.SC_OK) {
@ -322,10 +324,11 @@ public class NiciraNvpApi {
if (pm.getStatusCode() != HttpStatus.SC_OK) { if (pm.getStatusCode() != HttpStatus.SC_OK) {
String errorMessage = responseToErrorMessage(pm); String errorMessage = responseToErrorMessage(pm);
pm.releaseConnection();
s_logger.error("Failed to update object : " + errorMessage); s_logger.error("Failed to update object : " + errorMessage);
throw new NiciraNvpApiException("Failed to update object : " + errorMessage); throw new NiciraNvpApiException("Failed to update object : " + errorMessage);
} }
pm.releaseConnection();
} }
private <T> T executeCreateObject(T newObject, Type returnObjectType, String uri, Map<String,String> parameters) throws NiciraNvpApiException { private <T> T executeCreateObject(T newObject, Type returnObjectType, String uri, Map<String,String> parameters) throws NiciraNvpApiException {
@ -352,6 +355,7 @@ public class NiciraNvpApi {
if (pm.getStatusCode() != HttpStatus.SC_CREATED) { if (pm.getStatusCode() != HttpStatus.SC_CREATED) {
String errorMessage = responseToErrorMessage(pm); String errorMessage = responseToErrorMessage(pm);
pm.releaseConnection();
s_logger.error("Failed to create object : " + errorMessage); s_logger.error("Failed to create object : " + errorMessage);
throw new NiciraNvpApiException("Failed to create object : " + errorMessage); throw new NiciraNvpApiException("Failed to create object : " + errorMessage);
} }
@ -361,6 +365,8 @@ public class NiciraNvpApi {
result = (T)gson.fromJson(pm.getResponseBodyAsString(), TypeToken.get(newObject.getClass()).getType()); result = (T)gson.fromJson(pm.getResponseBodyAsString(), TypeToken.get(newObject.getClass()).getType());
} catch (IOException e) { } catch (IOException e) {
throw new NiciraNvpApiException("Failed to decode json response body", e); throw new NiciraNvpApiException("Failed to decode json response body", e);
} finally {
pm.releaseConnection();
} }
return result; return result;
@ -382,9 +388,11 @@ public class NiciraNvpApi {
if (dm.getStatusCode() != HttpStatus.SC_NO_CONTENT) { if (dm.getStatusCode() != HttpStatus.SC_NO_CONTENT) {
String errorMessage = responseToErrorMessage(dm); String errorMessage = responseToErrorMessage(dm);
dm.releaseConnection();
s_logger.error("Failed to delete object : " + errorMessage); s_logger.error("Failed to delete object : " + errorMessage);
throw new NiciraNvpApiException("Failed to delete object : " + errorMessage); throw new NiciraNvpApiException("Failed to delete object : " + errorMessage);
} }
dm.releaseConnection();
} }
private <T> T executeRetrieveObject(Type returnObjectType, String uri, Map<String,String> parameters) throws NiciraNvpApiException { private <T> T executeRetrieveObject(Type returnObjectType, String uri, Map<String,String> parameters) throws NiciraNvpApiException {
@ -410,6 +418,7 @@ public class NiciraNvpApi {
if (gm.getStatusCode() != HttpStatus.SC_OK) { if (gm.getStatusCode() != HttpStatus.SC_OK) {
String errorMessage = responseToErrorMessage(gm); String errorMessage = responseToErrorMessage(gm);
gm.releaseConnection();
s_logger.error("Failed to retrieve object : " + errorMessage); s_logger.error("Failed to retrieve object : " + errorMessage);
throw new NiciraNvpApiException("Failed to retrieve object : " + errorMessage); throw new NiciraNvpApiException("Failed to retrieve object : " + errorMessage);
} }
@ -421,8 +430,9 @@ public class NiciraNvpApi {
} catch (IOException e) { } catch (IOException e) {
s_logger.error("IOException while retrieving response body",e); s_logger.error("IOException while retrieving response body",e);
throw new NiciraNvpApiException(e); throw new NiciraNvpApiException(e);
} finally {
gm.releaseConnection();
} }
return returnValue; return returnValue;
} }
@ -430,6 +440,7 @@ public class NiciraNvpApi {
try { try {
_client.executeMethod(method); _client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) { if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
method.releaseConnection();
// login and try again // login and try again
login(); login();
_client.executeMethod(method); _client.executeMethod(method);

View File

@ -62,11 +62,7 @@ public class ClusterServiceServletImpl implements ClusterService {
method.addParameter("stopOnError", pdu.isStopOnError() ? "1" : "0"); method.addParameter("stopOnError", pdu.isStopOnError() ? "1" : "0");
method.addParameter("pduType", Integer.toString(pdu.getPduType())); method.addParameter("pduType", Integer.toString(pdu.getPduType()));
try { return executePostMethod(client, method);
return executePostMethod(client, method);
} finally {
method.releaseConnection();
}
} }
@Override @Override
@ -81,15 +77,11 @@ public class ClusterServiceServletImpl implements ClusterService {
method.addParameter("method", Integer.toString(RemoteMethodConstants.METHOD_PING)); method.addParameter("method", Integer.toString(RemoteMethodConstants.METHOD_PING));
method.addParameter("callingPeer", callingPeer); method.addParameter("callingPeer", callingPeer);
try { String returnVal = executePostMethod(client, method);
String returnVal = executePostMethod(client, method); if("true".equalsIgnoreCase(returnVal)) {
if("true".equalsIgnoreCase(returnVal)) { return true;
return true;
}
return false;
} finally {
method.releaseConnection();
} }
return false;
} }
private String executePostMethod(HttpClient client, PostMethod method) { private String executePostMethod(HttpClient client, PostMethod method) {
@ -115,6 +107,8 @@ public class ClusterServiceServletImpl implements ClusterService {
s_logger.error("IOException from : " + _serviceUrl + ", method : " + method.getParameter("method")); s_logger.error("IOException from : " + _serviceUrl + ", method : " + method.getParameter("method"));
} catch(Throwable e) { } catch(Throwable e) {
s_logger.error("Exception from : " + _serviceUrl + ", method : " + method.getParameter("method") + ", exception :", e); s_logger.error("Exception from : " + _serviceUrl + ", method : " + method.getParameter("method") + ", exception :", e);
} finally {
method.releaseConnection();
} }
return result; return result;

View File

@ -131,6 +131,8 @@ public class UpgradeManagerImpl implements UpgradeManager {
return "Unable to retrieve the file from " + url; return "Unable to retrieve the file from " + url;
} catch (final IOException e) { } catch (final IOException e) {
return "Unable to retrieve the file from " + url; return "Unable to retrieve the file from " + url;
} finally {
method.releaseConnection();
} }
file.delete(); file.delete();