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;
response = client.executeMethod(method);
if (response != HttpURLConnection.HTTP_OK) {
method.releaseConnection();
s_logger.warn("Retrieving from " + url + " gives response code: "
+ response);
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 "
+ url + ", ", e);
}
method.releaseConnection();
}
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.methods.GetMethod;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
/**
@ -413,23 +414,26 @@ public class S3CAStorBucketAdapter implements S3BucketAdapter {
}
public class ScspDataSource implements DataSource {
GetMethod method;
public ScspDataSource(GetMethod m) {
method = m;
String content_type = null;
byte content[] = null;
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
public String getContentType() {
Header h = method.getResponseHeader("Content-type");
return h==null ? null : h.getValue();
return content_type;
}
@Override
public InputStream getInputStream() throws IOException {
try {
return method.getResponseBodyAsStream();
} catch (Exception e) {
s_logger.error("CAStor loadObjectRange getInputStream error", e);
return null;
}
public InputStream getInputStream() {
return new ByteArrayInputStream(content);
}
@Override
public String getName() {
@ -445,21 +449,27 @@ public class S3CAStorBucketAdapter implements S3BucketAdapter {
@Override
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 {
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 = httpClient.executeMethod(method);
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) {
statusCode = httpClient.executeMethod(method);
} catch (HttpException e) {
s_logger.error("CAStor loadObjectRange failure", e);
throw new FileNotExistException("CAStor loadObjectRange failure: " + e);
} catch (IOException e) {
s_logger.error("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

View File

@ -119,6 +119,8 @@ public class NiciraNvpApi {
throw new NiciraNvpApiException("Nicira NVP API login failed ", e);
} catch (IOException e) {
throw new NiciraNvpApiException("Nicira NVP API login failed ", e);
} finally {
pm.releaseConnection();
}
if (pm.getStatusCode() != HttpStatus.SC_OK) {
@ -322,10 +324,11 @@ public class NiciraNvpApi {
if (pm.getStatusCode() != HttpStatus.SC_OK) {
String errorMessage = responseToErrorMessage(pm);
pm.releaseConnection();
s_logger.error("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 {
@ -352,6 +355,7 @@ public class NiciraNvpApi {
if (pm.getStatusCode() != HttpStatus.SC_CREATED) {
String errorMessage = responseToErrorMessage(pm);
pm.releaseConnection();
s_logger.error("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());
} catch (IOException e) {
throw new NiciraNvpApiException("Failed to decode json response body", e);
} finally {
pm.releaseConnection();
}
return result;
@ -382,9 +388,11 @@ public class NiciraNvpApi {
if (dm.getStatusCode() != HttpStatus.SC_NO_CONTENT) {
String errorMessage = responseToErrorMessage(dm);
dm.releaseConnection();
s_logger.error("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 {
@ -410,6 +418,7 @@ public class NiciraNvpApi {
if (gm.getStatusCode() != HttpStatus.SC_OK) {
String errorMessage = responseToErrorMessage(gm);
gm.releaseConnection();
s_logger.error("Failed to retrieve object : " + errorMessage);
throw new NiciraNvpApiException("Failed to retrieve object : " + errorMessage);
}
@ -421,8 +430,9 @@ public class NiciraNvpApi {
} catch (IOException e) {
s_logger.error("IOException while retrieving response body",e);
throw new NiciraNvpApiException(e);
} finally {
gm.releaseConnection();
}
return returnValue;
}
@ -430,6 +440,7 @@ public class NiciraNvpApi {
try {
_client.executeMethod(method);
if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
method.releaseConnection();
// login and try again
login();
_client.executeMethod(method);

View File

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

View File

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