Class CommonDelegatingErrorHandler
- java.lang.Object
-
- org.springframework.kafka.listener.CommonDelegatingErrorHandler
-
- All Implemented Interfaces:
CommonErrorHandler,DeliveryAttemptAware
public class CommonDelegatingErrorHandler extends java.lang.Object implements CommonErrorHandler
An error handler that delegates to different error handlers, depending on the exception type. The delegates must have compatible properties (isAckAfterHandle()etc.CommonErrorHandler.deliveryAttemptHeader()is not supported - always returns false.- Since:
- 2.8
-
-
Constructor Summary
Constructors Constructor Description CommonDelegatingErrorHandler(CommonErrorHandler defaultErrorHandler)Construct an instance with a default error handler that will be invoked if the exception has no matches.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddDelegate(java.lang.Class<? extends java.lang.Throwable> throwable, CommonErrorHandler handler)Add a delegate to the end of the current collection.voidclearThreadState()Optional method to clear thread state; will be called just before a consumer thread terminates.voidhandleBatch(java.lang.Exception thrownException, org.apache.kafka.clients.consumer.ConsumerRecords<?,?> data, org.apache.kafka.clients.consumer.Consumer<?,?> consumer, MessageListenerContainer container, java.lang.Runnable invokeListener)Handle the exception for a batch listener.voidhandleOtherException(java.lang.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(java.lang.Exception thrownException, java.util.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.remainingRecords()returns true.booleanisAckAfterHandle()Return true if the offset should be committed for a handled error (no exception thrown).booleanremainingRecords()Return false if this error handler should only receive the current failed record; remaining records will be passed to the listener after the error handler returns.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).voidsetErrorHandlers(java.util.Map<java.lang.Class<? extends java.lang.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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.springframework.kafka.listener.CommonErrorHandler
deliveryAttempt, deliveryAttemptHeader, handleRecord
-
-
-
-
Constructor Detail
-
CommonDelegatingErrorHandler
public CommonDelegatingErrorHandler(CommonErrorHandler defaultErrorHandler)
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 Detail
-
setErrorHandlers
public void setErrorHandlers(java.util.Map<java.lang.Class<? extends java.lang.Throwable>,CommonErrorHandler> delegates)
Set the delegate error handlers; aLinkedHashMapargument is recommended so that the delegates are searched in a known order.- Parameters:
delegates- the delegates.
-
remainingRecords
public boolean remainingRecords()
Description copied from interface:CommonErrorHandlerReturn false if this error handler should only receive the current failed record; remaining records will be passed to the listener after the error handler returns. When true (default), all remaining records including the failed record are passed to the error handler.- Specified by:
remainingRecordsin interfaceCommonErrorHandler- Returns:
- false to receive only the failed record.
- See Also:
CommonErrorHandler.handleRecord(Exception, ConsumerRecord, Consumer, MessageListenerContainer),CommonErrorHandler.handleRemaining(Exception, List, Consumer, MessageListenerContainer)
-
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
-
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
public void addDelegate(java.lang.Class<? extends java.lang.Throwable> throwable, CommonErrorHandler handler)Add a delegate to the end of the current collection.- Parameters:
throwable- the throwable for this handler.handler- the handler.
-
handleRemaining
public void handleRemaining(java.lang.Exception thrownException, java.util.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.remainingRecords()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:
CommonErrorHandler.remainingRecords()
-
handleBatch
public void handleBatch(java.lang.Exception thrownException, org.apache.kafka.clients.consumer.ConsumerRecords<?,?> data, org.apache.kafka.clients.consumer.Consumer<?,?> consumer, MessageListenerContainer container, java.lang.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.
-
handleOtherException
public void handleOtherException(java.lang.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.
-
-