Search code examples
spring-integrationspring-integration-dsl

Spring Integration DSL: Simple way to send some messages to a Flow inputChannel


If I want to generate some sample data for testing purposes of the Spring Integration DSL functionality, one way I have come up with so far is like this:

@Bean
public IntegrationFlow myFlow() {
    return IntegrationFlows
            .from(Http.inboundChannelAdapter("numbers").get())
            .scatterGather(s -> s
                    .applySequence(true)
                    .recipientFlow(f -> f.handle((a, b) -> Arrays.asList(1,2,3,4,5,6,7,8,9,10)))
            )
            .split() // unpack the wrapped list
            .split() // unpack the elements of the list
            .log()
            .get();
}

Is there another/better way to do the same thing ? Using the Scatter-Gather EIP seems like overkill for something so basic...


Solution

  • I found a simpler way of doing the same thing using .transform() instead of .scatterGather()

    @Bean
    public IntegrationFlow testFlow() {
        return IntegrationFlows
                .from(Http.inboundChannelAdapter("test").get())
                .transform(t -> Arrays.asList(1,2,3,4,5,6,7,8,9,10))
                .split() // unpack the wrapped list
                .split() // unpack the elements of the list
                .get();
    }
    

    And here's another way you could do it using .gateway()

    @Bean
    public IntegrationFlow testFlow() {
        return IntegrationFlows
                .from(Http.inboundChannelAdapter("test").get())
                .gateway(f -> f.handle((a, b) -> Arrays.asList(1,2,3,4,5,6,7,8,9,10)))
                .split() // unpack the elements of the list
                .get();
    }
    

    And yet another:

    @Bean
    IntegrationFlow fromSupplier() {
        return IntegrationFlow.fromSupplier(() -> List.of(1,2,3,4), 
                        s -> s.poller(p -> p.fixedDelay(1, TimeUnit.DAYS)))
                .log(Message::getPayload)
                .nullChannel();
    }