Search code examples
javaspringspring-bootspring-cloud-stream

What does the spring.cloud.stream.source really do?


I am trying to understand how the new functional model of Spring Cloud Streams works and how the configuration actually works under the hood.

One of the properties I am unable to figure out is spring.cloud.stream.source.

What does this property actually signify ?

I could not understand the documentation :

Note that preceding example does not have any source functions defined (e.g., Supplier bean) leaving the framework with no trigger to create source bindings, which would be typical for cases where configuration contains function beans. So to trigger the creation of source binding we use spring.cloud.stream.source property where you can declare the name of your sources. The provided name will be used as a trigger to create a source binding.

What if I did not need a Supplier ?

What exactly is a source binding and why is it important ?

What if I only wanted to produce to a messaging topic ? Would I still need this property ?

I also could not understand how it is used in the sample here.


Solution

  • Spring cloud stream looks for java.util Function<?, ?, Consumer<?>, Supplier<?> beans and creates bindings for them.

    In the supplier case, the framework polls the supplier (each second by default) and sends the resulting data.

    For example

    @Bean
    public Supplier<String> output() {
        return () -> "foo";
    }
    
    spring.cloud.stream.bindings.output-out-0.destination=bar
    

    will send foo to destination bar each second.

    But, what if you don't need a polled source, but you want to configure a binding to which you can send arbitrary data. Enter spring.cloud.stream.source.

    spring.cloud.stream.source=output
    spring.cloud.stream.bindings.output-out-0.destination=bar
    

    allows you to send arbitrary data to the stream bridge

    bridge.send("output-out-0", "test");
    

    In other words, it allows you to configure one or more ouput bindings that you can use in the StreamBridge; otherwise, when you send to the bridge, the binding is created dynamically.