spring-integrationspring-integration-dsl

How to extend Spring Integration IntegrationFlowBuilder with convenience functions?


We are using Spring Integration Flows in a project to create an api out of incoming kafka events. While using these flows certain patterns have emerged in how we use them. To keep our code short and concise I would like to provide quality of life functionality.

E.g. instead of:

    @Bean
    public IntegrationFlow sampleFlow() {
        return IntegrationFlow
                .from("sampleChannel")
                .filter(locationFilter::accept, spec -> spec.discardChannel(nullChannel))
                .get();
    }

I would like to be able to write:

    @Bean
    public IntegrationFlow sampleFlowConcise() {
        return IntegrationFlow
                .from("sampleChannel")
                .filterWithoutLogging(locationFilter::accept)
                .get();
    }

Or instead of

                .transformWith(spec -> spec
                        .transformer(basicTransformer::transform)
                        .requiresReply(false))

I would like to write

                .transformAllowNull(basicTransformer::transform)

My problem is that I cannot find the correct way to extend the IntegrationFlowBuilder or similiar classes from the Spring Integration Framework to achieve this. Has anyone done this before and has a code sample for me?

I have already found this documentation from spring, but this doesnt allow me to use the existing IntegrationFlowBuilder functionality. https://docs.spring.io/spring-integration/reference/dsl/java-extensions.html

I have tried extending IntegrationFlowDefinition and BaseIntegrationFlowDefinition in a similiar way as it is used in the spring integration source code. Problem is that these classes are all intertwinded, e.g. IntegrationFlow.from() directly returns an instance of IntegrationFlowBuilder, which uses IntegrationFlowDefinition.

So i don't see a way to connect a custom class in between or extend one of the classes.


Solution

  • The IntegrationFlowExtension is exactly what you are looking for.

    So, you declare a custom class extending this IntegrationFlowExtension and define there your custom operators.

    Then you use return new YourCustomFlowDefiniiton() and so on.

    These custom flows starts from a channel with a name of the IntegrationFlow bean definition plus .input suffix.

    I wonder why the sample is in the mentioned doc is not enough for you.

    And what IntegrationFlowBuilder functionality doesn't work with that extension?