Class CommonDelegatingErrorHandler
java.lang.Object
org.springframework.kafka.listener.CommonDelegatingErrorHandler
- All Implemented Interfaces:
CommonErrorHandler, DeliveryAttemptAware
An error handler that delegates to different error handlers, depending on the exception
type. The delegates must have compatible properties (
isAckAfterHandle() etc.
deliveryAttemptHeader() is supported when the default error handler or any
delegate supports it; the delivery attempt is reported by the handler that handled the
latest failure.- Since:
- 2.8
-
Constructor Summary
ConstructorsConstructorDescriptionCommonDelegatingErrorHandler(CommonErrorHandler defaultErrorHandler) Construct an instance with a default error handler that will be invoked if the exception has no matches. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddDelegate(Class<? extends Throwable> throwable, CommonErrorHandler handler) Add a delegate to the end of the current collection.voidOptional method to clear thread state; will be called just before a consumer thread terminates.voidclearThreadStateFor(Collection<org.apache.kafka.common.TopicPartition> partitions) Clear thread state only for the given partitions.intdeliveryAttempt(TopicPartitionOffset topicPartitionOffset) Return the next delivery attempt for the topic/partition/offset.booleanReturn true if this error handler supports delivery attempts headers.voidhandleBatch(Exception thrownException, org.apache.kafka.clients.consumer.ConsumerRecords<?, ?> data, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container, Runnable invokeListener) Handle the exception for a batch listener.<K,V> org.apache.kafka.clients.consumer.ConsumerRecords <K, V> handleBatchAndReturnRemaining(Exception thrownException, org.apache.kafka.clients.consumer.ConsumerRecords<?, ?> data, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container, Runnable invokeListener) Handle the exception for a batch listener.booleanhandleOne(Exception thrownException, org.apache.kafka.clients.consumer.ConsumerRecord<?, ?> record, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container) Handle the exception for a record listener whenCommonErrorHandler.seeksAfterHandling()returns false.voidhandleOtherException(Exception thrownException, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container, boolean batchListener) Called when an exception is thrown with no records available, e.g.voidhandleRemaining(Exception thrownException, List<org.apache.kafka.clients.consumer.ConsumerRecord<?, ?>> records, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container) Handle the exception for a record listener whenCommonErrorHandler.seeksAfterHandling()returns true.booleanReturn true if the offset should be committed for a handled error (no exception thrown).voidonPartitionsAssigned(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, Collection<org.apache.kafka.common.TopicPartition> partitions, Runnable publishPause) Called when partitions are assigned.booleanReturn true if this error handler performs seeks on the failed record and remaining records (or just the remaining records after a failed record is recovered).voidsetAckAfterHandle(boolean ack) Set to false to prevent the container from committing the offset of a recovered record (when the error handler does not itself throw an exception).voidsetCauseChainTraversing(boolean causeChainTraversing) Set the flag enabling deep exception's cause chain traversing.voidsetErrorHandlers(Map<Class<? extends Throwable>, CommonErrorHandler> delegates) Set the delegate error handlers; aLinkedHashMapargument is recommended so that the delegates are searched in a known order.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface CommonErrorHandler
logger
-
Constructor Details
-
CommonDelegatingErrorHandler
Construct an instance with a default error handler that will be invoked if the exception has no matches.- Parameters:
defaultErrorHandler- the default error handler.
-
-
Method Details
-
setErrorHandlers
Set the delegate error handlers; aLinkedHashMapargument is recommended so that the delegates are searched in a known order.- Parameters:
delegates- the delegates.- Throws:
IllegalArgumentException- if any of the delegates is not compatible with the default error handler.
-
setCauseChainTraversing
public void setCauseChainTraversing(boolean causeChainTraversing) Set the flag enabling deep exception's cause chain traversing. If true, the delegate for the first exception match by theExceptionMatcherwill be retrieved.- Parameters:
causeChainTraversing- the causeChainTraversing flag.- Since:
- 2.8.8
-
seeksAfterHandling
public boolean seeksAfterHandling()Description copied from interface:CommonErrorHandlerReturn true if this error handler performs seeks on the failed record and remaining records (or just the remaining records after a failed record is recovered).- Specified by:
seeksAfterHandlingin interfaceCommonErrorHandler- Returns:
- true if the next poll should fetch records.
-
deliveryAttemptHeader
public boolean deliveryAttemptHeader()Description copied from interface:CommonErrorHandlerReturn true if this error handler supports delivery attempts headers.- Specified by:
deliveryAttemptHeaderin interfaceCommonErrorHandler- Returns:
- true if capable.
-
deliveryAttempt
Description copied from interface:DeliveryAttemptAwareReturn the next delivery attempt for the topic/partition/offset.- Specified by:
deliveryAttemptin interfaceCommonErrorHandler- Specified by:
deliveryAttemptin interfaceDeliveryAttemptAware- Parameters:
topicPartitionOffset- the topic/partition/offset.- Returns:
- the next delivery attempt.
-
clearThreadState
public void clearThreadState()Description copied from interface:CommonErrorHandlerOptional method to clear thread state; will be called just before a consumer thread terminates.- Specified by:
clearThreadStatein interfaceCommonErrorHandler
-
clearThreadStateFor
Description copied from interface:CommonErrorHandlerClear thread state only for the given partitions. Called after a successful batch invocation to avoid resetting backoff state for partitions that are still retrying.- Specified by:
clearThreadStateForin interfaceCommonErrorHandler- Parameters:
partitions- the partitions whose retry state should be cleared.
-
onPartitionsAssigned
public void onPartitionsAssigned(org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, Collection<org.apache.kafka.common.TopicPartition> partitions, Runnable publishPause) Description copied from interface:CommonErrorHandlerCalled when partitions are assigned.- Specified by:
onPartitionsAssignedin interfaceCommonErrorHandler- Parameters:
consumer- the consumer.partitions- the newly assigned partitions.publishPause- called to publish a consumer paused event.
-
isAckAfterHandle
public boolean isAckAfterHandle()Description copied from interface:CommonErrorHandlerReturn true if the offset should be committed for a handled error (no exception thrown).- Specified by:
isAckAfterHandlein interfaceCommonErrorHandler- Returns:
- true to commit.
-
setAckAfterHandle
public void setAckAfterHandle(boolean ack) Description copied from interface:CommonErrorHandlerSet to false to prevent the container from committing the offset of a recovered record (when the error handler does not itself throw an exception).- Specified by:
setAckAfterHandlein interfaceCommonErrorHandler- Parameters:
ack- false to not commit.
-
addDelegate
Add a delegate to the end of the current collection.- Parameters:
throwable- the throwable for this handler.handler- the handler.- Throws:
IllegalArgumentException- if the handler is not compatible with the default error handler.
-
handleRemaining
public void handleRemaining(Exception thrownException, List<org.apache.kafka.clients.consumer.ConsumerRecord<?, ?>> records, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container) Description copied from interface:CommonErrorHandlerHandle the exception for a record listener whenCommonErrorHandler.seeksAfterHandling()returns true. The failed record and all the remaining records from the poll are passed in. Usually used when the error handler performs seeks so that the remaining records will be redelivered on the next poll.- Specified by:
handleRemainingin interfaceCommonErrorHandler- Parameters:
thrownException- the exception.records- the remaining records including the one that failed.consumer- the consumer.container- the container.- See Also:
-
handleBatch
public void handleBatch(Exception thrownException, org.apache.kafka.clients.consumer.ConsumerRecords<?, ?> data, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container, Runnable invokeListener) Description copied from interface:CommonErrorHandlerHandle the exception for a batch listener. The completeConsumerRecordsfrom the poll is supplied. The error handler needs to perform seeks if you wish to reprocess the records in the batch.- Specified by:
handleBatchin interfaceCommonErrorHandler- Parameters:
thrownException- the exception.data- the consumer records.consumer- the consumer.container- the container.invokeListener- a callback to re-invoke the listener.
-
handleBatchAndReturnRemaining
public <K,V> org.apache.kafka.clients.consumer.ConsumerRecords<K,V> handleBatchAndReturnRemaining(Exception thrownException, org.apache.kafka.clients.consumer.ConsumerRecords<?, ?> data, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container, Runnable invokeListener) Description copied from interface:CommonErrorHandlerHandle the exception for a batch listener. The completeConsumerRecordsfrom the poll is supplied. Return the members of the batch that should be re-sent to the listener. The returned records MUST be in the same order as the original records.- Specified by:
handleBatchAndReturnRemainingin interfaceCommonErrorHandler- Type Parameters:
K- the key type.V- the value type.- Parameters:
thrownException- the exception.data- the consumer records.consumer- the consumer.container- the container.invokeListener- a callback to re-invoke the listener.- Returns:
- the consumer records, or a subset.
-
handleOtherException
public void handleOtherException(Exception thrownException, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container, boolean batchListener) Description copied from interface:CommonErrorHandlerCalled when an exception is thrown with no records available, e.g. if the consumer poll throws an exception.- Specified by:
handleOtherExceptionin interfaceCommonErrorHandler- Parameters:
thrownException- the exception.consumer- the consumer.container- the container.batchListener- true if the listener is a batch listener.
-
handleOne
public boolean handleOne(Exception thrownException, org.apache.kafka.clients.consumer.ConsumerRecord<?, ?> record, org.apache.kafka.clients.consumer.Consumer<?, ?> consumer, MessageListenerContainer container) Description copied from interface:CommonErrorHandlerHandle the exception for a record listener whenCommonErrorHandler.seeksAfterHandling()returns false. Use this to handle just the single failed record.- Specified by:
handleOnein interfaceCommonErrorHandler- Parameters:
thrownException- the exception.record- the record.consumer- the consumer.container- the container.- Returns:
- true if the error was "handled" or false if not and the container will re-submit the record to the listener.
- See Also:
-