Skip to content

Instantly share code, notes, and snippets.

@marciorodrigues87
Last active July 6, 2017 02:32
Show Gist options
  • Save marciorodrigues87/45436ac17f87afa7a4bde604660f8763 to your computer and use it in GitHub Desktop.
Save marciorodrigues87/45436ac17f87afa7a4bde604660f8763 to your computer and use it in GitHub Desktop.
RestTemplateConfigStepNine.java
final RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(100)
.setConnectTimeout(500)
.setSocketTimeout(1000)
.build();
final HttpClient httpClient = HttpClients.custom()
.setMaxConnPerRoute(100)
.setMaxConnTotal(1000)
.setConnectionTimeToLive(30, MINUTES)
.setRetryHandler((IOException exception, int executionCount, HttpContext context) -> {
return executionCount <= 3;
})
.setServiceUnavailableRetryStrategy(new DefaultServiceUnavailableRetryStrategy(3, 1))
.setDefaultRequestConfig(requestConfig)
.setUserAgent("Aplicacao-VivaReal-v1.0.2")
.build();
final RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
interceptors.add(new BasicAuthorizationInterceptor("user", "password"));
interceptors.add((HttpRequest request, byte[] body, ClientHttpRequestExecution execution) -> { // <--
ClientHttpResponse response = null;
final long start = System.currentTimeMillis();
try {
return response = execution.execute(request, body);
} finally {
log.debug("REQUEST method={}, uri={}, params={}", request.getMethod(), request.getURI(), request.getURI().getQuery()); // <--
if (response != null) {
log.debug("RESPONSE status={}, time={}", response.getRawStatusCode(), System.currentTimeMillis() - start); // <--
}
}
});
restTemplate.setInterceptors(interceptors); // <--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment