简体   繁体   English

Spring Kafka:按顺序从两个不同的主题中读取

[英]Spring Kafka: Read from two different topics in order

Is it possible with Spring Kafka to read from two different topics in different consumers in a guaranteed order? Spring Kafka 是否可以按有保证的顺序从不同消费者的两个不同主题中读取? For example, topic A stores information that is pertinent to determine how to handle the data that is stored in topic B. Topic A is read into memory and referenced, but needs to be completely populated before reading data in from topic B. Below is an example of what my current setup looks like...例如,主题 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
  }
}

So far I've been leaning towards creating a Spring startup process (using @PostConstruct) that initializes the data structure by reading from topic A first, but have not been able to get that working.到目前为止,我一直倾向于创建一个 Spring 启动过程(使用 @PostConstruct),该过程通过首先从主题 A 中读取来初始化数据结构,但未能使其正常工作。 Does anyone have any suggestions?有没有人有什么建议? Thanks in advance!提前致谢!

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

Then autowire the KafkaListenerEndpointRegistry bean and registry.getListenerContainer("bConsumer").start();然后自动装配KafkaListenerEndpointRegistry bean 和registry.getListenerContainer("bConsumer").start(); when you are ready.当你准备好的时候。

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

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