title: How do I set a timeout on Spring's AsyncRestTemplate?
body: <p>I've setup a RestTemplate and an AsyncRestTemplate in my project similar to the following:</p>
  
  <p><a href="http://vincentdevillers.blogspot.fr/2013/10/a-best-spring-asyncresttemplate.html">http://vincentdevillers.blogspot.fr/2013/10/a-best-spring-asyncresttemplate.html</a></p>
  
  <p>I've noticed that the connect timeout doesn't actually work unless I change the httpRequestFactory() bean to be the following:</p>
  
  <pre><code>@Bean
  public ClientHttpRequestFactory httpRequestFactory() {
      HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient());
      factory.setConnectTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
      factory.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
      return factory;
  }
  </code></pre>
  
  <p>If I set DEFAULT_READ_TIMEOUT_MILLISECONDS to 5, a timeout occurs when I use restTemplate (as expected). However, when I use AsyncRestTemplate, a timeout doesn't occur. I've modified the asyncHttpRequestFactory() like httpRequestFactory(), but no dice.</p>
  
  <pre><code>@Bean
  public AsyncClientHttpRequestFactory asyncHttpRequestFactory() {
      HttpComponentsAsyncClientHttpRequestFactory factory = new HttpComponentsAsyncClientHttpRequestFactory(asyncHttpClient());
      factory.setConnectTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
      factory.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
      return factory;
  }
  </code></pre>
  
  <p>Here's how I'm attempting to use AsyncRestTemplate in a Spring MVC Controller:</p>
  
  <pre><code>String url = "...";
  // Start the clock
  long start = System.currentTimeMillis();
  
  ListenableFuture&lt;ResponseEntity&lt;String&gt;&gt; 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();
  </code></pre>
  
  <p>How can I get AsyncRestTemplate to read my connection timeout settings?</p>
  
  <p>On a related note, <a href="https://spring.io/guides/gs/async-method/">https://spring.io/guides/gs/async-method/</a> uses @Async and RestTemplate and seems to accomplish what I'm looking for. What's the advantage of using AsyncRestTemplate over RestTemplate?</p>