0

In my use case I need to make 2 REST calls to fetch a list of items based on departmentId. I need to merge the 2 lists and process them.

I am using the scatterGather pattern, i can see that the fetchRoles & fetchGroups are being called but i dont see "Aggregated List:" is printed at the end. Can some one help me what is wrong in the code

@Bean
public IntegrationFlow processDomainFileFlow() {
    return IntegrationFlows
            .from("receiverChannel")
            .scatterGather(scatterer -> scatterer
                            .applySequence(true)
                            .recipientFlow(fetchRoles())
                            .recipientFlow(fetchGroups()))
            .log(INFO, CATEGORY, m -> "Aggregated List: " + m.getPayload())
            .get();
}

@Bean
public IntegrationFlow fetchRoles() {
    return IntegrationFlows.from("fetch.roles")
            .handle(outboundGateway( someServiceUrl + "/{departmentId}/roles")
                    .uriVariable("departmentId", m -> m.getHeaders().get("departmentId"))
                    .httpMethod(HttpMethod.GET)
                    .expectedResponseType(Item[].class))
            .get();
}

@Bean
public IntegrationFlow fetchGroups() {
    return IntegrationFlows.from("fetch.groups")
            .handle(outboundGateway(someServiceUrl + "/{departmentId}/groups")
                    .uriVariable("departmentId", m -> m.getHeaders().get("departmentId"))
                    .httpMethod(HttpMethod.GET)
                    .expectedResponseType(Item[].class))
            .get();
}
nagendra
  • 593
  • 1
  • 9
  • 29

1 Answers1

0

As far as you use a default correlation strategy in the gatherer, you are missing the

/**
 * @param applySequence the applySequence.
 * @return the router spec.
 * @see AbstractMessageRouter#setApplySequence(boolean)
 */
public S applySequence(boolean applySequence) {

on the scatterer to let it to populate a standard sequence details headers to let a default correlation logic to do its job based on the provided sequence details headers: https://docs.spring.io/spring-integration/reference/html/messaging-routing-chapter.html#scatter-gather-functionality

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • Also, this `.releaseStrategy(group -> group.size() > 0))` looks suspicious - it will release when only one message is in the group. – Gary Russell Oct 16 '18 at 15:16
  • Yeah... That's good catch, indeed, but at the same time this is slightly different story... – Artem Bilan Oct 16 '18 at 15:18
  • It didn't work even after adding `.applySequence(true)`. I kept some debug stmts in releaseStrategy which are not executed. I thought the default releaseStrategy is on aggregate completion hence i mentioned group.size() > 0, only go to next step when the result is more than zero – nagendra Oct 17 '18 at 10:10
  • 1
    Try to remove `gatherer` argument altogether from there. This way it is going to be based on the default aggregation strategy which comes from the `sequenceDetails` headers by the mentioned `.applySequence(true)` – Artem Bilan Oct 17 '18 at 15:45
  • I have a logger between `handle()` and `get()` `.log(DEBUG, CATEGORY, m -> "Roles fetched: " + m.getPayload().size())`, when i comment the logger then only it worked. Not sure how logger is affecting the flow – nagendra Oct 18 '18 at 09:55
  • 1
    `log()` in the end of the flow is terminal operator. No reply is going to sent after that – Artem Bilan Oct 18 '18 at 12:01
  • I have to write this `.>handle((p, h) -> p.stream().flatMap(m -> Arrays.asList(m).stream()).collect((Collectors.toList())))` in order to merge the 2 responses. – nagendra Oct 18 '18 at 13:23