Interface KafkaProducer<K,V>

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

public interface KafkaProducer<K,V>
Kafka Producer API.

Unlike KafkaProducer, this API is guaranteed to be asynchronous. Note that even though the org.apache.kafka.clients.producer.KafkaProducer is documented to be asynchronous, it actually may block in some cases; see KAFKA-3539 for more info.

The way asynchrony is guaranteed here is an implementation detail. Currently, the sending actions are executed on a special sending thread, but when KAFKA-3539 is fixed, the implementation may become just a simple wrapper providing a Uni API.

  • Method Summary

    Modifier and Type
    Method
    Description
    io.smallrye.mutiny.Uni<Void>
     
    io.smallrye.mutiny.Uni<Void>
     
    void
    Close the producer client
    io.smallrye.mutiny.Uni<Void>
     
     
    io.smallrye.mutiny.Uni<Void>
    Sends all buffered records immediately.
    io.smallrye.mutiny.Uni<Void>
     
    io.smallrye.mutiny.Uni<List<org.apache.kafka.common.PartitionInfo>>
    Returns a list of partition metadata for given topic.
    io.smallrye.mutiny.Uni<Void>
    runOnSendingThread(Consumer<org.apache.kafka.clients.producer.Producer<K,V>> action)
    Runs an action on the sending thread.
    <R> io.smallrye.mutiny.Uni<R>
    runOnSendingThread(Function<org.apache.kafka.clients.producer.Producer<K,V>,R> action)
    Runs an action on the sending thread.
    io.smallrye.mutiny.Uni<org.apache.kafka.clients.producer.RecordMetadata>
    send(org.apache.kafka.clients.producer.ProducerRecord<K,V> record)
    Send a record to a topic.
    io.smallrye.mutiny.Uni<Void>
    sendOffsetsToTransaction(Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets, org.apache.kafka.clients.consumer.ConsumerGroupMetadata groupMetadata)
     
    org.apache.kafka.clients.producer.Producer<K,V>
     
  • Method Details

    • configuration

      Map<String,?> configuration()
      Returns:
      Kafka producer configuration
    • runOnSendingThread

      @CheckReturnValue <R> io.smallrye.mutiny.Uni<R> runOnSendingThread(Function<org.apache.kafka.clients.producer.Producer<K,V>,R> action)
      Runs an action on the sending thread.

      The action is a function taking as parameter the Producer 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 runOnSendingThread(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.
    • runOnSendingThread

      @CheckReturnValue io.smallrye.mutiny.Uni<Void> runOnSendingThread(Consumer<org.apache.kafka.clients.producer.Producer<K,V>> action)
      Runs an action on the sending thread.

      The action is a consumer receiving the Producer. 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.
    • send

      @CheckReturnValue io.smallrye.mutiny.Uni<org.apache.kafka.clients.producer.RecordMetadata> send(org.apache.kafka.clients.producer.ProducerRecord<K,V> record)
      Send a record to a topic. The returned Uni completes with RecordMetadata when the send has been acknowledged, or with an exception in case of an error.
    • flush

      @CheckReturnValue io.smallrye.mutiny.Uni<Void> flush()
      Sends all buffered records immediately. The returned Uni completes when all requests belonging to the buffered records complete. In other words, when the returned Uni completes, all previous send(ProducerRecord) operations are known to be complete as well. No guarantee is made about the completion of records sent after flush was called.
    • partitionsFor

      @CheckReturnValue io.smallrye.mutiny.Uni<List<org.apache.kafka.common.PartitionInfo>> partitionsFor(String topic)
      Returns a list of partition metadata for given topic.
    • initTransactions

      @CheckReturnValue io.smallrye.mutiny.Uni<Void> initTransactions()
      Returns:
      the Uni emitting null when the Producer.initTransactions() executes.
    • beginTransaction

      @CheckReturnValue io.smallrye.mutiny.Uni<Void> beginTransaction()
      Returns:
      the Uni emitting null when the Producer.beginTransaction() executes.
    • commitTransaction

      @CheckReturnValue io.smallrye.mutiny.Uni<Void> commitTransaction()
      Returns:
      the Uni emitting null when the Producer.commitTransaction() executes.
    • abortTransaction

      @CheckReturnValue io.smallrye.mutiny.Uni<Void> abortTransaction()
      Returns:
      the Uni emitting null when the Producer.abortTransaction() executes.
    • sendOffsetsToTransaction

      @CheckReturnValue io.smallrye.mutiny.Uni<Void> sendOffsetsToTransaction(Map<org.apache.kafka.common.TopicPartition,org.apache.kafka.clients.consumer.OffsetAndMetadata> offsets, org.apache.kafka.clients.consumer.ConsumerGroupMetadata groupMetadata)
      Parameters:
      offsets - topic partition offsets to commit into transaction
      groupMetadata - consumer group metadata of the exactly-once consumer
      Returns:
      the Uni emitting null when the Producer.sendOffsetsToTransaction(Map, ConsumerGroupMetadata) executes.
    • unwrap

      org.apache.kafka.clients.producer.Producer<K,V> unwrap()
      Returns:
      the underlying producer. Be aware that to use it you needs to be on the sending thread.
    • close

      void close()
      Close the producer client