5

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.

ng.newbie
  • 2,807
  • 3
  • 23
  • 57

1 Answers1

5

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.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Feel free to open an issue to ask for clarification https://github.com/spring-cloud/spring-cloud-stream/issues – Gary Russell Sep 16 '21 at 18:25
  • If I want to send arbitrary data via the stream-bridge, then why even configure a `Supplier` bean ? I will just create a binding and use that binding to send the arbitrary data. I am still not convinced of the need for `spring.cloud.stream.source`. – ng.newbie Sep 20 '21 at 16:29
  • What I mean is that I can do something like this: `spring.cloud.stream.bindings.myBinding.destination=myTopic`. And I can just use `myBinding` in `StreamBridge`. I don't even need `spring.cloud.stream.source`. – ng.newbie Sep 20 '21 at 16:43
  • Also please note that [github.com/spring-cloud/spring-cloud-stream/issues](https://github.com/spring-cloud/spring-cloud-stream/issues) specifically notes that questions asking for clarification are strictly forbidden. **Please ask questions about how to use something, or to understand why something isn't working as you expect it to, on Stack Overflow using the spring-cloud-stream tag.** – ng.newbie Sep 20 '21 at 16:52
  • `destination` is a property that is applied to a binding; binding properties do not trigger the creation of a binding, they are used to customize it. Producer binding creation is triggered by one of three things - the presence of a `Supplier>` bean, or a `Function, ?>` bean, or the value of the `source` property. I am just telling you how it IS, I didn't design it (although I am not sure I would have done it any differently). I didn't suggest you ask for clarification in an ISSUE, I suggested you open one to request a change to the docs to make it clearer, if you think it's not clear. – Gary Russell Sep 21 '21 at 12:25
  • 1
    Of course, the binding can be created dynamically on the first send (and the properties will be applied). The `source` is only needed if you want to create the binding during application initialization - i.e. to detect any misconfiguration then, rather than when you first send a message. – Gary Russell Sep 21 '21 at 12:29
  • Opened the issue: https://github.com/spring-cloud/spring-cloud-stream/issues/2226 – ng.newbie Sep 23 '21 at 12:33