httpclient - 为 RestTemplate 设置 Http 连接池时的 Websphere 问题

标签 httpclient connection-pooling websphere-8 resttemplate spring-4

我正在使用 Spring 4.0.3.RELEASE 版本。我能够成功地调用电话。然而,我了解到HTTP连接是昂贵的并且想到了使用连接池。我读了几篇像this这样的文章和 this .当我将依赖项包含在 Maven3 中并进行编译时,一切都很好。问题出现在运行时。 使用此代码,我得到了 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);
}

通过这段代码,我得到了 HttpClients 的类未找到异常。

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

我正在 Websphere 8.5 上部署。我尝试了从 4.0.1 到 4.5.2 的不同版本的 httpclient,但没有成功。 在此先感谢您提供正确方向的任何提示。

最佳答案

所以,这基本上是 Websphere 8+ 版本的问题,包括带有 jvm 库的 httpclient。幸运的是,我们的 Enterprise Websphere 都是 7.5.x 版本。因此,如果我们将 httpclient 与 ear 捆绑在一起,它不会在生产或预生产中造成任何问题。

对于必须使用 Websphere 8.x 版本的用户,请使用共享隔离库方法 as defined here .我将它用于我的本地 8.5 Websphere,它工作得非常好。

关于httpclient - 为 RestTemplate 设置 Http 连接池时的 Websphere 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37221247/

相关文章:

java - WebSphere 8.5 : unit testing & embeddable container

spring - 如何将 Java 6 中的 TLS 1.2 与 Apache HttpClient 一起使用?

java - 使用 TLS 的 Apache HTTPClient

c# - 如何正确使用 HttpClient PostAsync 参数?

java - 如何在 Hibernate 中禁用连接池

java - JPA 应用程序中的连接何时返回到连接池?

java - HikariPool-1 - 连接不可用,请求超时

java - 无法启动部署在 websphere 8.5.5 上的 springboot 应用程序

java - Spring + Websphere 8.5 : Unable to lookup JNDI name [java:comp/websphere/ExtendedJTATransaction]

java - 为什么要为 HttpHead 方法取回 cookie?