Class AsyncKafkaConsumer<K,V>
java.lang.Object
org.apache.kafka.clients.consumer.internals.AsyncKafkaConsumer<K,V>
- All Implemented Interfaces:
Closeable,AutoCloseable,Consumer<K,,V> ConsumerDelegate<K,V>
This
Consumer implementation uses an event handler to process
application events so that the network I/O can be processed in a dedicated
network thread. Visit
this document
for implementation detail.
Note: this Consumer implementation is part of the revised consumer group protocol from KIP-848.
This class should not be invoked directly; users should instead create a KafkaConsumer as before.
This consumer implements the new consumer group protocol and is intended to be the default in coming releases.-
Method Summary
Modifier and TypeMethodDescriptionvoidassign(Collection<org.apache.kafka.common.TopicPartition> partitions) Set<org.apache.kafka.common.TopicPartition> beginningOffsets(Collection<org.apache.kafka.common.TopicPartition> partitions) beginningOffsets(Collection<org.apache.kafka.common.TopicPartition> partitions, Duration timeout) clientId()org.apache.kafka.common.UuidclientInstanceId(Duration timeout) voidclose()voidvoidThis method sends a commit event to the EventHandler and return.voidcommitAsync(Map<org.apache.kafka.common.TopicPartition, OffsetAndMetadata> offsets, OffsetCommitCallback callback) voidcommitAsync(OffsetCommitCallback callback) voidCommit offsets returned on the lastpoll()for all the subscribed list of topics and partitions.voidcommitSync(Duration timeout) This method sends a commit event to the EventHandler and waits for the event to finish.voidcommitSync(Map<org.apache.kafka.common.TopicPartition, OffsetAndMetadata> offsets) voidcommitSync(Map<org.apache.kafka.common.TopicPartition, OffsetAndMetadata> offsets, Duration timeout) Map<org.apache.kafka.common.TopicPartition, OffsetAndMetadata> Map<org.apache.kafka.common.TopicPartition, OffsetAndMetadata> committed(org.apache.kafka.common.TopicPartition partition) Deprecated.Deprecated.currentLag(org.apache.kafka.common.TopicPartition topicPartition) endOffsets(Collection<org.apache.kafka.common.TopicPartition> partitions) endOffsets(Collection<org.apache.kafka.common.TopicPartition> partitions, Duration timeout) voidvoidenforceRebalance(String reason) listTopics(Duration timeout) Map<org.apache.kafka.common.MetricName, ? extends org.apache.kafka.common.Metric> metrics()org.apache.kafka.common.metrics.MetricsMap<org.apache.kafka.common.TopicPartition, OffsetAndTimestamp> offsetsForTimes(Map<org.apache.kafka.common.TopicPartition, Long> timestampsToSearch) Map<org.apache.kafka.common.TopicPartition, OffsetAndTimestamp> offsetsForTimes(Map<org.apache.kafka.common.TopicPartition, Long> timestampsToSearch, Duration timeout) List<org.apache.kafka.common.PartitionInfo> partitionsFor(String topic) List<org.apache.kafka.common.PartitionInfo> partitionsFor(String topic, Duration timeout) voidpause(Collection<org.apache.kafka.common.TopicPartition> partitions) Set<org.apache.kafka.common.TopicPartition> paused()poll(long timeoutMs) Deprecated.poll implementation usingApplicationEventHandler.longposition(org.apache.kafka.common.TopicPartition partition) longvoidresume(Collection<org.apache.kafka.common.TopicPartition> partitions) voidseek(org.apache.kafka.common.TopicPartition partition, long offset) voidseek(org.apache.kafka.common.TopicPartition partition, OffsetAndMetadata offsetAndMetadata) voidseekToBeginning(Collection<org.apache.kafka.common.TopicPartition> partitions) voidseekToEnd(Collection<org.apache.kafka.common.TopicPartition> partitions) voidsubscribe(Collection<String> topics) voidsubscribe(Collection<String> topics, ConsumerRebalanceListener listener) voidvoidsubscribe(Pattern pattern, ConsumerRebalanceListener listener) Get the current subscription.voidbooleanupdateAssignmentMetadataIfNeeded(org.apache.kafka.common.utils.Timer timer) voidwakeup()
-
Method Details
-
poll
poll implementation usingApplicationEventHandler. 1. Poll for background events. If there's a fetch response event, process the record and return it. If it is another type of event, process it. 2. Send fetches if needed. If the timeout expires, return an empty ConsumerRecord.- Specified by:
pollin interfaceConsumer<K,V> - Parameters:
timeout- timeout of the poll loop- Returns:
- ConsumerRecord. It can be empty if time timeout expires.
- Throws:
org.apache.kafka.common.errors.WakeupException- ifwakeup()is called before or while this function is calledorg.apache.kafka.common.errors.InterruptException- if the calling thread is interrupted before or while this function is calledorg.apache.kafka.common.errors.RecordTooLargeException- if the fetched record is larger than the maximum allowable sizeorg.apache.kafka.common.KafkaException- for any other unrecoverable errorsIllegalStateException- if the consumer is not subscribed to any topics or manually assigned any partitions to consume from or an unexpected error occurredOffsetOutOfRangeException- if the fetch position of the consumer is out of range and no offset reset policy is configured.org.apache.kafka.common.errors.TopicAuthorizationException- if the consumer is not authorized to read from a partitionorg.apache.kafka.common.errors.SerializationException- if the fetched records cannot be deserializedorg.apache.kafka.common.errors.UnsupportedAssignorException- if the `group.remote.assignor` configuration is set to an assignor that is not available on the broker.- See Also:
-
commitSync
public void commitSync()Commit offsets returned on the lastpoll()for all the subscribed list of topics and partitions.- Specified by:
commitSyncin interfaceConsumer<K,V> - See Also:
-
commitAsync
public void commitAsync()This method sends a commit event to the EventHandler and return.- Specified by:
commitAsyncin interfaceConsumer<K,V> - See Also:
-
commitAsync
- Specified by:
commitAsyncin interfaceConsumer<K,V> - See Also:
-
commitAsync
public void commitAsync(Map<org.apache.kafka.common.TopicPartition, OffsetAndMetadata> offsets, OffsetCommitCallback callback) - Specified by:
commitAsyncin interfaceConsumer<K,V> - See Also:
-
seek
public void seek(org.apache.kafka.common.TopicPartition partition, long offset) -
seek
public void seek(org.apache.kafka.common.TopicPartition partition, OffsetAndMetadata offsetAndMetadata) -
seekToBeginning
- Specified by:
seekToBeginningin interfaceConsumer<K,V> - See Also:
-
seekToEnd
-
position
public long position(org.apache.kafka.common.TopicPartition partition) -
position
-
committed
Deprecated. -
committed
@Deprecated public OffsetAndMetadata committed(org.apache.kafka.common.TopicPartition partition, Duration timeout) Deprecated. -
committed
public Map<org.apache.kafka.common.TopicPartition,OffsetAndMetadata> committed(Set<org.apache.kafka.common.TopicPartition> partitions) -
committed
public Map<org.apache.kafka.common.TopicPartition,OffsetAndMetadata> committed(Set<org.apache.kafka.common.TopicPartition> partitions, Duration timeout) -
metrics
-
partitionsFor
- Specified by:
partitionsForin interfaceConsumer<K,V> - See Also:
-
partitionsFor
- Specified by:
partitionsForin interfaceConsumer<K,V> - See Also:
-
listTopics
- Specified by:
listTopicsin interfaceConsumer<K,V> - See Also:
-
listTopics
- Specified by:
listTopicsin interfaceConsumer<K,V> - See Also:
-
paused
-
pause
-
resume
-
offsetsForTimes
public Map<org.apache.kafka.common.TopicPartition,OffsetAndTimestamp> offsetsForTimes(Map<org.apache.kafka.common.TopicPartition, Long> timestampsToSearch) - Specified by:
offsetsForTimesin interfaceConsumer<K,V> - See Also:
-
offsetsForTimes
public Map<org.apache.kafka.common.TopicPartition,OffsetAndTimestamp> offsetsForTimes(Map<org.apache.kafka.common.TopicPartition, Long> timestampsToSearch, Duration timeout) - Specified by:
offsetsForTimesin interfaceConsumer<K,V> - See Also:
-
beginningOffsets
public Map<org.apache.kafka.common.TopicPartition,Long> beginningOffsets(Collection<org.apache.kafka.common.TopicPartition> partitions) - Specified by:
beginningOffsetsin interfaceConsumer<K,V> - See Also:
-
beginningOffsets
public Map<org.apache.kafka.common.TopicPartition,Long> beginningOffsets(Collection<org.apache.kafka.common.TopicPartition> partitions, Duration timeout) - Specified by:
beginningOffsetsin interfaceConsumer<K,V> - See Also:
-
endOffsets
public Map<org.apache.kafka.common.TopicPartition,Long> endOffsets(Collection<org.apache.kafka.common.TopicPartition> partitions) - Specified by:
endOffsetsin interfaceConsumer<K,V> - See Also:
-
endOffsets
public Map<org.apache.kafka.common.TopicPartition,Long> endOffsets(Collection<org.apache.kafka.common.TopicPartition> partitions, Duration timeout) - Specified by:
endOffsetsin interfaceConsumer<K,V> - See Also:
-
currentLag
- Specified by:
currentLagin interfaceConsumer<K,V> - See Also:
-
groupMetadata
- Specified by:
groupMetadatain interfaceConsumer<K,V> - See Also:
-
enforceRebalance
public void enforceRebalance()- Specified by:
enforceRebalancein interfaceConsumer<K,V> - See Also:
-
enforceRebalance
- Specified by:
enforceRebalancein interfaceConsumer<K,V> - See Also:
-
close
public void close() -
close
-
wakeup
public void wakeup() -
commitSync
This method sends a commit event to the EventHandler and waits for the event to finish.- Specified by:
commitSyncin interfaceConsumer<K,V> - Parameters:
timeout- max wait time for the blocking operation.- See Also:
-
commitSync
- Specified by:
commitSyncin interfaceConsumer<K,V> - See Also:
-
commitSync
public void commitSync(Map<org.apache.kafka.common.TopicPartition, OffsetAndMetadata> offsets, Duration timeout) - Specified by:
commitSyncin interfaceConsumer<K,V> - See Also:
-
clientInstanceId
Description copied from interface:Consumer- Specified by:
clientInstanceIdin interfaceConsumer<K,V>
-
assignment
- Specified by:
assignmentin interfaceConsumer<K,V> - See Also:
-
subscription
Get the current subscription. or an empty set if no such call has been made.- Specified by:
subscriptionin interfaceConsumer<K,V> - Returns:
- The set of topics currently subscribed to
- See Also:
-
assign
-
unsubscribe
public void unsubscribe()- Specified by:
unsubscribein interfaceConsumer<K,V> - See Also:
-
poll
Deprecated. -
updateAssignmentMetadataIfNeeded
public boolean updateAssignmentMetadataIfNeeded(org.apache.kafka.common.utils.Timer timer) - Specified by:
updateAssignmentMetadataIfNeededin interfaceConsumerDelegate<K,V>
-
subscribe
-
subscribe
-
subscribe
-
subscribe
-
clientId
- Specified by:
clientIdin interfaceConsumerDelegate<K,V>
-
metricsRegistry
public org.apache.kafka.common.metrics.Metrics metricsRegistry()- Specified by:
metricsRegistryin interfaceConsumerDelegate<K,V>
-
kafkaConsumerMetrics
- Specified by:
kafkaConsumerMetricsin interfaceConsumerDelegate<K,V>
-