To build on Artem's answer, this can be a bit tricky because of the asynchronicity of the events involved.
If the topic doesn't exist in the Kafka broker the describeTopics
method in KafkaAdminClient
throws an UnknownTopicOrPartitionException
wrapped in an ExecutionException
, so we can leverage that to check the topic's existence and wait for its creation or deletion.
EDIT: While the above strategy does work, the proper way to do this is to just wait for the future to complete, for example with admin.deleteTopics(List.of(topicName)).all().get()
But since the Embedded broker is not aware that the topic has been deleted, the embeddedKafkaBroker.getTopics()
result doesn't get updated, so in order to find out if the topic has actually been deleted we need to use the admin.describeTopics(List.of(topicName)).all().get()
method instead.