title: Spring cloud stream merge responses from two different functions
body: <p>I'm trying to use spring cloud stream to solve the following problem:</p>
  <p><a href="https://i.stack.imgur.com/KBPZE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KBPZE.png" alt="enter image description here" /></a></p>
  <p>I have a class that calls two separated functions (Function A and B), both of those functions must work in parallel if the Function A finishes it must call the Function C, the same happens if Function B finish but this will call Function D, then I need to wait for Function C and Function D to finish and merge both responses in a single response, and then return this merged response object to the starting class that must be waiting to get that response.</p>
  <p>The problems I have are:</p>
  <ul>
  <li>How do I call Function C to pass the Function A response?</li>
  <li>How to wait until Function C and Function D finish and get their responses in Function E?</li>
  <li>How to wait for the response of Function E in the controller, I'm using <code>streamBridge.send</code> which to start Function A and Function B at the same time.</li>
  </ul>
  <p>I'm using</p>
  <ul>
  <li><code>spring-cloud-stream-3.1.3</code></li>
  <li><code>spring-cloud-stream-binder-rabbit</code> <strong>Required</strong></li>
  </ul>
  <p>I cannot use <code>Kafka</code> <strong>Required</strong></p>
  <p><strong>Sample code</strong></p>
  <p><em>ServiceClass</em></p>
  <pre><code>@Service
  @RequiredArgsConstructor
  public class ServiceClass {
  
      @NonNull
      private final StreamBridge streamBridge;
  
      @Override
      protected MergedResponse execute(Input input) {
          var send1 = streamBridge.send(&quot;functionA-in-0&quot;, input);
          var send2 = streamBridge.send(&quot;functionB-in-0&quot;, input);
  
          //TODO: Wait for Function E response object
      }
  }
  
  </code></pre>
  <p><em>Function A</em></p>
  <pre><code>@Slf4j
  @Configuration
  public class FunctionAClass{
  
      @Bean
      public Function&lt;Input, OutputFunctionA&gt; functionA() {
          return input -&gt; {
              //TODO: Invoke Function C to pass OutputFunctionA object
              return OutputFunctionA.builder.build();
          };
      }
  }
  
  </code></pre>
  <p>I don't mind using <code>Supplier</code> or <code>Consumer</code> instead of <code>Function</code>.</p>
  <p><strong>Edit</strong>
  Hi, @Oleg Zhurakousky thanks for your help, to answer your question my problem is: I have to create a REST endpoint that consume <code>N</code> different third-party REST endpoints (two at first, async is a must as it will be too slow to process every request sequentially) I don't need all the data from them, just a few fields to build a common object. I'm planning to used <code>streamBridge</code> to start the first two functions that will to build the required request for each third-party, then a function to invoke each of their endpoints, then a function to build a common object with each response, and finally a function that collects the commons objects and send them in the response of my service. Let me know if you have another question.</p>
  <p>Regards.</p>