title: executing a method in parallel from a call method
body: <p>I have a library which is being used by customer and they are passing <code>DataRequest</code> object which has <code>userid</code>, <code>timeout</code> and some other fields in it. Now I use this <code>DataRequest</code> object to make a URL and then I make an HTTP call using <code>RestTemplate</code> and my service returns back a JSON response which I use it to make a <code>DataResponse</code> object and return this <code>DataResponse</code> object back to them.</p>
  
  <p>Below is my <code>DataClient</code> class used by customer by passing <code>DataRequest</code> object to it. I am using timeout value passed by customer in <code>DataRequest</code> to timeout the request if it is taking too much time in <code>getSyncData</code> method.</p>
  
  <pre><code>public class DataClient implements Client {
  
      private RestTemplate restTemplate = new RestTemplate();
      // first executor
      private ExecutorService service = Executors.newFixedThreadPool(15);
  
      @Override
      public DataResponse getSyncData(DataRequest key) {
          DataResponse response = null;
          Future&lt;DataResponse&gt; responseFuture = null;
  
          try {
              responseFuture = getAsyncData(key);
              response = responseFuture.get(key.getTimeout(), key.getTimeoutUnit());
          } catch (TimeoutException ex) {
              response = new DataResponse(DataErrorEnum.CLIENT_TIMEOUT, DataStatusEnum.ERROR);
              responseFuture.cancel(true);
              // logging exception here               
          }
  
          return response;
      }   
  
      @Override
      public Future&lt;DataResponse&gt; getAsyncData(DataRequest key) {
          DataFetcherTask task = new DataFetcherTask(key, restTemplate);
          Future&lt;DataResponse&gt; future = service.submit(task);
  
          return future;
      }
  }
  </code></pre>
  
  <p><code>DataFetcherTask</code> class:</p>
  
  <pre><code>public class DataFetcherTask implements Callable&lt;DataResponse&gt; {
  
      private DataRequest key;
      private RestTemplate restTemplate;
  
      public DataFetcherTask(DataRequest key, RestTemplate restTemplate) {
          this.key = key;
          this.restTemplate = restTemplate;
      }
  
      @Override
      public DataResponse call() throws Exception {
          // In a nutshell below is what I am doing here. 
          // 1. Make an url using DataRequest key.
          // 2. And then execute the url RestTemplate.
          // 3. Make a DataResponse object and return it.
  
          // I am calling this whole logic in call method as LogicA
      }
  }
  </code></pre>
  
  <p>As of now my <code>DataFetcherTask</code> class is responsible for one <code>DataRequest</code> key as shown above..</p>
  
  <p><strong>Problem Statement:-</strong></p>
  
  <p>Now I have a small design change. Customer will pass <code>DataRequest</code> (for example keyA) object to my library and then I will make a new http call to another service (which I am not doing in my current design) by using user id present in <code>DataRequest</code> (keyA) object which will give me back list of user id's so I will use those user id's and make few other <code>DataRequest</code> (keyB, keyC, keyD) objects one for each user id returned in the response. And then I will have <code>List&lt;DataRequest&gt;</code> object which will have keyB, keyC and keyD <code>DataRequest</code> object. Max element in the <code>List&lt;DataRequest&gt;</code> will be three, that's all.</p>
  
  <p>Now for each of those <code>DataRequest</code> object in <code>List&lt;DataRequest&gt;</code> I want to execute above <code>DataFetcherTask.call</code> method in parallel and then make <code>List&lt;DataResponse&gt;</code> by adding each <code>DataResponse</code> for each key. So I will have three parallel calls to <code>DataFetcherTask.call</code>. Idea behind this parallel call is to get the data for all those max three keys in the same global timeout value.</p>
  
  <p>So my proposal is -  <code>DataFetcherTask</code> class will return back <code>List&lt;DataResponse&gt;</code> object instead of <code>DataResponse</code> and then signature of <code>getSyncData</code> and <code>getAsyncData</code> method will change as well. So here is the algorithm:</p>
  
  <ul>
  <li>Use DataRequest object passed by customer to make <code>List&lt;DataRequest&gt;</code> by calling another HTTP service.</li>
  <li>Make a parallel call for each <code>DataRequest</code> in <code>List&lt;DataRequest&gt;</code> to <code>DataFetcherTask.call</code> method and return <code>List&lt;DataResponse&gt;</code> object to customer instead of <code>DataResponse</code>.</li>
  </ul>
  
  <p>With this way, I can apply same global timeout on step 1 along with step 2 as well. If either of above step is taking time, we will just timeout in <code>getSyncData</code> method.</p>
  
  <p><code>DataFetcherTask</code> class after design change:</p>
  
  <pre><code>public class DataFetcherTask implements Callable&lt;List&lt;DataResponse&gt;&gt; {
  
      private DataRequest key;
      private RestTemplate restTemplate;
      // second executor here
      private ExecutorService executorService = Executors.newFixedThreadPool(10);
  
      public DataFetcherTask(DataRequest key, RestTemplate restTemplate) {
          this.key = key;
          this.restTemplate = restTemplate;
      }
  
      @Override
      public List&lt;DataResponse&gt; call() throws Exception {
          List&lt;DataRequest&gt; keys = generateKeys();
          CompletionService&lt;DataResponse&gt; comp = new ExecutorCompletionService&lt;&gt;(executorService);
  
          int count = 0;
          for (final DataRequest key : keys) {
              comp.submit(new Callable&lt;DataResponse&gt;() {
                  @Override
                  public DataResponse call() throws Exception {
                      return performDataRequest(key);
                  }
              });
          }
  
          List&lt;DataResponse&gt; responseList = new ArrayList&lt;DataResponse&gt;();
          while (count-- &gt; 0) {
              Future&lt;DataResponse&gt; future = comp.take();
              responseList.add(future.get());
          }
          return responseList;
      }
  
      // In this method I am making a HTTP call to another service
      // and then I will make List&lt;DataRequest&gt; accordingly.
      private List&lt;DataRequest&gt; generateKeys() {
          List&lt;DataRequest&gt; keys = new ArrayList&lt;&gt;();
          // use key object which is passed in contructor to make HTTP call to another service
          // and then make List of DataRequest object and return keys.
          return keys;
      }       
  
      private DataResponse performDataRequest(DataRequest key) {
          // This will have all LogicA code here which is shown in my original design.
          // everything as it is same..
      }
  }
  </code></pre>
  
  <p>Now my question is - </p>
  
  <ul>
  <li>Does it have to be like this? What is the right design to solve this problem? I mean having <code>call</code> method in another <code>call</code> method looks weird? </li>
  <li>Do we need to have two executors like I have in my code? Is there any better way to solve this problem or any kind of simplification/design change we can do here?</li>
  </ul>
  
  <p>I have simplified the code so that idea gets clear what I am trying to do..</p>