HttpClient 3.1 Connection pool compared to HttpClient 4.3.2

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)); // set the so_timeout
            clientParams.setParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT, new Integer(3000)); // Head responses should come back in 3 seconds or less
            clientParams.setParameter(HttpMethodParams.REJECT_HEAD_BODY, Boolean.TRUE); // We should reject any BODY sent as part of the HEAD request
            clientParams.setParameter(HttpMethodParams.STATUS_LINE_GARBAGE_LIMIT, new Integer(0)); // We will not tolerate any garbage in the STATUS line
            clientParams.setParameter(HttpClientParams.MAX_REDIRECTS, new Integer(5)); // We'll follow redirects 5 times.
            clientParams.setConnectionManagerTimeout(30000); // We'll wait 30 seconds before timing out on getting a connection out of the connection manager
        } 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);

    /**
     * Get a handle to a common HTTPClient that uses a multithreaded connection pool manager
     * Code that uses this shares the pool.
     * So it should be ok to use pools other than this one in case the threads in this pool are stuck on proxying web videos etc. 
     * @return
     */
    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

+4
source share

Source: https://habr.com/ru/post/1525551/


All Articles