【问题标题】:Websphere problems when setting up Http Connection Pool for RestTemplate为 RestTemplate 设置 Http 连接池时的 Websphere 问题
【发布时间】:2016-09-10 07:32:48
【问题描述】:

我正在使用 Spring 4.0.3.RELEASE 版本。我能够成功拨打安静的电话。但是,我了解到 HTTP 连接很昂贵,并考虑使用连接池。我读了几篇像thisthis 这样的文章。当我在 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,它们的范围不同(提供,编译),但没有运气。 提前感谢您提供正确方向的任何提示。

【问题讨论】:

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


    【解决方案1】:

    您应该停止应用程序,然后转到 websphere 中的管理模块选项卡,然后 并在“类加载器顺序”组合框中选择“父级最后”项 然后启动应用程序。问题解决了……

    【讨论】:

    • 这不是正确的方法。如标记答案中所述,共享库方法是引用 jar 的正确方法,这与 Websphere 容器库不同。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-09-25
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 1970-01-01
      相关资源
      最近更新 更多