spring - Spring KafkaListener:如何知道它何时准备就绪
问题描述
我有一个简单的 Spring Boot 应用程序,它从 Kafka 读取并写入 Kafka。我写了一个SpringBootTest
使用 anEmbeddedKafka
来测试所有这些。
主要问题是:有时测试失败是因为测试过早发送Kafka消息。这样,在 Spring 应用程序(或者KafkaListener
准确地说)准备好之前,消息就已经写入 Kafka。由于侦听器从latest
偏移量读取(我不想为我的测试更改任何配置 - 除了 bootstrap.servers),它不会收到该测试中的所有消息。
有谁知道我如何在测试中知道它KafkaListener
已准备好接收消息?
我能想到的唯一方法是等到/health
可用,但我不知道我是否可以确定这意味着KafkaListener
要准备好。
任何帮助是极大的赞赏!
此致。
解决方案
如果你有一个KafkaMessageListenerContainer
实例,那么它很容易使用org.springframework.kafka.test.utils.ContainerTestUtils.waitForAssignment(Object container, int partitions)
。
https://docs.spring.io/spring-kafka/api/org/springframework/kafka/test/utils/ContainerTestUtils.html
例如,在您的测试设置中调用ContainerTestUtils.waitForAssignment(container, 1);
将阻塞,直到容器分配了 1 个分区。
推荐阅读
- mysql - Debezium 引擎没有捕捉到我的 MySQL 数据库的变化
- c++ - 像素着色器中的 SamplerState 问题
- firebase - 是否可以将嵌套的 Kotlin 类转换为包含 Firestore 中子集合的集合?
- excel - 尝试使用 VBA Excel 创建表块
- google-cloud-platform - 将 VPN 隧道从项目 A 移动到项目 B - GCP
- pagerduty - 如何在团队之间移动服务?
- c - 如果我不在函数范围内,建议不会弹出
- javascript - 使用 .map() 在对象 React 中迭代数组
- go - 在golang中解释make的幕后?
- flutter - 在列表中添加或删除项目时,列表构建器重复数据的问题