diff --git a/src/main/java/com/silverpop/api/client/ApiClient.java b/src/main/java/com/silverpop/api/client/ApiClient.java index 099757e..42f4aad 100644 --- a/src/main/java/com/silverpop/api/client/ApiClient.java +++ b/src/main/java/com/silverpop/api/client/ApiClient.java @@ -6,6 +6,7 @@ import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethodBase; +import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -16,9 +17,10 @@ public abstract class ApiClient { private Log log = LogFactory.getLog(this.getClass()); private ApiCommandProcessor commandProcessor; - protected HttpClient httpClient; protected ApiSession session; + private final HttpClient httpClient; + public ApiSession getSession() { return session; @@ -26,9 +28,15 @@ public ApiSession getSession() { protected ApiClient(ApiCommandProcessor commandProcessor, ApiSession session) { - this(commandProcessor, new HttpClient(), session); + this(commandProcessor, new HttpClient(new MultiThreadedHttpConnectionManager()), session); } + /** + * Constructor. + * @param commandProcessor can not be {@code null}. + * @param httpClient this should be configured with a {@code MultiThreadedHttpConnectionManager}, otherwise the ApiClient constructed will not be thread-safe. + * @param session can not be {@code null}. + */ protected ApiClient(ApiCommandProcessor commandProcessor, HttpClient httpClient, ApiSession session) { this.commandProcessor = commandProcessor; this.httpClient = httpClient; @@ -84,7 +92,7 @@ private void ensureSessionIsOpen() { } } - protected String executeMethod(HttpMethodBase method) throws ApiResultException { + private String executeMethod(HttpMethodBase method) throws ApiResultException { try { log.info("executing method:" + method); int responseCode = httpClient.executeMethod(method); @@ -106,8 +114,10 @@ protected String executeMethod(HttpMethodBase method) throws ApiResultException } } catch(IOException e) { throw new ApiException("Error executing API: ", e); - } - } + } finally { + method.releaseConnection(); + } + } private ApiResult extractResult(String requestName, ApiResponse response) throws ApiResultException { if(response.isSuccessful()) {