Spring 集成 scatterGather 用法

Spring integration scatterGather usage

在我的用例中,我需要进行 2 次 REST 调用以获取基于 departmentId 的项目列表。我需要合并 2 个列表并处理它们。

我正在使用 scatterGather 模式,我可以看到正在调用 fetchRoles 和 fetchGroups,但我没有看到最后打印 "Aggregated List:"。谁能帮我看看代码有什么问题

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

就您在 gatherer 中使用默认关联策略而言,您错过了

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

scatterer 上让它填充标准序列详细信息 headers 让默认关联逻辑根据提供的序列详细信息完成其工作 headers:https://docs.spring.io/spring-integration/reference/html/messaging-routing-chapter.html#scatter-gather-functionality