title: SimpleClientHttpRequestFactory vs HttpComponentsClientHttpRequestFactory for Http Request timeout with RestTemplate?
body: <p>I am working on a project in which I need to make a HTTP URL call to my server which is running Restful Service which returns back the response as a <code>JSON String</code>.</p>
  
  <p>Below is my main code which is using the future and callables:</p>
  
  <pre><code>public class TimeoutThreadExample {
  
      private ExecutorService executor = Executors.newFixedThreadPool(10);
      private RestTemplate restTemplate = new RestTemplate();
  
      public String getData() {
          Future&lt;String&gt; future = executor.submit(new Task(restTemplate));
          String response = null;
  
          try {
              response = future.get(500, TimeUnit.MILLISECONDS);
          } catch (TimeoutException e) {
              e.printStackTrace();
          } catch (InterruptedException e) {
              e.printStackTrace();
          } catch (ExecutionException e) {
              e.printStackTrace();
          }
  
          return response;
      }
  }
  </code></pre>
  
  <p>Below is my <code>Task</code> class which implements the <code>Callable</code> interface and uses the <code>RestTemplate</code>:</p>
  
  <pre><code>class Task implements Callable&lt;String&gt; {
  
      private RestTemplate restTemplate;
  
      public Task(RestTemplate restTemplate) {
          this.restTemplate = restTemplate;
      }
  
      public String call() throws Exception {
  
          String url = "some_url";
          String response = restTemplate.getForObject(url, String.class);
  
          return response;
      }
  }
  </code></pre>
  
  <p><strong>Problem Statement:</strong></p>
  
  <p>As you can see above, I am using default way of executing the URL using <code>RestTemplate</code> which doesn't use any Http Request timeout so that means internally it is using <code>-1</code> as the <code>read</code> and <code>connection</code> timeout. </p>
  
  <p>Now what I am looking to do is, I want to set up Http Request timeout using <code>RestTemplate</code> in my above code efficiently. And I am not sure which class I need to use for that, I can see <code>HttpComponentsClientHttpRequestFactory</code> and <code>SimpleClientHttpRequestFactory</code> so not sure which one I need to use?</p>
  
  <p>Any simple example basis on my above code will help me understand better on how to set the Http Request timeout using <code>RestTemplate</code>.</p>
  
  <p>And also does my Http Request timeout value should be less than future timeout value?</p>
  
  <ul>
  <li><code>HttpComponentsClientHttpRequestFactory</code> vs <code>SimpleClientHttpRequestFactory</code>. Which one to use?</li>
  <li>Does my Http Request timeout value should be less than future timeout value?</li>
  </ul>