1

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.

Anadi Misra
  • 1,925
  • 4
  • 39
  • 68

2 Answers2

2

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.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Thanks! I tried `@Qualifier("publish-in-0") @Autowired MessageChannel publishChannel;` and then `publishChannel.send(MessageBuilder.withPayload(...).build());` is this the right way? – Anadi Misra Sep 16 '22 at 09:35
1

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).

Oleg Zhurakousky
  • 5,820
  • 16
  • 17