Class FailedRecordProcessor

All Implemented Interfaces:
DeliveryAttemptAware
Direct Known Subclasses:
DefaultAfterRollbackProcessor, FailedBatchProcessor

public abstract class FailedRecordProcessor extends ExceptionClassifier implements DeliveryAttemptAware
Common super class for classes that deal with failing to consume a consumer record.
Since:
2.3.1
  • Field Details

    • logger

      protected final org.springframework.core.log.LogAccessor logger
  • Constructor Details

    • FailedRecordProcessor

      protected FailedRecordProcessor(@Nullable BiConsumer<org.apache.kafka.clients.consumer.ConsumerRecord<?,?>, Exception> recoverer, org.springframework.util.backoff.BackOff backOff)
    • FailedRecordProcessor

      protected FailedRecordProcessor(@Nullable BiConsumer<org.apache.kafka.clients.consumer.ConsumerRecord<?,?>, Exception> recoverer, org.springframework.util.backoff.BackOff backOff, @Nullable BackOffHandler backOffHandler)
  • Method Details

    • isCommitRecovered

      protected boolean isCommitRecovered()
      Whether the offset for a recovered record should be committed.
      Returns:
      true to commit recovered record offsets.
    • setCommitRecovered

      public void setCommitRecovered(boolean commitRecovered)
      Set to true to commit the offset for a recovered record.
      Parameters:
      commitRecovered - true to commit.
    • setBackOffFunction

      public void setBackOffFunction(BiFunction<org.apache.kafka.clients.consumer.ConsumerRecord<?,?>, Exception, @Nullable org.springframework.util.backoff.BackOff> backOffFunction)
      Set a function to dynamically determine the BackOff to use, based on the consumer record and/or exception. If null is returned, the default BackOff will be used.
      Parameters:
      backOffFunction - the function.
      Since:
      2.6
    • setResetStateOnRecoveryFailure

      public void setResetStateOnRecoveryFailure(boolean resetStateOnRecoveryFailure)
      Set to false to immediately attempt to recover on the next attempt instead of repeating the BackOff cycle when recovery fails.
      Parameters:
      resetStateOnRecoveryFailure - false to retain state.
      Since:
      2.5.5
    • setResetStateOnExceptionChange

      public void setResetStateOnExceptionChange(boolean resetStateOnExceptionChange)
      Set to true to reset the retry BackOff if the exception is a different type to the previous failure for the same record. The backOffFunction, if provided, will be called to get the BackOff to use for the new exception; otherwise, the configured BackOff will be used. Default true since 2.9; set to false to use the existing retry state, even when exceptions change.
      Parameters:
      resetStateOnExceptionChange - true to reset.
      Since:
      2.6.3
    • setMaxRecoveryFailures

      public void setMaxRecoveryFailures(int maxRecoveryFailures)
      Set the number of times the recoverer is allowed to throw for the same record. When the recoverer throws for the nth time, that failure is the last one: the record is logged at ERROR level and treated as recovered, so it is no longer included in the seeks and its offset is committed according to the container's ack mode. A value of 1 means the record is skipped as soon as the first recovery attempt fails. RetryListener.recoveryFailed(ConsumerRecord, Exception, Exception) is called for every failure, including the last one, but RetryListener.recovered(ConsumerRecord, Exception) is not, because recovery never succeeded. The count is kept per thread, partition and offset; it is reset once the record is skipped, when recovery succeeds, when a different offset fails on the partition, and by clearThreadState() / clearThreadStateFor(Collection). Only applies to record listeners; batch listeners do not recover through this mechanism. By default there is no limit and a record whose recovery keeps failing is redelivered until the recoverer succeeds.
      Parameters:
      maxRecoveryFailures - the number of recovery failures to allow; must be greater than 0.
      Since:
      4.2
      See Also:
    • setRetryListeners

      public void setRetryListeners(RetryListener... listeners)
      Set one or more RetryListener to receive notifications of retries and recovery.
      Parameters:
      listeners - the listeners.
      Since:
      2.7
    • getRetryListeners

      protected List<RetryListener> getRetryListeners()
    • isSeekAfterError

      public boolean isSeekAfterError()
      Return whether to seek after an exception is handled.
      Returns:
      true to seek.
      Since:
      2.9
    • setSeekAfterError

      public void setSeekAfterError(boolean seekAfterError)
      When true (default), the error handler will perform seeks on the failed and/or remaining records to they will be redelivered on the next poll. When false, the container will present the failed and/or remaining records to the listener by pausing the consumer for the next poll and using the existing records from the previous poll. When false; has the side-effect of setting setResetStateOnExceptionChange(boolean) to true.
      Parameters:
      seekAfterError - false to not perform seeks.
      Since:
      2.9
    • deliveryAttempt

      public int deliveryAttempt(TopicPartitionOffset topicPartitionOffset)
      Description copied from interface: DeliveryAttemptAware
      Return the next delivery attempt for the topic/partition/offset.
      Specified by:
      deliveryAttempt in interface DeliveryAttemptAware
      Parameters:
      topicPartitionOffset - the topic/partition/offset.
      Returns:
      the next delivery attempt.
    • getFailureTracker

      protected org.springframework.kafka.listener.FailedRecordTracker getFailureTracker()
      Return the failed record tracker.
      Returns:
      the tracker.
      Since:
      2.9
    • clearThreadState

      public void clearThreadState()
    • clearThreadStateFor

      public void clearThreadStateFor(Collection<org.apache.kafka.common.TopicPartition> partitions)