我们获取消息都是通过poll方法,现在我们从整体上看一下消费的流程:
public ConsumerRecords<K, V> poll(long timeout) {
//防止并发操作
acquire();
try {
if (timeout < 0)
throw new IllegalArgumentException("Timeout must not be negative");
//获取拉取消息的开始时间
long start = time.milliseconds();
long remaining = timeout;
do {
//在规定时间内拉取一次消息
Map<TopicPartition, List<ConsumerRecord<K, V>>> records = pollOnce(remaining);
if (!records.isEmpty()) {
//如果拉取到了消息,发送一次消息拉取的请求,不会阻塞不会被中断
fetcher.sendFetches();
client.pollNoWakeup();
//经过拦截器处理后返回
if (this.interceptors == null)
return new ConsumerRecords<>(records);
else
return this.interceptors.onConsume(ne