Search code examples
javaspringspring-integrationdsl

Spring-Integration Filter when predicate is false within request-reply flow


I'm trying to use filter() in order to execute certain code conditionally and yet always return a reply in a request-reply flow.

For instance, in Spring-Integration 5.5.17, in the IntegrationFlow, I can conditionally multiply odd integer payloads by two.

    @Bean(name = "sendsEvenValuesWorksFlow")
    IntegrationFlow sendEvenValuesWorksFlow() {
        return IntegrationFlows.from("sendsEvenValuesWorksChannel")
                               .filter("(payload % 2) == 1", filterSpec -> filterSpec.discardFlow(noop()))
                               .transform("payload * 2")
                               .get();
    }

    private static IntegrationFlow noop() {
        return f -> f.filter("true");
    }

where the filter has a discardFlow which does nothing.

But I would think that there would be a simpler way of continuing the flow with the current payload. The following doesn't work, for instance, because it produces no output message:

    @Bean(name = "sendsEvenValuesFailsFlow")
    IntegrationFlow sendEvenValuesFailsFlow() {
        return IntegrationFlows.from("sendsEvenValuesFailsChannel")
                               .filter("(payload % 2) == 1")
                               .transform("payload * 2")
                               .get();
    }

Is a discard mechanism (flow or channel) obligatory in order to guarantee a reply message is produced with a filter() method? If so, is there a simpler way of creating a noop() flow?

Thanks for any pointers.


Solution

  • Well, if you question is to just reply the same input payload, then your noop would look like this:

    private static IntegrationFlow noop() {
        return f -> f.bridge();
    }
    

    The bridge is a simple pass-through request-reply handler, and if there is no output channel, it consults a replyChannel header.