title: How to use RestTemplate efficiently in Multithreaded environment?
body: <p>I am working on a project in which I need to make a HTTP URL call to my server which is running <code>Restful Service</code> which returns back the response as a JSON String.</p>
  
  <p>Below is my main code which is using the <code>future</code> and <code>callables</code> -</p>
  
  <pre><code>public class TimeoutThreadExample {
  
      private ExecutorService executor = Executors.newFixedThreadPool(10);
  
      public String getData() {
          Future&lt;String&gt; future = executor.submit(new Task());
          String response = null;
  
          try {
              response = future.get(100, 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 = new RestTemplate();
  
      public String call() throws Exception {
  
          String url = "some_url";
          String response = restTemplate.getForObject(url, String.class);
  
          return response;
      }
  }
  </code></pre>
  
  <p>And now I have below code in another class <code>DemoTest</code> which calls the <code>getData</code> method in <code>TimeoutThreadExample</code> class <code>5000 times</code> sequentially -</p>
  
  <pre><code>public class DemoTest { 
     public static void main(String[] args) {
  
          TimeoutThreadExample bc = new TimeoutThreadExample();
  
          for (int i = 0; i &lt;= 5000; i++) {
          //  TimerTest timer = TimerTest.getInstance(); // line 1
              bc.getData();
          //  timer.getDuration(); // line 2
          }
      }
  }       
  </code></pre>
  
  <p>So my question is should <code>RestTemplate</code> be static here in my <code>Task class</code> as if I see it correctly, I am recreating the whole connection pool for each request in <code>RestTemplate</code> which is not the right way I guess..</p>
  
  <p><strong>NOTE:</strong> If I am making RestTemplate static, then I see better performance end to end as compared to non static <code>RestTemplate</code> after commenting out line1 and line2 in <code>DemoTest</code> class which measures the performance.</p>
  
  <p>In general what is the right way to use <code>RestTemplate</code> in Multithreading environment? Currently I am calling sequentially <code>getData</code> method 5000 times one by one but some customer will call it in a multithreaded way so need to know what is the best way to have RestTemplate in a multithreaded environment..</p>
  
  <p>May be to use ConnectionFactory in the RestTemplate constructor? Any thoughts?</p>
  
  <p><strong>UPDATE:-</strong></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(100, TimeUnit.MILLISECONDS);
          } catch (TimeoutException e) {
              e.printStackTrace();
          } catch (InterruptedException e) {
              e.printStackTrace();
          } catch (ExecutionException e) {
              e.printStackTrace();
          }
  
          return response;
      }
  }
  </code></pre>
  
  <p>And below my <code>TaskClass</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>