@Override
public MessageAndOffset read() throws StageException {
try {
//has next blocks indefinitely if consumer.timeout.ms is set to -1
//But if consumer.timeout.ms is set to a value, like 6000, a ConsumerTimeoutException is thrown
//if no message is written to kafka topic in that time.
if(consumerIterator.hasNext()) {
MessageAndMetadata<byte[], byte[]> messageAndMetadata = consumerIterator.next();
byte[] message = messageAndMetadata.message();
long offset = messageAndMetadata.offset();
int partition = messageAndMetadata.partition();
return new MessageAndOffset(message, offset, partition);
}
return null;
} catch (ConsumerTimeoutException e) {
/*For high level consumer the fetching logic is handled by a background
fetcher thread and is hidden from user, for either case of
1) broker down or
2) no message is available
the fetcher thread will keep retrying while the user thread will wait on the fetcher thread to put some
data into the buffer until timeout. So in a sentence the high-level consumer design is to
not let users worry about connect / reconnect issues.*/
return null;
}
}
KafkaConsumer08.java 文件源码
java
阅读 21
收藏 0
点赞 0
评论 0
项目:datacollector
作者:
评论列表
文章目录