Search code examples
spring-cloudspring-cloud-stream

How to set up a splitter using Spring Cloud Stream 3.x's new functional binding style?


I'm trying to wrap my head around the new functional binders in Spring Cloud Stream 3.x/

In the legacy style, I have a @StreamListener that receives a Message, then may produce 0..n messages on its output:

@StreamListener(Channels.INPUT)
public void handle(@Valid @Payload Payload payload) {

  // do stuff, then send multiple messages to the output channel:
  channels.output().send(result); // Do this 0..n times

}

In the new functional style, instead of a listener, I would define a bean like so:

@Bean
public Function<Payload, Result> processPayload() {
    return value -> {
        System.out.println("Received: " + payload);
        // do stuff
        return result; // (only one?)
    };
}
  1. How should I define a Function bean if I want to return 0..n Result in response to a single received Payload? (This is a splitter!)
  2. I was able to use @Valid to trigger bean validation with the legacy setup. Is there similar shorthand in functional land, or do I get to instantiate my own Validator?

Solution

  • Use Consumer<Payload> instead of Function together with a StreamBridge.

    Docs here.