Class CommonDelegatingErrorHandler

java.lang.Object
org.springframework.kafka.listener.CommonDelegatingErrorHandler
All Implemented Interfaces:
CommonErrorHandler, DeliveryAttemptAware

public class CommonDelegatingErrorHandler extends 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
Author:
Gary Russell, Adrian Chlebosz, Antonin Arquey
  • Constructor Details

    • 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 Details

    • setErrorHandlers

      public void setErrorHandlers(Map<Class<? extends Throwable>,CommonErrorHandler> delegates)
      Set the delegate error handlers; a LinkedHashMap argument 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 classified by BinaryExceptionClassifier will be retrieved.
      Parameters:
      causeChainTraversing - the causeChainTraversing flag.
      Since:
      2.8.8
    • seeksAfterHandling

      public boolean seeksAfterHandling()
      Description copied from interface: CommonErrorHandler
      Return 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:
      seeksAfterHandling in interface CommonErrorHandler
      Returns:
      true if the next poll should fetch records.
    • clearThreadState

      public void clearThreadState()
      Description copied from interface: CommonErrorHandler
      Optional method to clear thread state; will be called just before a consumer thread terminates.
      Specified by:
      clearThreadState in interface CommonErrorHandler
    • isAckAfterHandle

      public boolean isAckAfterHandle()
      Description copied from interface: CommonErrorHandler
      Return true if the offset should be committed for a handled error (no exception thrown).
      Specified by:
      isAckAfterHandle in interface CommonErrorHandler
      Returns:
      true to commit.
    • setAckAfterHandle

      public void setAckAfterHandle(boolean ack)
      Description copied from interface: CommonErrorHandler
      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).
      Specified by:
      setAckAfterHandle in interface CommonErrorHandler
      Parameters:
      ack - false to not commit.
    • addDelegate

      public void addDelegate(Class<? extends Throwable> throwable, CommonErrorHandler handler)
      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: CommonErrorHandler
      Handle the exception for a record listener when CommonErrorHandler.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:
      handleRemaining in interface CommonErrorHandler
      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: CommonErrorHandler
      Handle the exception for a batch listener. The complete ConsumerRecords from the poll is supplied. The error handler needs to perform seeks if you wish to reprocess the records in the batch.
      Specified by:
      handleBatch in interface CommonErrorHandler
      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(Exception thrownException, org.apache.kafka.clients.consumer.Consumer<?,?> consumer, MessageListenerContainer container, boolean batchListener)
      Description copied from interface: CommonErrorHandler
      Called when an exception is thrown with no records available, e.g. if the consumer poll throws an exception.
      Specified by:
      handleOtherException in interface CommonErrorHandler
      Parameters:
      thrownException - the exception.
      consumer - the consumer.
      container - the container.
      batchListener - true if the listener is a batch listener.