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?