3

I have the following code where I branch into recipientFlows in the mainFlow and then process the result of those recipientFlows in a separate persistToDbFlow, via a direct channel PERSIST_TO_DB_CHANNEL:

@Bean
IntegrationFlow mainFlow() {
    return IntegrationFlows
            .from(Http.inboundChannelAdapter("some/path")
                    .requestMapping(mapping -> mapping.methods(HttpMethod.GET))
                    .get())
            .routeToRecipients(route -> route
                    .recipientFlow("some SpEL Expression", IntegrationFlows
                            .from(MessageChannels.executor(executorService))
                            .gateway(getSomeDataFromSourceA_Flow())
                            .channel(PERSIST_TO_DB_CHANNEL)
                            .get())
                    .recipientFlow("some other SpEL Expression", IntegrationFlows
                            .from(MessageChannels.executor(executorService))
                            .gateway(getSomeDataFromSourceB_Flow())
                            .channel(PERSIST_TO_DB_CHANNEL)
                            .get())
                    .defaultSubFlowMapping(flow -> flow
                            .log(WARN)
                            .nullChannel()))
            .get();
}

@Bean
IntegrationFlow persistToDbFlow() {
    return IntegrationFlows.from(PERSIST_TO_DB_CHANNEL)
            .gateway(/* persist to DB */)
            .nullChannel();
}

Is there any way I can do the same thing without the need to create a separate persistToDbFlow for the persistence logic, and instead keep the persistence logic as part of the mainFlow, similar to how scatter-gather lets you gather the results back to the main flow ?

I imagine it looking something like this:

@Bean
IntegrationFlow mainFlow() {
    return IntegrationFlows
            .from(Http.inboundChannelAdapter("some/path")
                    .requestMapping(mapping -> mapping.methods(HttpMethod.GET))
                    .get())
            .routeToRecipients(route -> route
                    .recipientFlow("some SpEL Expression", IntegrationFlows
                            .from(MessageChannels.executor(executorService))
                            .gateway(getSomeDataFromSourceA_Flow())
                            .returnToParentChannel())
                    .recipientFlow("some other SpEL Expression", IntegrationFlows
                            .from(MessageChannels.executor(executorService))
                            .gateway(getSomeDataFromSourceB_Flow())
                            .returnToParentChannel())
                    .defaultSubFlowMapping(flow -> flow
                            .log(WARN)
                            .nullChannel()))
            .gateway(/* persist to DB */)
            .get();
}
Chris
  • 4,212
  • 5
  • 37
  • 52

1 Answers1

0

Here's a very convoluted solution. I'm still hoping to find an easier way to do this:

return flow -> flow
    .enrich(e -> e
        .requestSubFlow(enricherFlow -> enricherFlow
            .routeToRecipients(route -> route
                .recipientFlow(flow2 -> flow2
                    .log(m -> "do something")
                    .channel("someChannel"))
                .recipientFlow(flow2 -> flow2
                    .log(m -> "do something else")
                    .channel("someChannel"))))
        .replyChannel("someChannel"))
    .logAndReply(m -> "continue the main flow");
Chris
  • 4,212
  • 5
  • 37
  • 52