本文介绍了Kafka-用poll()收听Java主题的最佳选择是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java创建消费者客户端.我意识到poll()函数已被贬值.收听Kafka主题有哪些选择?

I'm trying to create a consumer client in Java. I realized the poll() function is depreciated. What are the alternatives to listen to a topic of Kafka?

我的代码:

KafkaConsumer< String, UserSegmentPayload > kc = new KafkaConsumer<>(props2);
kc.subscribe(Collections.singletonList(topicName));
while (true) {
    ConsumerRecords<String, UserSegmentPayload> records = kc.poll(100);
    for (ConsumerRecord<String, UserSegmentPayload> record : records) {
        System.out.printf("offset = %d, key = %s, value = %s\n",
        record.offset(), record.key(), record.value());
    }
}

推荐答案

不推荐使用poll()poll(long)的原因是它们可能无限期地阻塞(即使在第二种情况下,也指定了超时).此行为的根本原因是,这些方法中的初始元数据更新可能永远被阻止(请参阅此处).相反,您应该使用KafkaConsumerpoll(Duration)方法.因此,在您的代码中,您要做的就是将kc.poll(100)替换为kc.poll(Duration.ofMillis(100)).

The reason poll() and poll(long) are deprecated is that they may block indefinitely (even in the second case, where a timeout is specified). The root cause for this behaviour is that an initial metadata update in those methods may block forever (see here). Instead, you should use the poll(Duration)-method of the KafkaConsumer. So, in your code, all you have to do is to replace kc.poll(100) with kc.poll(Duration.ofMillis(100)).

这篇关于Kafka-用poll()收听Java主题的最佳选择是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 12:39