Search code examples
spring-integrationspring-integration-dsl

Spring integration scatterGather usage


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();
}

Solution

  • 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