How to set timeout on Spring AsyncRestTemplate?

I installed RestTemplate and AsyncRestTemplate in my project similar to the following:

http://vincentdevillers.blogspot.fr/2013/10/a-best-spring-asyncresttemplate.html

I noticed that the connection timeout did not actually work unless I changed the httpRequestFactory () bean as follows:

@Bean public ClientHttpRequestFactory httpRequestFactory() { HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient()); factory.setConnectTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS); factory.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS); return factory; } 

If I set DEFAULT_READ_TIMEOUT_MILLISECONDS to 5, a timeout will occur when I use restTemplate (as expected). However, when I use AsyncRestTemplate, a timeout does not occur. I changed asyncHttpRequestFactory () as httpRequestFactory (), but not a cube.

 @Bean public AsyncClientHttpRequestFactory asyncHttpRequestFactory() { HttpComponentsAsyncClientHttpRequestFactory factory = new HttpComponentsAsyncClientHttpRequestFactory(asyncHttpClient()); factory.setConnectTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS); factory.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS); return factory; } 

This is how I try to use AsyncRestTemplate in a Spring MVC controller:

 String url = "..."; // Start the clock long start = System.currentTimeMillis(); ListenableFuture<ResponseEntity<String>> results = asyncRestTemplate.getForEntity(url, String.class); // Wait until the request is finished while (!(results.isDone())) { Thread.sleep(10); //millisecond pause between each check } System.out.println("Elapsed time: " + (System.currentTimeMillis() - start)); return results.get().getBody(); 

How can I get an AsyncRestTemplate to read connection timeout settings?

In a related note, https://spring.io/guides/gs/async-method/ uses @Async and RestTemplate and seems to do what I'm looking for. What is the advantage of using AsyncRestTemplate over RestTemplate?

+7
spring asynchronous spring-mvc resttemplate
source share
4 answers

I think the point of AsyncRestTemplate is ListenableFuture (whose functions you are not actually using). So probably @Async is right for your use case. Also, I think results.get(5,TimeUnit.SECOND) more efficient (and elegant), and then loop with Thread.sleep(...) , but this is up to you, I think. Or maybe I missed something.

+3
source share

AsyncRestTemplate uses the capabilities of the HTTP NIO client, so you can manage a large number of connections with a small number of threads. To achieve a similar result with @Async + RestTemplate will require much more resources. If you do this in only a few places, then this is probably good, although even then AsyncRestTemplate, since it exists, is slightly better and more specialized to use.

As for reading timeout, it looks like it might be an oversight. Feel free to create a ticket. A quick look at this page , an example HttpAsyncClient configuration, shows with:

IOReactorConfig.custom().setConnectTimeout(30000) .

I guess you could try quickly

+3
source share

Very similar to sync one:

 final SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setTaskExecutor(new SimpleAsyncTaskExecutor()); requestFactory.setConnectTimeout(connectTimeout); requestFactory.setReadTimeout(readTimeout); final AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate(); asyncRestTemplate.setAsyncRequestFactory(requestFactory); 

From the source code, you can see that the AsyncRestTemplate instance is created using the SimpleClientHttpRequestFactory along with the SimpleAsyncTaskExecutor . That way, you can just do the same with an instance of SimpleClientHttpRequestFactory whose timeout values ​​are set.

+3
source share
 public CloseableHttpAsyncClient asyncHttpClient() { IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setConnectTimeout(getProperties().getConnectTimeout()).setSoTimeout(getProperties().getReadTimeout()).build() return HttpAsyncClients.custom().setDefaultIOReactorConfig(ioReactorConfig).build(); } 

for example, the AsyncRestTemplate timeout configuration is fine.

0
source share

All Articles