有谁知道一个听众是否可以听如下多个主题?我知道只有“ topic1”有效,如果我想添加其他主题怎么办?您能在下面显示两个示例吗?谢谢您的帮助!
@KafkaListener(topics = "topic1,topic2") public void listen(ConsumerRecord<?, ?> record, Acknowledgment ack) { System.out.println(record); }
要么
ContainerProperties containerProps = new ContainerProperties(new TopicPartitionInitialOffset("topic1, topic2", 0));
是的,只需遵循@KafkaListenerJavaDocs:
@KafkaListener
/** * 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"})