title: How to return response immediate to client in spring flux by controlling the no of thread using ExecutorService and CompletableFuture?
body: <p>I needed to call two downstream systems parallelly with non-blocking io from Spring flux-based my rest service API. But the first downstream system capacity is 10 requests at a time and the second downstream system is 100.</p>
  
  <p>The first downstream system out is input to the second downstream system so I can make a more parallel request to the second system to expedite the process. </p>
  
  <p>The second downstream system response is very large so unable to hold in memory to concrete all the response So immediate want to return the response to the client. </p>
  
  <p>Ex workflow: </p>
  
  <p><a href="https://i.stack.imgur.com/W3osS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/W3osS.png" alt="enter image description here"></a></p>
  
  <p>Sample Code:</p>
  
  <pre><code>@GetMapping(path = "/stream", produces = MediaType.APPLICATION_STREAM_JSON_VALUE)
  public Flux&lt;String&gt; getstream() {
  
      ExecutorService executor = Executors.newFixedThreadPool(10);
  
      List&lt;CompletableFuture&gt; list = new ArrayList&lt;&gt;();
  
      AtomicInteger ai = new AtomicInteger(1);
      RestTemplate restTemplate = new RestTemplate();
  
      for (int i = 0; i &lt; 100; i++) {
          CompletableFuture&lt;Object&gt; cff = CompletableFuture.supplyAsync(
  
                  () -&gt; ai.getAndAdd(1) + " first downstream web service " +
                          restTemplate.getForObject("http://dummy.restapiexample.com/api/v1/employee/" + ai.get(), String.class)
  
          ).thenApplyAsync(v -&gt; {
  
              Random r = new Random();
              Integer in = r.nextInt(1000);
  
              return v + " second downstream web service  " + in + " " + restTemplate.getForObject("http://dummy.restapiexample.com/api/v1/employee/" + ai.get() + 1, String.class) + " \n";
          }, executor);
  
          list.add(cff);
      }
  
      return Flux.fromStream(list.stream().map(m -&gt; {
                  try {
                      return m.get().toString();
                  } catch (InterruptedException | ExecutionException e) {
                      e.printStackTrace();
                  }
                  return "";
              })
      );
  
  }
  </code></pre>
  
  <p>This code only working for the first five threads after I am getting a response all threads completed the process. But I needed to get a response immediately to the client once I am getting the response from the second downstream system.</p>
  
  <p>Note: The above code is not implemented with a second level thread pool. </p>
  
  <p>Thank you in advance.  </p>