0

I have a code that uses KafkaTemplate to send message into different Kafka topics. Topic name is received from API and can't be known in advance, the code looks like:

var message = MessageBuilder.fromMessage(message)
                    .setHeader(KafkaHeaders.TOPIC, targetTopic)
                    .build();
kafkaTemplate.send(message);

Now I want to refactor existing approach to use Spring Cloud Stream but it requires to define in application.properties topic name which I can't know.

spring.cloud.stream.bindings.output.binder=kafka
spring.cloud.stream.bindings.output.destination=user-events

The code I use to send message with binders approach looks like:

// enable bindning in config class
@EnableBinding(KafkaBinding.class)
// interfaces with output() method
@Output("output")
MessageChannel output();
// sending message to topic from application.properties
kafkaBinding.output().send(message)

How can I set topic name in runtime using approach with binders?

boden
  • 1,621
  • 1
  • 21
  • 42

1 Answers1

1

@EnableBinding is deprecated in favor of the newer, preferred functional style.

You can use the StreamBridge to send to dynamic destinations.

https://docs.spring.io/spring-cloud-stream/docs/3.1.1/reference/html/spring-cloud-stream.html#_streambridge_and_dynamic_destinations

Gary Russell
  • 166,535
  • 14
  • 146
  • 179