Class BatchListenerFailedException

java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
org.springframework.core.NestedRuntimeException
org.springframework.kafka.KafkaException
org.springframework.kafka.listener.BatchListenerFailedException
All Implemented Interfaces:
Serializable

public class BatchListenerFailedException extends KafkaException
An exception thrown by a batch listener to indicate which record in the batch failed. The framework will commit the offsets of all records before the failed record and seek the remaining records (starting from the failed one) for redelivery.

Important contract: throwing this exception carries an implicit assertion that every record before the indicated index (or before the provided ConsumerRecord) has been fully and irreversibly processed. The framework commits those preceding offsets immediately; they will not be redelivered.

This exception is not appropriate when:

  • records are processed in parallel (e.g. using virtual threads or an executor), because preceding records may still be in-flight when the exception is thrown;
  • processing involves multiple steps (e.g. transform then produce to an output topic), because earlier records may have completed only some steps;
  • the listener is transactional and a full rollback is required on any failure.
In those cases, throw a plain RuntimeException instead so that the entire batch is redelivered.
Since:
2.5
See Also:
  • Constructor Details

    • BatchListenerFailedException

      public BatchListenerFailedException(String message, int index)
      Construct an instance with the provided properties.
      Parameters:
      message - the message.
      index - the index in the batch of the failed record.
    • BatchListenerFailedException

      public BatchListenerFailedException(String message, @Nullable Throwable cause, int index)
      Construct an instance with the provided properties.
      Parameters:
      message - the message.
      cause - the cause.
      index - the index in the batch of the failed record.
    • BatchListenerFailedException

      public BatchListenerFailedException(String message, org.apache.kafka.clients.consumer.ConsumerRecord<?,?> record)
      Construct an instance with the provided properties.
      Parameters:
      message - the message.
      record - the failed record.
    • BatchListenerFailedException

      public BatchListenerFailedException(String message, @Nullable Throwable cause, org.apache.kafka.clients.consumer.ConsumerRecord<?,?> record)
      Construct an instance with the provided properties.
      Parameters:
      message - the message.
      cause - the cause.
      record - the failed record.
  • Method Details

    • getRecord

      public @Nullable org.apache.kafka.clients.consumer.ConsumerRecord<?,?> getRecord()
      Return the failed record.
      Returns:
      the record.
    • getIndex

      public int getIndex()
      Return the index in the batch of the failed record.
      Returns:
      the index.
    • getMessage

      public String getMessage()
      Overrides:
      getMessage in class Throwable