Interface KafkaTransactions<T>

Type Parameters:
T - emitted payload type
All Superinterfaces:
io.smallrye.reactive.messaging.EmitterType
All Known Implementing Classes:
KafkaTransactionsImpl

@TechPreview("Tech Preview API") public interface KafkaTransactions<T> extends io.smallrye.reactive.messaging.EmitterType
Emitter interface for producing records in a Kafka transaction. This interface defines a custom Emitter for implementing Kafka Producer Transactions and Exactly-once processing.

Transactional producer:

 

 @Inject @Channel("tx-out-channel") KafkaTransactions<String> txEmitter;

 // ...
 Uni<Integer> txWork = txEmitter.withTransaction(emitter -> {
     emitter.send("a");
     emitter.send("b");
     emitter.send("c");
     return Uni.createFrom().item(3);
 });

 // ...
 
 

Exactly-once processing:

 

 @Inject @Channel("tx-out-channel") KafkaTransactions<String> txEmitter;

 @Incoming("in-channel")
 Uni<Void> process(KafkaRecordBatch<String, String> batch) {
     return txEmitter.withTransaction(batch, emitter -> {
         for (KafkaRecord<String, String> rec : batch) {
           txEmitter.send(KafkaRecord.of(record.getKey(), record.getPayload()));
         }
         return Uni.createFrom().voidItem();
     });
 }
 
 
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    <R> io.smallrye.mutiny.Uni<R>
    withTransaction(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordBatchMetadata<?,?> metadata, Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
    Produce records in a Kafka transaction, by processing the given incoming record batch exactly-once.
    <R> io.smallrye.mutiny.Uni<R>
    withTransaction(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata<?,?> metadata, Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
    Produce records in a Kafka transaction, by processing the given incoming record exactly-once.
    <R> io.smallrye.mutiny.Uni<R>
    withTransaction(Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
    Produce records in a Kafka transaction.
    <R> io.smallrye.mutiny.Uni<R>
    withTransaction(org.eclipse.microprofile.reactive.messaging.Message<?> message, Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
    Produce records in a Kafka transaction, by processing the given message exactly-once.
    default io.smallrye.mutiny.Uni<Void>
    withTransactionAndAck(org.eclipse.microprofile.reactive.messaging.Message<?> batchMessage, Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<Void>> work)
    Produce records in a Kafka transaction, by processing the given batch message exactly-once.
    <R> R
    withTransactionAndAwait(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordBatchMetadata<?,?> metadata, Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
    Produce records in a Kafka transaction processing the given incoming record batch exactly-once, blocking the calling thread until the transaction completes.
    <R> R
    withTransactionAndAwait(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata<?,?> metadata, Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
    Produce records in a Kafka transaction processing the given incoming record exactly-once, blocking the calling thread until the transaction completes.
    <R> R
    withTransactionAndAwait(Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
    Produce records in a Kafka transaction, blocking the calling thread until the transaction completes.
  • Method Details

    • withTransaction

      @CheckReturnValue <R> io.smallrye.mutiny.Uni<R> withTransaction(Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
      Produce records in a Kafka transaction.

      The given processing function receives a TransactionalEmitter for producing records, and returns a Uni that will provide the result for a successful transaction.

      If this method is called on a Vert.x context, the processing function is also called on that context. Otherwise, it is called on the sending thread of the producer.

      If the processing completes successfully, the producer is flushed and the transaction is committed. If the processing throws an exception, returns a failing Uni, or marks the TransactionalEmitter for abort, the transaction is aborted.

      Type Parameters:
      R - the return type
      Parameters:
      work - the processing function for producing records.
      Returns:
      the Uni representing the result of the transaction. If the transaction completes successfully, it will complete with the item returned from the work function. If the transaction completes with failure, it will fail with the reason.
      Throws:
      IllegalStateException - if a transaction is already in progress.
    • withTransactionAndAwait

      <R> R withTransactionAndAwait(Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
      Produce records in a Kafka transaction, blocking the calling thread until the transaction completes.

      This is a synchronous variant of withTransaction(Function), intended for use in blocking contexts such as REST endpoints running on worker threads or @Blocking annotated methods.

      Must not be called on a Vert.x event loop thread.

      Type Parameters:
      R - the return type
      Parameters:
      work - the processing function for producing records.
      Returns:
      the result of the transaction.
      Throws:
      IllegalStateException - if a transaction is already in progress.
      See Also:
    • withTransaction

      @CheckReturnValue <R> io.smallrye.mutiny.Uni<R> withTransaction(org.eclipse.microprofile.reactive.messaging.Message<?> message, Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
      Produce records in a Kafka transaction, by processing the given message exactly-once.

      If the processing completes successfully, before committing the transaction, the topic partition offsets of the given message will be committed to the transaction. If the processing needs to abort, after aborting the transaction, the consumer's position is reset to the last committed offset, effectively resuming the consumption from that offset.

      Parameters:
      message - the incoming Kafka message expected to contain a metadata IncomingKafkaRecordBatchMetadata or IncomingKafkaRecordMetadata.
      work - the processing function for producing records.
      Returns:
      the Uni representing the result of the transaction. If the transaction completes successfully, it will complete with the item returned from the work function. If the transaction completes with failure, it will fail with the reason.
      Throws:
      IllegalStateException - if a transaction is already in progress.
    • withTransaction

      @CheckReturnValue <R> io.smallrye.mutiny.Uni<R> withTransaction(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata<?,?> metadata, Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
      Produce records in a Kafka transaction, by processing the given incoming record exactly-once.

      If the processing completes successfully, before committing the transaction, the topic partition offset of the given record metadata will be committed to the transaction. If the processing needs to abort, after aborting the transaction, the consumer's position is reset to the last committed offset, effectively resuming the consumption from that offset.

      Parameters:
      metadata - the incoming Kafka record metadata.
      work - the processing function for producing records.
      Returns:
      the Uni representing the result of the transaction.
      Throws:
      IllegalStateException - if a transaction is already in progress.
    • withTransactionAndAwait

      <R> R withTransactionAndAwait(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordMetadata<?,?> metadata, Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
      Produce records in a Kafka transaction processing the given incoming record exactly-once, blocking the calling thread until the transaction completes.

      This is a synchronous variant of withTransaction(IncomingKafkaRecordMetadata, Function). Must not be called on a Vert.x event loop thread.

      Type Parameters:
      R - the return type
      Parameters:
      metadata - the incoming Kafka record metadata.
      work - the processing function for producing records.
      Returns:
      the result of the transaction.
      Throws:
      IllegalStateException - if a transaction is already in progress.
      See Also:
    • withTransaction

      @CheckReturnValue <R> io.smallrye.mutiny.Uni<R> withTransaction(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordBatchMetadata<?,?> metadata, Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
      Produce records in a Kafka transaction, by processing the given incoming record batch exactly-once.

      If the processing completes successfully, before committing the transaction, the topic partition offsets of the given batch metadata will be committed to the transaction. If the processing needs to abort, after aborting the transaction, the consumer's position is reset to the last committed offset, effectively resuming the consumption from that offset.

      Parameters:
      metadata - the incoming Kafka record batch metadata.
      work - the processing function for producing records.
      Returns:
      the Uni representing the result of the transaction.
      Throws:
      IllegalStateException - if a transaction is already in progress.
    • withTransactionAndAwait

      <R> R withTransactionAndAwait(io.smallrye.reactive.messaging.kafka.api.IncomingKafkaRecordBatchMetadata<?,?> metadata, Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<R>> work)
      Produce records in a Kafka transaction processing the given incoming record batch exactly-once, blocking the calling thread until the transaction completes.

      This is a synchronous variant of withTransaction(IncomingKafkaRecordBatchMetadata, Function). Must not be called on a Vert.x event loop thread.

      Type Parameters:
      R - the return type
      Parameters:
      metadata - the incoming Kafka record batch metadata.
      work - the processing function for producing records.
      Returns:
      the result of the transaction.
      Throws:
      IllegalStateException - if a transaction is already in progress.
      See Also:
    • withTransactionAndAck

      @CheckReturnValue default io.smallrye.mutiny.Uni<Void> withTransactionAndAck(org.eclipse.microprofile.reactive.messaging.Message<?> batchMessage, Function<TransactionalEmitter<T>,io.smallrye.mutiny.Uni<Void>> work)
      Produce records in a Kafka transaction, by processing the given batch message exactly-once. This is a convenience method that ignores the item returned by the transaction and returns the Uni which
      • acks the given message if the transaction is successful.
      • nacks the given message if the transaction is aborted.
      Parameters:
      batchMessage - the batch message expected to contain a metadata of IncomingKafkaRecordBatchMetadata.
      work - function for producing records.
      Returns:
      the Uni acking or negative acking the message depending on the result of the transaction.
      Throws:
      IllegalStateException - if a transaction is already in progress.
      See Also:
    • isTransactionInProgress

      boolean isTransactionInProgress()
      Returns:
      true if a transaction is in progress.