|
4 | 4 | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
5 | 5 | import org.apache.http.impl.client.CloseableHttpClient; |
6 | 6 | import org.apache.http.impl.client.HttpClients; |
| 7 | +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
| 8 | +import org.apache.http.client.config.RequestConfig; |
7 | 9 | import org.apache.http.ssl.TrustStrategy; |
8 | 10 | import org.springframework.context.annotation.Bean; |
9 | 11 | import org.springframework.context.annotation.Configuration; |
@@ -36,16 +38,27 @@ public RestTemplate restTemplate() throws Exception { |
36 | 38 | null, |
37 | 39 | NoopHostnameVerifier.INSTANCE); |
38 | 40 |
|
39 | | - // Create HTTP client with SSL configuration |
| 41 | + // Create connection manager with pooling |
| 42 | + PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); |
| 43 | + connectionManager.setMaxTotal(100); |
| 44 | + connectionManager.setDefaultMaxPerRoute(20); |
| 45 | + |
| 46 | + // Create request config with timeouts |
| 47 | + RequestConfig requestConfig = RequestConfig.custom() |
| 48 | + .setConnectTimeout(10000) // 10 seconds |
| 49 | + .setSocketTimeout(30000) // 30 seconds |
| 50 | + .build(); |
| 51 | + |
| 52 | + // Create HTTP client with SSL configuration and timeouts |
40 | 53 | CloseableHttpClient httpClient = HttpClients.custom() |
41 | 54 | .setSSLSocketFactory(csf) |
| 55 | + .setConnectionManager(connectionManager) |
| 56 | + .setDefaultRequestConfig(requestConfig) |
42 | 57 | .build(); |
43 | 58 |
|
44 | 59 | // Create request factory with custom HTTP client |
45 | 60 | HttpComponentsClientHttpRequestFactory requestFactory = |
46 | 61 | new HttpComponentsClientHttpRequestFactory(httpClient); |
47 | | - requestFactory.setConnectTimeout(10000); // 10 seconds |
48 | | - requestFactory.setReadTimeout(30000); // 30 seconds |
49 | 62 |
|
50 | 63 | return new RestTemplate(requestFactory); |
51 | 64 | } |
|
0 commit comments