2017-01-25 103 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的方式工作! –