Search code examples
apache-kafkarabbitmqspring-cloud-streamspring-rabbitspring-cloud-stream-binder-kafka

Spring cloud stream create and delete topics programmatically


I have a requirement where I need to create and delete Kafka topics programmatically. org.springframework.cloud.stream.binding.BinderAwareChannelResolver . resolveDestination(String channelName) can create a topic, but this is deprecated. Also, there is no way to delete Kafka topic programmatically which I am aware of.

Any suggestions to achieve this are welcome!


Solution

  • You can use an AdminClient to create and delete topics. Spring Boot will auto-configure a KafkaAdmin and you can use kafkaAdmin.getConfigurationProperties() to create an admin client with AdminClient.create(props) (or you can just use your own properties).