Class ConsumerBuilder<K,V>
- Type Parameters:
K- The record key typeV- The record value type
- All Implemented Interfaces:
Closeable,AutoCloseable,org.apache.kafka.clients.consumer.ConsumerRebalanceListener
ConsumerTask) that consume records.
The wrapped Kafka consumer is created lazily when a ConsumerTask is created and started.
Until the consumer is created builder methods prefixed with with- act to provide additional configuration.
The created consumer is closed automatically when the task is terminated.
The consumer uses an underlying ExecutorService for polling records.
As Kafka consumer is not thread-safe, other operations accessing the underlying consumer are also run inside the
ExecutorService
-
Constructor Summary
ConstructorsConstructorDescriptionConsumerBuilder(Map<String, Object> props, Duration kafkaApiTimeout, String keyDeserializerType, String valueDeserializerType) Creates a newConsumerBuilder.ConsumerBuilder(Map<String, Object> props, Duration kafkaApiTimeout, org.apache.kafka.common.serialization.Deserializer<K> keyDeser, org.apache.kafka.common.serialization.Deserializer<V> valueDeser) Creates a newConsumerBuilder. -
Method Summary
Modifier and TypeMethodDescriptionSet<org.apache.kafka.common.TopicPartition>clientId()voidclose()Close the underlyingKafkaConsumer.voidCommit the current offset and close the consumervoidcommitAndClose(Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets) Commit the given offset and close the consumerMap<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> org.apache.kafka.clients.consumer.OffsetAndMetadatacommitted(org.apache.kafka.common.TopicPartition partition) Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> committed(org.apache.kafka.common.TopicPartition... partitions) Set<org.apache.kafka.common.TopicPartition>fromOffsets(Map<org.apache.kafka.common.TopicPartition, Long> offsets) CreateConsumerTaskfor consuming records starting from the given offsets.fromOffsets(Map<org.apache.kafka.common.TopicPartition, Long> offsets, long numberOfRecords) CreateConsumerTaskfor consuming records starting from the given offsets.fromOffsets(Map<org.apache.kafka.common.TopicPartition, Long> offsets, Duration during) CreateConsumerTaskfor consuming records starting from the given offsets.fromOffsets(Map<org.apache.kafka.common.TopicPartition, Long> offsets, Function<io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>, io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>> plugFunction) CreateConsumerTaskfor consuming records starting from the given offsets.fromPrevious(KafkaTask<?, ?> task) fromPrevious(KafkaTask<?, ?> task, long numberOfRecords) fromPrevious(KafkaTask<?, ?> task, long numberOfRecords, Duration during) fromPrevious(KafkaTask<?, ?> task, Duration during) fromPrevious(KafkaTask<?, ?> task, Function<io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>, io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>> plugFunction) fromTopics(String... topics) CreateConsumerTaskfor consuming records from the given topics.fromTopics(String topic, long numberOfRecords) CreateConsumerTaskfor consuming records from the given topic.fromTopics(String topic, long numberOfRecords, Duration during) CreateConsumerTaskfor consuming records from the given topic.fromTopics(String topic, Duration during) CreateConsumerTaskfor consuming records from the given topic.fromTopics(String topic, Function<io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>, io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>> plugFunction) CreateConsumerTaskfor consuming records from the given topic.fromTopics(Set<String> topics) CreateConsumerTaskfor consuming records from the given topics.fromTopics(Set<String> topics, long numberOfRecords) CreateConsumerTaskfor consuming records from the given topics.fromTopics(Set<String> topics, Duration during) CreateConsumerTaskfor consuming records from the given topics.fromTopics(Set<String> topics, Function<io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>, io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>> plugFunction) CreateConsumerTaskfor consuming records from the given topics.groupId()org.apache.kafka.clients.consumer.ConsumerGroupMetadatavoidonPartitionsAssigned(Collection<org.apache.kafka.common.TopicPartition> partitions) voidonPartitionsRevoked(Collection<org.apache.kafka.common.TopicPartition> partitions) voidpause()Pause the consumerMap<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> position()longposition(org.apache.kafka.common.TopicPartition partition) voidReset to the last committed positionvoidresume()Resume the consumervoidunwrap()io.smallrye.mutiny.Uni<Set<org.apache.kafka.common.TopicPartition>>Enable auto commitwithClientId(String clientId) Add configuration property forclient.idwithCommitAsync(Function<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>, Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata>> commitFunction, org.apache.kafka.clients.consumer.OffsetCommitCallback callback) If set the given function is used to determine when and which offsets to commit asyncwithCommitAsyncWhen(Predicate<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>> commitPredicate) If set the given function is used to determine when to commit asyncwithCommitSync(Function<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>, Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata>> commitFunction) If set the given function is used to determine when and which offsets to commit syncwithCommitSyncWhen(Predicate<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>> commitPredicate) If set the given function is used to determine when to commit syncwithGroupId(String groupId) Add configuration property forgroup.idwithIsolationLevel(org.apache.kafka.common.IsolationLevel isolationLevel) Set isolation levelwithOffsetReset(org.apache.kafka.clients.consumer.OffsetResetStrategy offsetResetStrategy) Add configuration property forauto.offset.resetwithOnPartitionsAssigned(Consumer<Collection<org.apache.kafka.common.TopicPartition>> onPartitionsAssigned) withOnPartitionsRevoked(Consumer<Collection<org.apache.kafka.common.TopicPartition>> onPartitionsRevoked) withOnTermination(BiConsumer<org.apache.kafka.clients.consumer.KafkaConsumer<K, V>, Throwable> onTermination) withPollTimeout(Duration timeout) Set poll timeoutAdd property to the configuration to be used when the consumer is created.Add properties to the configuration to be used when the consumer is created.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.kafka.clients.consumer.ConsumerRebalanceListener
onPartitionsLost
-
Constructor Details
-
ConsumerBuilder
public ConsumerBuilder(Map<String, Object> props, Duration kafkaApiTimeout, org.apache.kafka.common.serialization.Deserializer<K> keyDeser, org.apache.kafka.common.serialization.Deserializer<V> valueDeser) Creates a newConsumerBuilder.Note that on producer creation
Deserializer.configure(java.util.Map<java.lang.String, ?>, boolean)methods will NOT be called.- Parameters:
props- the initial properties for consumer creationkafkaApiTimeout- the timeout for api calls to KafkakeyDeser- the deserializer for record keysvalueDeser- the deserializer for record values
-
ConsumerBuilder
public ConsumerBuilder(Map<String, Object> props, Duration kafkaApiTimeout, String keyDeserializerType, String valueDeserializerType) Creates a newConsumerBuilder.On consumer creation, key and value deserializers will be created and
Deserializer.configure(java.util.Map<java.lang.String, ?>, boolean)methods will be called.- Parameters:
props- the initial properties for consumer creationkafkaApiTimeout- the timeout for api calls to KafkakeyDeserializerType- the deserializer class name for record keysvalueDeserializerType- the deserializer class name for record values
-
-
Method Details
-
unwrap
- Returns:
- the underlying
KafkaConsumer, may benullif producer is not yet created.
-
onPartitionsRevoked
- Specified by:
onPartitionsRevokedin interfaceorg.apache.kafka.clients.consumer.ConsumerRebalanceListener
-
onPartitionsAssigned
- Specified by:
onPartitionsAssignedin interfaceorg.apache.kafka.clients.consumer.ConsumerRebalanceListener
-
close
public void close()Close the underlyingKafkaConsumer.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
terminate
-
clientId
- Returns:
- the
client.idproperty of this consumer
-
groupId
- Returns:
- the
group.idproperty of this consumer
-
withProp
Add property to the configuration to be used when the consumer is created.- Parameters:
key- the key for propertyvalue- the value for property- Returns:
- this
ConsumerBuilder
-
withProps
Add properties to the configuration to be used when the consumer is created.- Parameters:
properties- the properties- Returns:
- this
ConsumerBuilder
-
withClientId
Add configuration property forclient.id- Parameters:
clientId- the client id- Returns:
- this
ConsumerBuilder
-
withGroupId
Add configuration property forgroup.id- Parameters:
groupId- the group id- Returns:
- this
ConsumerBuilder
-
withOffsetReset
public ConsumerBuilder<K,V> withOffsetReset(org.apache.kafka.clients.consumer.OffsetResetStrategy offsetResetStrategy) Add configuration property forauto.offset.reset- Parameters:
offsetResetStrategy- the offset reset strategy- Returns:
- this
ConsumerBuilder
-
withIsolationLevel
public ConsumerBuilder<K,V> withIsolationLevel(org.apache.kafka.common.IsolationLevel isolationLevel) Set isolation level- Parameters:
isolationLevel- the isolation level- Returns:
- this
ConsumerBuilder
-
withPollTimeout
Set poll timeout- Parameters:
timeout- the poll timeout- Returns:
- this
ConsumerBuilder
-
withAutoCommit
Enable auto commit- Returns:
- this
ConsumerBuilder
-
withCommitAsyncWhen
public ConsumerBuilder<K,V> withCommitAsyncWhen(Predicate<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>> commitPredicate) If set the given function is used to determine when to commit async- Parameters:
commitPredicate- the commit async predicate- Returns:
- this
ConsumerBuilder
-
withCommitAsync
public ConsumerBuilder<K,V> withCommitAsync(Function<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>, Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata>> commitFunction, org.apache.kafka.clients.consumer.OffsetCommitCallback callback) If set the given function is used to determine when and which offsets to commit async- Parameters:
commitFunction- the function to call for commit asynccallback- the callback to set for commit async- Returns:
- this
ConsumerBuilder
-
withCommitSyncWhen
public ConsumerBuilder<K,V> withCommitSyncWhen(Predicate<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>> commitPredicate) If set the given function is used to determine when to commit sync- Parameters:
commitPredicate- the commit sync predicate- Returns:
- this
ConsumerBuilder
-
withCommitSync
public ConsumerBuilder<K,V> withCommitSync(Function<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>, Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata>> commitFunction) If set the given function is used to determine when and which offsets to commit sync- Parameters:
commitFunction- the function to call- Returns:
- this
ConsumerBuilder
-
withOnPartitionsAssigned
public ConsumerBuilder<K,V> withOnPartitionsAssigned(Consumer<Collection<org.apache.kafka.common.TopicPartition>> onPartitionsAssigned) - Parameters:
onPartitionsAssigned- the callback to be called on partitions assigned- Returns:
- this
ConsumerBuilder
-
withOnPartitionsRevoked
public ConsumerBuilder<K,V> withOnPartitionsRevoked(Consumer<Collection<org.apache.kafka.common.TopicPartition>> onPartitionsRevoked) - Parameters:
onPartitionsRevoked- the callback to be called on partitions revoked- Returns:
- this
ConsumerBuilder
-
withOnTermination
public ConsumerBuilder<K,V> withOnTermination(BiConsumer<org.apache.kafka.clients.consumer.KafkaConsumer<K, V>, Throwable> onTermination) - Parameters:
onTermination- the callback to be called on termination- Returns:
- this
ConsumerBuilder
-
groupMetadata
public org.apache.kafka.clients.consumer.ConsumerGroupMetadata groupMetadata()- Returns:
- the consumer group metadata
-
currentAssignment
- Returns:
- the current partition assignments of this consumer
-
assignment
- Returns:
- the partition assignment of this consumer
-
waitForAssignment
- Returns:
- the
Unifor waiting on the assignments of partitions
-
position
public long position(org.apache.kafka.common.TopicPartition partition) - Parameters:
partition- the topic partition- Returns:
- the position of this consumer for the given topic partition
-
position
public Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> position()- Returns:
- the position of this consumer for all assigned topic partitions
-
resetToLastCommittedPositions
public void resetToLastCommittedPositions()Reset to the last committed position -
committed
public org.apache.kafka.clients.consumer.OffsetAndMetadata committed(org.apache.kafka.common.TopicPartition partition) - Parameters:
partition- the topic partition- Returns:
- the map of committed offsets for the topic partition
-
committed
public Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> committed(org.apache.kafka.common.TopicPartition... partitions) - Parameters:
partitions- the topic partitions- Returns:
- the map of committed offsets for topic partitions
-
committed
public Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> committed()- Returns:
- the map of committed offsets for topic partitions assigned to this consumer
-
pause
public void pause()Pause the consumer -
resume
public void resume()Resume the consumer -
fromOffsets
public ConsumerTask<K,V> fromOffsets(Map<org.apache.kafka.common.TopicPartition, Long> offsets, Function<io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>, io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>> plugFunction) CreateConsumerTaskfor consuming records starting from the given offsets.The plug function is used to modify the
Multigenerating the records, ex. limiting the number of records produced.The task will run until a failure occurs or is explicitly stopped (subscription to the
Multicancelled).- Parameters:
offsets- the map of offsets to start consuming recordsplugFunction- the function to apply to the resulting multi- Returns:
- the
ConsumerTask
-
fromOffsets
CreateConsumerTaskfor consuming records starting from the given offsets.The task will run until a failure occurs or is explicitly stopped (subscription to the
Multicancelled).- Parameters:
offsets- the map of offsets to start consuming records- Returns:
- the
ConsumerTask
-
fromOffsets
public ConsumerTask<K,V> fromOffsets(Map<org.apache.kafka.common.TopicPartition, Long> offsets, Duration during) CreateConsumerTaskfor consuming records starting from the given offsets.The resulting
ConsumerTaskwill be already started. The task will run until theMultiis terminated (with completion or failure) or is explicitly stopped (subscription to theMulticancelled).- Parameters:
offsets- the map of offsets to start consuming recordsduring- the duration of the producing task to run- Returns:
- the
ConsumerTask
-
fromOffsets
public ConsumerTask<K,V> fromOffsets(Map<org.apache.kafka.common.TopicPartition, Long> offsets, long numberOfRecords) CreateConsumerTaskfor consuming records starting from the given offsets.The resulting
ConsumerTaskwill be already started. The task will run for generating the given number of records.- Parameters:
offsets- the map of offsets to start consuming recordsnumberOfRecords- the number of records to produce- Returns:
- the
ConsumerTask
-
fromTopics
public ConsumerTask<K,V> fromTopics(Set<String> topics, Function<io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>, io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>> plugFunction) CreateConsumerTaskfor consuming records from the given topics.The plug function is used to modify the
Multigenerating the records, ex. limiting the number of records produced.The task will run until a failure occurs or is explicitly stopped (subscription to the
Multicancelled).- Parameters:
topics- the set of topics to subscribeplugFunction- the function to apply to the resulting multi- Returns:
- the
ConsumerTask
-
fromTopics
public ConsumerTask<K,V> fromTopics(String topic, Function<io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>, io.smallrye.mutiny.Multi<org.apache.kafka.clients.consumer.ConsumerRecord<K, V>>> plugFunction) CreateConsumerTaskfor consuming records from the given topic.The plug function is used to modify the
Multigenerating the records, ex. limiting the number of records produced.The resulting
ConsumerTaskwill be already started. The task will run until theMultiis terminated (with completion or failure) or is explicitly stopped (subscription to theMulticancelled).- Parameters:
topic- the topic to subscribeplugFunction- the function to apply to the resulting multi- Returns:
- the
ConsumerTask
-
fromTopics
CreateConsumerTaskfor consuming records from the given topics.The resulting
ConsumerTaskwill be already started. The task will run until a failure occurs or is explicitly stopped (subscription to theMulticancelled).- Parameters:
topics- the set of topics to subscribe- Returns:
- the
ConsumerTask
-
fromTopics
CreateConsumerTaskfor consuming records from the given topics.The resulting
ConsumerTaskwill be already started. The task will run until a failure occurs or is explicitly stopped (subscription to theMulticancelled).- Parameters:
topics- the set of topics to subscribe- Returns:
- the
ConsumerTask
-
fromTopics
CreateConsumerTaskfor consuming records from the given topics.The resulting
ConsumerTaskwill be already started. The task will run until the given number of records consumed.- Parameters:
topics- the set of topics to subscribenumberOfRecords- the number of records to produce- Returns:
- the
ConsumerTask
-
fromTopics
CreateConsumerTaskfor consuming records from the given topics.The resulting
ConsumerTaskwill be already started. The task will run during the given duration.- Parameters:
topics- the set of topics to subscribeduring- the duration of the producing task to run- Returns:
- the
ConsumerTask
-
fromTopics
CreateConsumerTaskfor consuming records from the given topic.The resulting
ConsumerTaskwill be already started. The task will run until the given number of records consumed.- Parameters:
topic- the topic to subscribenumberOfRecords- the number of records to produce- Returns:
- the
ConsumerTask
-
fromTopics
CreateConsumerTaskfor consuming records from the given topic.The resulting
ConsumerTaskwill be already started. The task will run during the given duration.- Parameters:
topic- the topic to subscribeduring- the duration of the producing task to run- Returns:
- the
ConsumerTask
-
fromTopics
CreateConsumerTaskfor consuming records from the given topic.The resulting
ConsumerTaskwill be already started. The task will run until the given number of records consumed or during the given duration.- Parameters:
topic- the topic to subscribenumberOfRecords- the number of records to produceduring- the duration of the producing task to run- Returns:
- the
ConsumerTask
-
fromPrevious
-
fromPrevious
-
fromPrevious
-
fromPrevious
-
fromPrevious
-
commitAndClose
public void commitAndClose(Map<org.apache.kafka.common.TopicPartition, org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets) Commit the given offset and close the consumer- Parameters:
offsets- the map of offsets to commit
-
commitAndClose
public void commitAndClose()Commit the current offset and close the consumer
-