本文介绍了SpringBoot RestTemplate 连接池:管理过时的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 RestTemplate 与 ConnectionPooling 结合使用,使用 PoolingHttpClientConnectionManager,如下面的代码所示:

I am using RestTemplate with ConnectionPooling using PoolingHttpClientConnectionManager as in below code :

PoolingHttpClientConnectionManager connectionManager = new
PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);

connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
    connectionManager.setMaxPerRoute(new HttpRoute(new
HttpHost(excConfig.getImsServerEndpoint())), IMS_ROUTE_MAX_CONNECTIONS);

    CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(connectionManager).build();

    HttpComponentsClientHttpRequestFactory httpReqFactory = new HttpComponentsClientHttpRequestFactory(httpclient);
    httpReqFactory.setReadTimeout(DEFAULT_HTTP_TIMEOUT_MILLISECONDS);
    httpReqFactory.setConnectionRequestTimeout(DEFAULT_HTTP_TIMEOUT_MILLISECONDS);
    httpReqFactory.setConnectTimeout(DEFAULT_HTTP_TIMEOUT_MILLISECONDS);

    restTemplate = new RestTemplate(httpReqFactory);

RestTemplate 是否自己负责终止过时的连接,或者我是否需要为此进行一些特定的处理.

Does RestTemplate takes care of terminating Stale Connections by itself, or do I need to put in some specific handling for the same.

推荐答案

setStaleConnectionCheckEnabled 已过时.

HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().).setConnectionManager(连接管理器).build();

HttpClients.custom() .setDefaultRequestConfig(RequestConfig.custom().) .setConnectionManager(connectionManager) .build();

在 HttpClient 4.5.3 中,以下代码有效 -

In HttpClient 4.5.3 the below code works -

PoolingHttpClientConnectionManager connManager
          = new PoolingHttpClientConnectionManager();
        connManager.setValidateAfterInactivity(20);

        HttpClient httpClient = HttpClients.custom().setConnectionManager(connManager).build();

这篇关于SpringBoot RestTemplate 连接池:管理过时的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:24