SpringKafka:按顺序阅读两个不同的主题

ac1kyiln  于 2021-06-05  发布在  Kafka
关注(0)|答案(1)|浏览(657)

SpringKafka有没有可能以保证的顺序阅读不同消费者的两个不同主题?例如,主题a存储与确定如何处理存储在主题b中的数据相关的信息。主题a被读入内存并被引用,但在从主题b读入数据之前需要完全填充。下面是我当前设置的示例。。。

@Service
public class TopicA {
  @KafkaListener(topics = "topicA")
  public void consume(ConsumerRecord<String, byte[]> record) {
    // ... some code here to populate an in-memory data structure
  }
}
@Service
public class TopicB {
  @KafkaListener(topics = "topicB")
  public void consume(ConsumerRecord<String, byte[]> record) {
    // ... some code here that depends on topic A having populated the in-memory data structure
  }
}

到目前为止,我一直倾向于创建一个spring启动过程(使用@postconstruct),通过先读取主题a来初始化数据结构,但一直未能使其正常工作。有人有什么建议吗?提前谢谢!

4uqofj5v

4uqofj5v1#

@KafkaListener(id = "bConsumer" topics = "topicB", autoStartup = "false")

然后自动连接 KafkaListenerEndpointRegistry 豆和 registry.getListenerContainer("bConsumer").start(); 当你准备好的时候。

相关问题