Interface KafkaConsumer<K,​V>

  • Type Parameters:
    K - the type of key
    V - the type of value
    All Known Implementing Classes:
    ReactiveKafkaConsumer

    public interface KafkaConsumer<K,​V>
    Kafka Consumer API.

    Unlike Consumer, this API is asynchronous and make sure that the actions are executed on the Kafka polling thread.

    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      io.smallrye.mutiny.Uni<Void> commit​(Map<org.apache.kafka.common.TopicPartition,​org.apache.kafka.clients.consumer.OffsetAndMetadata> map)
      Commits the offsets
      io.smallrye.mutiny.Uni<Map<org.apache.kafka.common.TopicPartition,​org.apache.kafka.clients.consumer.OffsetAndMetadata>> committed​(org.apache.kafka.common.TopicPartition... tps)
      Retrieved the last committed offset for each topic/partition
      io.smallrye.mutiny.Uni<Set<org.apache.kafka.common.TopicPartition>> getAssignments()
      Retrieves the current assignments of the consumer.
      io.smallrye.mutiny.Uni<Map<org.apache.kafka.common.TopicPartition,​Long>> getPositions()
      Retrieves the next positions for each assigned topic/partitions
      io.smallrye.mutiny.Uni<Set<org.apache.kafka.common.TopicPartition>> pause()
      Pauses the consumption of records.
      io.smallrye.mutiny.Uni<Set<org.apache.kafka.common.TopicPartition>> paused()
      Retrieves the set of paused topic/partition
      io.smallrye.mutiny.Uni<Void> resume()
      Resumes the consumption of record.
      io.smallrye.mutiny.Uni<Void> runOnPollingThread​(Consumer<org.apache.kafka.clients.consumer.Consumer<K,​V>> action)
      Runs an action on the polling thread.
      <R> io.smallrye.mutiny.Uni<R> runOnPollingThread​(Function<org.apache.kafka.clients.consumer.Consumer<K,​V>,​R> action)
      Runs an action on the polling thread.
      io.smallrye.mutiny.Uni<Void> seek​(org.apache.kafka.common.TopicPartition partition, long offset)
      Overrides the fetch offset that the consumer will use on the next poll of given topic and partition.
      io.smallrye.mutiny.Uni<Void> seek​(org.apache.kafka.common.TopicPartition partition, org.apache.kafka.clients.consumer.OffsetAndMetadata offsetAndMetadata)
      Overrides the fetch offset that the consumer will use on the next poll of given topic and partition.
      io.smallrye.mutiny.Uni<Void> seekToBeginning​(Collection<org.apache.kafka.common.TopicPartition> partitions)
      Seek to the first offset for each of the given partitions.
      io.smallrye.mutiny.Uni<Void> seekToEnd​(Collection<org.apache.kafka.common.TopicPartition> partitions)
      Seek to the last offset for each of the given partitions.
      org.apache.kafka.clients.consumer.Consumer<K,​V> unwrap()  
    • Method Detail

      • runOnPollingThread

        <R> io.smallrye.mutiny.Uni<R> runOnPollingThread​(Function<org.apache.kafka.clients.consumer.Consumer<K,​V>,​R> action)
        Runs an action on the polling thread.

        The action is a function taking as parameter the Consumer and that returns a result (potentially null). The produced Uni emits the returned result when the action completes. If the action throws an exception, the produced Uni emits the exception as failure.

        If the action does not return a result, use runOnPollingThread(java.util.function.Consumer).

        Type Parameters:
        R - the type of result, can be Void
        Parameters:
        action - the action to execute, must not be null
        Returns:
        the Uni emitting the result or the failure when the action completes.
      • runOnPollingThread

        io.smallrye.mutiny.Uni<Void> runOnPollingThread​(Consumer<org.apache.kafka.clients.consumer.Consumer<K,​V>> action)
        Runs an action on the polling thread.

        The action is a consumer receiving the Consumer. The produced Uni emits null when the action completes. If the action throws an exception, the produced Uni emits the exception as failure.

        Parameters:
        action - the action, must not be null
        Returns:
        the Uni emitting null or the failure when the action completes.
      • pause

        io.smallrye.mutiny.Uni<Set<org.apache.kafka.common.TopicPartition>> pause()
        Pauses the consumption of records. The polling will continue, but no records will be received.
        Returns:
        the Uni emitting when the action completes, the set of topic/partition paused by this call.
      • paused

        io.smallrye.mutiny.Uni<Set<org.apache.kafka.common.TopicPartition>> paused()
        Retrieves the set of paused topic/partition
        Returns:
        the Uni emitting the set of topic/partition paused.
      • committed

        io.smallrye.mutiny.Uni<Map<org.apache.kafka.common.TopicPartition,​org.apache.kafka.clients.consumer.OffsetAndMetadata>> committed​(org.apache.kafka.common.TopicPartition... tps)
        Retrieved the last committed offset for each topic/partition
        Parameters:
        tps - the set of topic/partition to query, must not be null, must not be empty.
        Returns:
        the Uni emitting the offset for the underlying consumer for each of the passed topic/partition.
      • resume

        io.smallrye.mutiny.Uni<Void> resume()
        Resumes the consumption of record. It resumes the consumption of all the paused topic/partition.
        Returns:
        the Uni indicating when the resume action completes.
      • unwrap

        org.apache.kafka.clients.consumer.Consumer<K,​V> unwrap()
        Returns:
        the underlying consumer. Be aware that to use it you needs to be on the polling thread.
      • commit

        io.smallrye.mutiny.Uni<Void> commit​(Map<org.apache.kafka.common.TopicPartition,​org.apache.kafka.clients.consumer.OffsetAndMetadata> map)
        Commits the offsets
        Parameters:
        map - the map of topic/partition -> offset to commit
        Returns:
        the Uni emitting null when the commit has been executed.
      • getPositions

        io.smallrye.mutiny.Uni<Map<org.apache.kafka.common.TopicPartition,​Long>> getPositions()
        Retrieves the next positions for each assigned topic/partitions
        Returns:
        the Uni emitting the map of topic/partition -> position.
      • getAssignments

        io.smallrye.mutiny.Uni<Set<org.apache.kafka.common.TopicPartition>> getAssignments()
        Retrieves the current assignments of the consumer.
        Returns:
        the Uni emitting the set of topic/partition currently assigned to the consumer
      • seek

        io.smallrye.mutiny.Uni<Void> seek​(org.apache.kafka.common.TopicPartition partition,
                                          long offset)
        Overrides the fetch offset that the consumer will use on the next poll of given topic and partition. Note that you may lose data if this API is arbitrarily used in the middle of consumption.
        Parameters:
        partition - the topic and partition for which to set the offset
        offset - the new offset
        Returns:
        a Uni that completes successfully when the offset is set; it completes with IllegalArgumentException if the provided offset is negative; it completes with IllegalStateException if the provided TopicPartition is not assigned to this consumer
      • seek

        io.smallrye.mutiny.Uni<Void> seek​(org.apache.kafka.common.TopicPartition partition,
                                          org.apache.kafka.clients.consumer.OffsetAndMetadata offsetAndMetadata)
        Overrides the fetch offset that the consumer will use on the next poll of given topic and partition. This method allows setting the leaderEpoch along with the desired offset. Note that you may lose data if this API is arbitrarily used in the middle of consumption.
        Parameters:
        partition - the topic and partition for which to set the offset
        offsetAndMetadata - the new offset, with additional metadata
        Returns:
        a Uni that completes successfully when the offset is set; it completes with IllegalArgumentException if the provided offset is negative; it completes with IllegalStateException if the provided TopicPartition is not assigned to this consumer
      • seekToBeginning

        io.smallrye.mutiny.Uni<Void> seekToBeginning​(Collection<org.apache.kafka.common.TopicPartition> partitions)
        Seek to the first offset for each of the given partitions. If no partitions are provided, seek to the first offset for all of the currently assigned partitions.
        Parameters:
        partitions - the partitions for which to set the offset
        Returns:
        a Uni that completes successfully when the offset is set; it completes with IllegalArgumentException if partitions is null; it completes with IllegalStateException if any of the provided TopicPartitions are not currently assigned to this consumer
      • seekToEnd

        io.smallrye.mutiny.Uni<Void> seekToEnd​(Collection<org.apache.kafka.common.TopicPartition> partitions)
        Seek to the last offset for each of the given partitions. If no partitions are provided, seek to the last offset for all of the currently assigned partitions.
        Parameters:
        partitions - the partitions for which to set the offset
        Returns:
        a Uni that completes successfully when the offset is set; it completes with IllegalArgumentException if partitions is null; it completes with IllegalStateException if any of the provided TopicPartitions are not currently assigned to this consumer