Class PooledKafkaProducer<K,V>

java.lang.Object
io.smallrye.reactive.messaging.kafka.impl.PooledKafkaProducer<K,V>
All Implemented Interfaces:
KafkaProducer<K,V>

public class PooledKafkaProducer<K,V> extends Object implements KafkaProducer<K,V>
  • Constructor Details

  • Method Details

    • send

      @CheckReturnValue public io.smallrye.mutiny.Uni<org.apache.kafka.clients.producer.RecordMetadata> send(org.apache.kafka.clients.producer.ProducerRecord<K,V> record)
      Description copied from interface: KafkaProducer
      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.
      Specified by:
      send in interface KafkaProducer<K,V>
    • flush

      @CheckReturnValue public io.smallrye.mutiny.Uni<Void> flush()
      Description copied from interface: KafkaProducer
      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 KafkaProducer.send(ProducerRecord) operations are known to be complete as well. No guarantee is made about the completion of records sent after flush was called.
      Specified by:
      flush in interface KafkaProducer<K,V>
    • partitionsFor

      @CheckReturnValue public io.smallrye.mutiny.Uni<List<org.apache.kafka.common.PartitionInfo>> partitionsFor(String topic)
      Description copied from interface: KafkaProducer
      Returns a list of partition metadata for given topic.
      Specified by:
      partitionsFor in interface KafkaProducer<K,V>
    • initTransactions

      @CheckReturnValue public io.smallrye.mutiny.Uni<Void> initTransactions()
      Specified by:
      initTransactions in interface KafkaProducer<K,V>
      Returns:
      the Uni emitting null when the Producer.initTransactions() executes.
    • beginTransaction

      @CheckReturnValue public io.smallrye.mutiny.Uni<Void> beginTransaction()
      Specified by:
      beginTransaction in interface KafkaProducer<K,V>
      Returns:
      the Uni emitting null when the Producer.beginTransaction() executes.
    • commitTransaction

      @CheckReturnValue public io.smallrye.mutiny.Uni<Void> commitTransaction()
      Specified by:
      commitTransaction in interface KafkaProducer<K,V>
      Returns:
      the Uni emitting null when the Producer.commitTransaction() executes.
    • abortTransaction

      @CheckReturnValue public io.smallrye.mutiny.Uni<Void> abortTransaction()
      Specified by:
      abortTransaction in interface KafkaProducer<K,V>
      Returns:
      the Uni emitting null when the Producer.abortTransaction() executes.
    • sendOffsetsToTransaction

      @CheckReturnValue public 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)
      Specified by:
      sendOffsetsToTransaction in interface KafkaProducer<K,V>
      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.
    • transactionScope

      public KafkaProducer<K,V> transactionScope()
      Description copied from interface: KafkaProducer
      Creates a new transaction scope for this producer. For pooled producers, returns a lightweight wrapper that acquires a producer from the pool for the duration of a transaction. All sends within the scope go through that single producer, ensuring a single atomic Kafka transaction.

      Multiple scopes can be active concurrently, each using a different pooled producer.

      For regular producers, returns this.

      Specified by:
      transactionScope in interface KafkaProducer<K,V>
      Returns:
      a transaction-scoped producer
    • configuration

      public Map<String,?> configuration()
      Specified by:
      configuration in interface KafkaProducer<K,V>
      Returns:
      Kafka producer configuration
    • runOnSendingThread

      @CheckReturnValue public <R> io.smallrye.mutiny.Uni<R> runOnSendingThread(Function<org.apache.kafka.clients.producer.Producer<K,V>,R> action)
      Description copied from interface: KafkaProducer
      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 KafkaProducer.runOnSendingThread(java.util.function.Consumer).

      Specified by:
      runOnSendingThread in interface KafkaProducer<K,V>
      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 public io.smallrye.mutiny.Uni<Void> runOnSendingThread(Consumer<org.apache.kafka.clients.producer.Producer<K,V>> action)
      Description copied from interface: KafkaProducer
      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.

      Specified by:
      runOnSendingThread in interface KafkaProducer<K,V>
      Parameters:
      action - the action, must not be null
      Returns:
      the Uni emitting null or the failure when the action completes.
    • unwrap

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

      public void close()
      Description copied from interface: KafkaProducer
      Close the producer client
      Specified by:
      close in interface KafkaProducer<K,V>
    • isClosed

      public boolean isClosed()
      Description copied from interface: KafkaProducer
      Whether this producer is closed.
      Specified by:
      isClosed in interface KafkaProducer<K,V>
      Returns:
      true if the producer is closed.
    • isPooled

      public boolean isPooled()
      Description copied from interface: KafkaProducer
      Whether this producer uses a pool of inner producers for concurrent transactions.
      Specified by:
      isPooled in interface KafkaProducer<K,V>
      Returns:
      true if pooled producer
    • getProducers

      public List<ReactiveKafkaProducer<K,V>> getProducers()
      Returns:
      the list of all producers in the pool. Visible for testing.