Last active
January 27, 2020 07:55
-
-
Save kgoralski/b3a6a08af5ef9ac0142428f18e06e3cd to your computer and use it in GitHub Desktop.
rest_template.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.goralski.application; | |
| import org.apache.http.client.HttpClient; | |
| import org.apache.http.client.config.RequestConfig; | |
| import org.apache.http.config.SocketConfig; | |
| import org.apache.http.impl.client.HttpClientBuilder; | |
| import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | |
| import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; | |
| import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; | |
| import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; | |
| import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor; | |
| import org.apache.http.nio.reactor.ConnectingIOReactor; | |
| import org.apache.http.nio.reactor.IOReactorException; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.context.annotation.Configuration; | |
| import org.springframework.http.client.AsyncClientHttpRequestFactory; | |
| import org.springframework.http.client.ClientHttpRequestFactory; | |
| import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory; | |
| import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | |
| import org.springframework.web.client.AsyncRestTemplate; | |
| import org.springframework.web.client.RestTemplate; | |
| // video https://vimeo.com/233798451 | |
| // http://www.baeldung.com/spring-retry | |
| // http://www.baeldung.com/httpclient-connection-management | |
| // http://www.baeldung.com/httpclient-timeout | |
| // http://www.baeldung.com/httpclient-ssl | |
| // setMaxTotal(int max): Set the maximum number of total open connections. (20 is default) | |
| // setDefaultMaxPerRoute(int max): Set the maximum number of concurrent connections per route, which is 2 by default. | |
| // setMaxPerRoute(int max): Set the total number of concurrent connections to a specific route, which is 2 by default. | |
| // the Connection Timeout (http.connection.timeout) – the time to establish the connection with the remote host . (sane default 100 ms) | |
| // the Socket Timeout (http.socket.timeout) – the time waiting for data – after the connection was established; maximum time of inactivity between two data packets (sane default 200 ms) | |
| // the Connection Manager Timeout (http.connection-manager.timeout) – the time to wait for a connection from the connection manager/pool | |
| // The first two parameters – the connection and socket timeouts – are the most important, but setting a timeout for obtaining a connection is definitely important in high load scenarios, which is why the third parameter shouldn’t be ignored. | |
| @Configuration | |
| public class RestTemplateConfiguration { | |
| // set your reasonable values | |
| @Bean | |
| public ClientHttpRequestFactory clientHttpRequestFactory() { | |
| HttpComponentsClientHttpRequestFactory factory = | |
| new HttpComponentsClientHttpRequestFactory(); | |
| int timeout = 60; // milliseconds | |
| factory.setConnectTimeout(timeout); | |
| factory.setReadTimeout(timeout); | |
| PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); | |
| connectionManager.setMaxTotal(200); | |
| connectionManager.setDefaultMaxPerRoute(50); | |
| connectionManager.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(timeout).build()); | |
| HttpClient httpClient = HttpClientBuilder.create() | |
| .useSystemProperties() | |
| .setConnectionManager(connectionManager) | |
| .build(); | |
| factory.setHttpClient(httpClient); | |
| return factory; | |
| } | |
| @Bean | |
| public AsyncClientHttpRequestFactory asyncClientHttpRequestFactory() throws IOReactorException { | |
| HttpComponentsAsyncClientHttpRequestFactory factory = | |
| new HttpComponentsAsyncClientHttpRequestFactory(); | |
| int timeout = 60; // milliseconds | |
| RequestConfig config = RequestConfig.custom() | |
| .setConnectTimeout(timeout) | |
| .setConnectionRequestTimeout(timeout) | |
| .setSocketTimeout(timeout).build(); | |
| factory.setConnectTimeout(timeout); | |
| factory.setReadTimeout(timeout); | |
| factory.setConnectionRequestTimeout(timeout); | |
| ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(); | |
| PoolingNHttpClientConnectionManager connectionManager = new PoolingNHttpClientConnectionManager(ioReactor); | |
| connectionManager.setMaxTotal(200); | |
| connectionManager.setDefaultMaxPerRoute(50); | |
| CloseableHttpAsyncClient asyncClient = HttpAsyncClientBuilder.create() | |
| .useSystemProperties() | |
| .setConnectionManager(connectionManager) | |
| .setDefaultRequestConfig(config) | |
| .build(); | |
| factory.setAsyncClient(asyncClient); | |
| return factory; | |
| } | |
| @Bean | |
| public RestTemplate restTemplate(ClientHttpRequestFactory clientHttpRequestFactory) { | |
| return new RestTemplate(clientHttpRequestFactory); | |
| } | |
| @Bean | |
| // replaced by WebClient in Spring 5 | |
| public AsyncRestTemplate asyncRestTemplate(AsyncClientHttpRequestFactory asyncClientHttpRequestFactory) { | |
| return new AsyncRestTemplate(asyncClientHttpRequestFactory); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment