2017-01-25 100 views
2

任何人都知道单个听众是否可以听下面的多个主题?我知道只是“topic1”的作品,如果我想添加额外的主题怎么办?你可以在下面展示两个例子吗?谢谢您的帮助!单个Spring的KafkaConsumer侦听器是否可以侦听多个主题?

@KafkaListener(topics = "topic1,topic2") 
public void listen(ConsumerRecord<?, ?> record, Acknowledgment ack) { 
    System.out.println(record); 
} 

ContainerProperties containerProps = new ContainerProperties(new TopicPartitionInitialOffset("topic1, topic2", 0)); 

回答

2

是的,只要按照@KafkaListener的JavaDoc:

/** 
* The topics for this listener. 
* The entries can be 'topic name', 'property-placeholder keys' or 'expressions'. 
* Expression must be resolved to the topic name. 
* Mutually exclusive with {@link #topicPattern()} and {@link #topicPartitions()}. 
* @return the topic names or expressions (SpEL) to listen to. 
*/ 
String[] topics() default {}; 

/** 
* The topic pattern for this listener. 
* The entries can be 'topic name', 'property-placeholder keys' or 'expressions'. 
* Expression must be resolved to the topic pattern. 
* Mutually exclusive with {@link #topics()} and {@link #topicPartitions()}. 
* @return the topic pattern or expression (SpEL). 
*/ 
String topicPattern() default ""; 

/** 
* The topicPartitions for this listener. 
* Mutually exclusive with {@link #topicPattern()} and {@link #topics()}. 
* @return the topic names or expressions (SpEL) to listen to. 
*/ 
TopicPartition[] topicPartitions() default {}; 

所以,您的使用情况应该是这样的:

@KafkaListener(topics = {"topic1" , "topic2"}) 
+0

而对于其他情况下,每个人都需要一个'TopicPartitionInitialOffset'。 –

+0

谢谢你们,非常有帮助!也让Gary的方式工作! –