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

Viewed 25

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.

2 Answers

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.

That is correct.

Since you are not mapping your binding to destination, the destination name becomes the same as the binding name. In your case you are mapping publish-out-0 (the output of the function), but you are not mapping publish-in-0 (the input of the function).

Related