title: How to use RestTemplate efficiently with RequestFactory?
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 JSON String. I am using <code>RestTemplate</code> here along with <code>HttpComponentsClientHttpRequestFactory</code> to execute an url.</p>
  
  <p>I have setup a http request timeout (READ and CONNECTION time out) on my <code>RestTemplate</code> by using <code>HttpComponentsClientHttpRequestFactory</code>.</p>
  
  <p>Below is my Interface:</p>
  
  <pre><code>public interface Client {
  
      // for synchronous
      public String getSyncData(String key, long timeout);
  
      // for asynchronous
      public String getAsyncData(String key, long timeout);
  }
  </code></pre>
  
  <p>Below is my implementation of Client interface -</p>
  
  <pre><code>public class DataClient implements Client {
  
      private final RestTemplate restTemplate = new RestTemplate();
      private ExecutorService executor = Executors.newFixedThreadPool(10);
  
      // for synchronous call
      @Override
      public String getSyncData(String key, long timeout) {
          String response = null;
  
          try {
              Task task = new Task(key, restTemplate, timeout);
  
              // direct call, implementing sync call as async + waiting is bad idea. 
              // It is meaningless and consumes one thread from the thread pool per a call.               
              response = task.call();
          } catch (Exception ex) {
              PotoLogging.logErrors(ex, DataErrorEnum.CLIENT_ERROR, key);
          }
  
          return response;
      }
  
      // for asynchronous call
      @Override
      public Future&lt;String&gt; getAsyncData(String key, long timeout) {
          Future&lt;String&gt; future = null;
  
          try {
              Task task = new Task(key, restTemplate, timeout);
              future = executor.submit(task); 
          } catch (Exception ex) {
              PotoLogging.logErrors(ex, DataErrorEnum.CLIENT_ERROR, key);
          }
  
          return future;
      }
  }
  </code></pre>
  
  <p>And below is my simple Task class</p>
  
  <pre><code>class Task implements Callable&lt;String&gt; {
  
      private RestTemplate restTemplate;
      private String key;
      private long timeout; // in milliseconds
  
      public Task(String key, RestTemplate restTemplate, long timeout) {
          this.key = key;
          this.restTemplate = restTemplate;
          this.timeout = timeout;
      }
  
      public String call() throws Exception {
  
          String url = "some_url_created_by_using_key";
  
          // does this looks right the way I am setting request factory?
          // or is there any other effficient way to do this?
          restTemplate.setRequestFactory(clientHttpRequestFactory());
          String response = restTemplate.exchange(url, HttpMethod.GET, null, String.class);
  
          return response;
      }
  
      private static ClientHttpRequestFactory clientHttpRequestFactory() {
          // is it ok to create a new instance of HttpComponentsClientHttpRequestFactory everytime?
          HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
          factory.setReadTimeout(timeout); // setting timeout as read timeout
          factory.setConnectTimeout(timeout); // setting timeout as connect timeout
          return factory;
      }   
  }
  </code></pre>
  
  <p>Now my question is - Does the way I am using <code>RestTemplate</code> along with <code>setRequestFactory</code> in the call method of Task class everytime is efficient? Since <code>RestTemplate</code> is very heavy to be created so not sure whether I got it right.</p>
  
  <p>And is it ok to create a new instance of <code>HttpComponentsClientHttpRequestFactory</code> everytime? Will it be expensive? </p>
  
  <p>What is the right and efficient way to use <code>RestTemplate</code> if we need to setup Read and Connection timeout on it.</p>
  
  <p>This library will be used like this - </p>
  
  <pre><code>String response = DataClientFactory.getInstance().getSyncData(keyData, 100);
  </code></pre>