简体   繁体   English

如何在单元测试中停止和启动嵌入式 Kafka?

[英]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.我使用 Embedded Kafka 测试向 Kafka 发送消息,当发送失败时,我的代码会自动重新发送,所以我尝试停止 Embedded Kafka,然后在重新发送期间重新启动它。 But I don't know how to stop and start the Embedded Kafka.但我不知道如何停止和启动 Embedded Kafka。

Kafka client code will manage resending messages in the event of failure. Kafka 客户端代码将在发生故障时管理重新发送消息。 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.您不需要为该行为编写测试 - 通过扩展您可以 go 进入真正的兔子洞,测试核心 Java class 行为等。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM