Search code examples
httpclientconnection-poolingwebsphere-8resttemplatespring-4

Websphere problems when setting up Http Connection Pool for RestTemplate


I'm working with Spring 4.0.3.RELEASE version. I'm able to make restful calls successfully. However, I learnt that HTTP connections are expensive and thought of using connection pool. I read few articles like this and this. Everything is fine when I include the dependency with Maven3 and compile. The problems arise during runtime. With this code, I get class not found exception for PoolingHttpClientConnectionManager.

public RestTemplate restTemplate(){
    HttpHost host = new HttpHost("localhost", 9081);
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    // Increase max total connection to 200
    cm.setMaxTotal(200);
    cm.setDefaultMaxPerRoute(50);       
    cm.setMaxPerRoute(new HttpRoute(host), 20);

    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create().setConnectionManager(cm);
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
            httpClientBuilder.build());
    return new RestTemplate(requestFactory);
}

And with this code, I get class not found exception for HttpClients.

public RestTemplate restTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setConnectTimeout(20000);
        requestFactory.setReadTimeout(20000);       
        restTemplate.setRequestFactory(requestFactory); 
        return  restTemplate;
    }

I'm deploying on Websphere 8.5. And I tried different versions of httpclient from 4.0.1 to 4.5.2 with different scope (provided, compile) with no luck. Thanks in advance for any hint in the right direction.


Solution

  • So, it's basically a problem with Websphere 8+ versions including httpclient with their jvm library. Luckily, our Enterprise Websphere are all 7.5.x version. So, if we bundle the httpclient with the ear, it does not cause any issue in production or pre-production.

    For those, who have to use Websphere 8.x versions, please use the shared isolated library approach as defined here. I used that for my local 8.5 Websphere and it works pretty neat.