I used HttpClient3.1 (Java) for the connection pool as shown below:
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;
public class HttpClientManager {
private static Logger logger = Logger.getLogger(HttpClientManager.class);
private static MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
private static HttpClientParams clientParams = new HttpClientParams();
static {
try {
clientParams.makeLenient();
clientParams.setAuthenticationPreemptive(false);
clientParams.setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(30000));
clientParams.setParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, new Integer(3000));
clientParams.setParameter(HttpMethodParams.REJECT_HEAD_BODY, Boolean.TRUE);
clientParams.setParameter(HttpMethodParams.STATUS_LINE_GARBAGE_LIMIT, new Integer(0));
clientParams.setParameter(HttpClientParams.MAX_REDIRECTS, new Integer(5));
clientParams.setConnectionManagerTimeout(30000);
} catch(Throwable ex) {
logger.error("Exception setting timeouts etc for client connection factory", ex);
System.out.println("Exception setting timeouts etc for client connection factory"+ex);
}
}
private static HttpClient client = new HttpClient(clientParams, connectionManager);
public static HttpClient getClient() {
return client;
}
}
I get the client as follows: HttpClient client = HttpClientManager.getClient();and start calling methods for this, and finally, I call method.releaseConnection();but most of the time I get the following exception:
java.net.SocketTimeoutException: Read timed out
java.net.SocketException: Software caused connection abort: recv failed
java.net.ConnectException: Connection timed out: connect
therefore, we plan to switch to httpclient4.3.2 and see some code similar to the one below for the connection pool:
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(100);
cm.closeExpiredConnections();
CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();
is this normal, or is there another way to implement the same?
thanks
source
share