Daniel
Daniel

Reputation: 31

How to stop and start the Embedded Kafka in unit test?

I use Embedded Kafka to test send message to Kafka, when send failed, my code will re-send automatically, so I try to stop the Embedded Kafka then restart it during re-sending. But I don't know how to stop and start the Embedded Kafka.

Upvotes: 3

Views: 5815

Answers (1)

Chris
Chris

Reputation: 1804

Kafka client code will manage resending messages in the event of failure. You should not need to write tests for that behaviour - by extension you could go down a real rabbit hole, testing core Java class behaviour etc.

However you can shut down and restart the broker with

embeddedKafkaBroker.getKafkaServers().forEach(KafkaServer::shutdown);
embeddedKafkaBroker.getKafkaServers().forEach(KafkaServer::awaitShutdown);
// Send messages
embeddedKafkaBroker.getKafkaServers().forEach(KafkaServer::startup);

Upvotes: 4

Related Questions