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
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.
RuntimeException instead so that the entire batch
is redelivered.- Since:
- 2.5
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class KafkaException
KafkaException.Level -
Constructor Summary
ConstructorsConstructorDescriptionBatchListenerFailedException(String message, int index) Construct an instance with the provided properties.BatchListenerFailedException(String message, @Nullable Throwable cause, int index) Construct an instance with the provided properties.BatchListenerFailedException(String message, @Nullable Throwable cause, org.apache.kafka.clients.consumer.ConsumerRecord<?, ?> record) Construct an instance with the provided properties.BatchListenerFailedException(String message, org.apache.kafka.clients.consumer.ConsumerRecord<?, ?> record) Construct an instance with the provided properties. -
Method Summary
Modifier and TypeMethodDescriptionintgetIndex()Return the index in the batch of the failed record.@Nullable org.apache.kafka.clients.consumer.ConsumerRecord<?, ?> Return the failed record.Methods inherited from class KafkaException
selfLogMethods inherited from class org.springframework.core.NestedRuntimeException
contains, getMostSpecificCause, getRootCauseMethods inherited from class Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Constructor Details
-
BatchListenerFailedException
Construct an instance with the provided properties.- Parameters:
message- the message.index- the index in the batch of the failed record.
-
BatchListenerFailedException
-
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
-
-
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
- Overrides:
getMessagein classThrowable
-