spring-cloud-streamspring-cloud-stream-binder-kafkaspring-cloud-function

stream bridge provisions a topic in Kafka with the binding name for input binding of a function


I have a function publish for which I've defined the binding too

@Bean
public Function<ProducerPayload<T>, Message<ProducerPayload<T>>> publish() {
// some magic here
}

the config properties:

spring.cloud.function.definition=publish

and

spring.cloud.stream.bindings.publish-out-0.destination=${category}-${spring.application.name}-${runtime-env}

When I call this via streamBridge I call the publish-in-0 binding

streamBridge.send("publish-in-0", "kafka", ProducerPayload.builder().event(event).content(entity).build(), MimeType.valueOf("application/json"));

this leads to creation of a Kafka topic publish-in-0 which is always empty, the message however does land up in the right topic as defined in the publish-out-0 destination. Why is this topic getting created? I was assuming a message channel by that name is registered and invoked by spring boot cloud stream.


Solution

  • That usage of StreamBridge is incorrect - StreamBridge is for sending to output bindings, not input bindings; unknown output bindings are bound on-demand. The fact that your function is receiving the data is probably by chance; it was certainly not designed that way.

    My guess is that the underlying channel has 2 subscribers - in the input function and the output binding; if you send a second message, it will probably go to the topic (round robin distribution across the 2 subscribers).

    If you want to send directly to the function, you should publish directly to its input channel, rather than using the StreamBridge.