Search code examples
spring-integrationspring-integration-dsl

Spring Integration - scatter-gather - dynamic value in releaseExpression


I am using scatter-gatter in a Spring Integration flow to call 3 services parallel:

                    .scatterGather(rlr -> rlr.applySequence(true)
                                             .recipientFlow(f1 -> f1
                                                    .channel(c -> c.executor(executorMLCalls))
                                                    .route(ifService1NeedsToBeCalled(), routeToService1OrBypassCall()))
                                             .recipientFlow(f2 -> f2
                                                    .channel(c -> c.executor(executorMLCalls))
                                                    .enrich(this::service2RequestEnricher)
                                                    .enrich(this::service2Enricher))
                                             .recipientFlow(f3 -> f3
                                                    .channel(c -> c.executor(executorMLCalls))
                                                    .enrich(this::service3RequestEnricher)
                                                    .enrich(this::service3Enricher)),
                                   agg -> agg.correlationStrategy(msg -> msg.getHeaders().get("corrMLCalls"))
                                             .releaseExpression("size() == 3"),
                                   sgs -> sgs.gatherTimeout(gatherTimeout)
                                             .requiresReply(true)
                    )

So the service2 and service3 have to be called always but calling the service1 depends on conditions.
Due to this I cannot use the size() == 3 release expression as sometimes it should be size() == 2.

How can I use dynamic value in the expression? Can I call a local method in the release expression?

Thank you!

Regards,
V.


Solution

  • Yes, such an expression is BeanFactory-aware and you can use a @ syntax to reach any bean in your application context.

    On the other hand there is a:

    /**
     * @param releaseStrategy the release strategy.
     * @return the handler spec.
     * @see AbstractCorrelatingMessageHandler#setReleaseStrategy(ReleaseStrategy)
     */
    public S releaseStrategy(ReleaseStrategy releaseStrategy) {
    

    Which may make any decission according your environment and MessageGroup as a releasing candidate.