title: How to parallelize webservice requests with CompletableFuture?
body: <p>I have a servlet request that basically requests data given by an input date. As I have multiple dates, I have to send multiple requests, and then aggregate the results. For example:</p>
  
  <pre><code>List&lt;Result&gt; results = new ArrayList&lt;&gt;();
  
  for (LocalDate date : dates) {
      ServletReq req = new ServletReq(date);
  
      try {
          ServletRsp rsp = webservice.send(req);
          results.addAll(rsp.getResults());
      } catch (SpecificException e) {
          //just ignore this result and continue
      }
  }
  </code></pre>
  
  <p>Question: how can I parallelize the code above? Means: sending multiple <code>ServletReq</code> async, and collect the result into the list. Wait for all requests to finish (maybe with a timeout), and ignore the <code>SpecificException</code>.</p>
  
  <p>I started as follows, but neither do I know if this is the right direction, nor did I succeed transfering the code above completely. Especially regarding the exception to be ignored.</p>
  
  <pre><code>ExecutorService service = Executors.newCachedThreadPool();
  List&lt;CompletableFuture&lt;ServletRsp&gt;&gt; futures = new ArrayList&lt;&gt;();
  
  for (LocalDate date : dates) {
      ServletReq req = new ServletReq(date);
      CompletableFuture future = CompletableFuture.supplyAsync(() -&gt; webservice.send(req), service);
      futures.add(future);
  }
  
  CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).join();
  </code></pre>
  
  <p>So far, but: How can I call <code>rsp.getResults()</code> on the async result, and put everything into the <code>list</code>. And how can I ignore the <code>SpecificException</code> during the async execution? (I cannot modify the <code>webservice.send()</code> method!).</p>