0

As I am planning to upgrade httpclient dependency from v.3.1 to v.4.5.13, but later I realized that, lot of functionalities have been changed and one among them is that, MultiThreadedHttpConnectionManager has been removed from recent version. While I was searching for an equivalent class, one of the posts in stackoverflow HttpClient 4 - What happened to MultiThreadedHttpConnectionManager?, suggested to use PoolingHttpClientConnectionManager class. But, I wanted to set connection timeout and socket timeout, where these functionalities are not provided by PoolingHttpClientConnectionManager.

final MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
        connectionManager.setMaxTotalConnections(150);
        connectionManager.setMaxConnectionsPerHost(90);
        connectionManager.setConnectionTimeout(15000);
        connectionManager.setSoTimeout(600000);

Can anyone suggest, how connection timeout and socket timeout can be set in recent httpclient v.4.5.x?

1 Answers1

-1

Here is my code:

    private static class HttpPool {

        private static CloseableHttpClient httpClient;
        private static final int TIME_OUT = 30;
        private static PoolingHttpClientConnectionManager cm;

        public static void init() {
            cm = new PoolingHttpClientConnectionManager();
            cm.setDefaultMaxPerRoute(200);
            cm.setMaxTotal(100);
            cm.setDefaultMaxPerRoute(20);
            HttpHost localhost = new HttpHost("locahost", 80);
            cm.setMaxPerRoute(new HttpRoute(localhost), 50);
            
            ConnectionKeepAliveStrategy kaStrategy = new DefaultConnectionKeepAliveStrategy() {
                @Override
                public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                    long keepAlive = super.getKeepAliveDuration(response, context);
                    if (keepAlive == -1) {
                        keepAlive = 0;
                    }
                    return keepAlive;
                }

            };
            SocketConfig socketConfig = SocketConfig.custom()
                    .setSoKeepAlive(false)
                    .setSoLinger(1)
                    .setSoReuseAddress(true)
                    .setSoTimeout(TIME_OUT)
                    .setTcpNoDelay(true).build();
            
            RequestConfig config = RequestConfig.custom()
                    .setConnectTimeout(TIME_OUT)
                    .setConnectionRequestTimeout(TIME_OUT)
                    .setSocketTimeout(TIME_OUT).build();
            
            httpClient = HttpClients.custom()
                    .setConnectionManager(cm)
                    .setKeepAliveStrategy(kaStrategy)
                    .setDefaultRequestConfig(config)
                    .setDefaultSocketConfig(socketConfig)
                    .setRetryHandler((IOException exception, int executionCount, HttpContext context) -> false)
                    .build();
            
            new Thread(() -> new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    // Close expired connections
                    cm.closeExpiredConnections();
                    // Optionally, close connections
                    // that have been idle longer than 30 sec
                    cm.closeIdleConnections(30, TimeUnit.SECONDS);
                }
            }, 0, 5000)).start();
            
        }

        public static CloseableHttpClient getClient() {
            if (httpClient == null) {
                synchronized (HttpPool.class) {
                    if (httpClient == null) {
                        init();
                    }
                }
            }
            return httpClient;
        }

    }

Some code is copyed from others, if there are errors, please point out

Cwift
  • 300
  • 1
  • 6