Class RedeliveryPolicy

java.lang.Object
org.apache.camel.processor.errorhandler.RedeliveryPolicy
All Implemented Interfaces:
Serializable, Cloneable

@Configurer public class RedeliveryPolicy extends Object implements Cloneable, Serializable
The policy used to decide how many times to redeliver and the time between the redeliveries before being sent to a Dead Letter Channel

The default values are:

  • maximumRedeliveries = 0
  • redeliveryDelay = 1000L (the initial delay)
  • maximumRedeliveryDelay = 60 * 1000L
  • asyncDelayedRedelivery = false
  • backOffMultiplier = 2
  • useExponentialBackOff = false
  • collisionAvoidanceFactor = 0.15d
  • useCollisionAvoidance = false
  • retriesExhaustedLogLevel = LoggingLevel.ERROR
  • retryAttemptedLogLevel = LoggingLevel.DEBUG
  • retryAttemptedLogInterval = 1
  • logRetryAttempted = true
  • logRetryStackTrace = false
  • logStackTrace = true
  • logHandled = false
  • logExhausted = true
  • logExhaustedMessageHistory = true
  • logExhaustedMessageBody = false
  • logNewException = true
  • allowRedeliveryWhileStopping = true

Setting the maximumRedeliveries to a negative value such as -1 will then always redeliver (unlimited). Setting the maximumRedeliveries to 0 will disable redelivery.

This policy can be configured either by one of the following two settings:

  • using conventional options, using all the options defined above
  • using delay pattern to declare intervals for delays

Note: If using delay patterns then the following options is not used (delay, backOffMultiplier, useExponentialBackOff, useCollisionAvoidance)

Using delay pattern:
The delay pattern syntax is: limit:delay;limit 2:delay 2;limit 3:delay 3;...;limit N:delay N.

How it works is best illustrate with an example with this pattern: delayPattern=5:1000;10:5000:20:20000
The delays will be for attempt in range 0..4 = 0 millis, 5..9 = 1000 millis, 10..19 = 5000 millis, >= 20 = 20000 millis.

If you want to set a starting delay, then use 0 as the first limit, eg: 0:1000;5:5000 will use 1 sec delay until attempt number 5 where it will use 5 seconds going forward.

See Also:
  • Field Details

    • DEFAULT_POLICY

      public static final RedeliveryPolicy DEFAULT_POLICY
    • randomNumberGenerator

      protected static Random randomNumberGenerator
    • redeliveryDelay

      protected long redeliveryDelay
    • maximumRedeliveries

      protected int maximumRedeliveries
    • maximumRedeliveryDelay

      protected long maximumRedeliveryDelay
    • backOffMultiplier

      protected double backOffMultiplier
    • useExponentialBackOff

      protected boolean useExponentialBackOff
    • collisionAvoidanceFactor

      protected double collisionAvoidanceFactor
    • useCollisionAvoidance

      protected boolean useCollisionAvoidance
    • retriesExhaustedLogLevel

      protected org.apache.camel.LoggingLevel retriesExhaustedLogLevel
    • retryAttemptedLogLevel

      protected org.apache.camel.LoggingLevel retryAttemptedLogLevel
    • retryAttemptedLogInterval

      protected int retryAttemptedLogInterval
    • logStackTrace

      protected boolean logStackTrace
    • logRetryStackTrace

      protected boolean logRetryStackTrace
    • logHandled

      protected boolean logHandled
    • logContinued

      protected boolean logContinued
    • logExhausted

      protected boolean logExhausted
    • logNewException

      protected boolean logNewException
    • logExhaustedMessageHistory

      protected Boolean logExhaustedMessageHistory
    • logExhaustedMessageBody

      protected Boolean logExhaustedMessageBody
    • logRetryAttempted

      protected boolean logRetryAttempted
    • delayPattern

      protected String delayPattern
    • asyncDelayedRedelivery

      protected boolean asyncDelayedRedelivery
    • allowRedeliveryWhileStopping

      protected boolean allowRedeliveryWhileStopping
    • exchangeFormatterRef

      protected String exchangeFormatterRef
  • Constructor Details

    • RedeliveryPolicy

      public RedeliveryPolicy()
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • copy

      public RedeliveryPolicy copy()
    • shouldRedeliver

      public boolean shouldRedeliver(org.apache.camel.Exchange exchange, int redeliveryCounter, org.apache.camel.Predicate retryWhile)
      Returns true if the policy decides that the message exchange should be redelivered.
      Parameters:
      exchange - the current exchange
      redeliveryCounter - the current retry counter
      retryWhile - an optional predicate to determine if we should redeliver or not
      Returns:
      true to redeliver, false to stop
    • sleep

      public long sleep(long redeliveryDelay, int redeliveryCounter) throws InterruptedException
      Calculates the new redelivery delay based on the last one and then sleeps for the necessary amount of time.

      This implementation will block while sleeping.

      Parameters:
      redeliveryDelay - previous redelivery delay
      redeliveryCounter - number of previous redelivery attempts
      Returns:
      the calculate delay
      Throws:
      InterruptedException - is thrown if the sleep is interrupted likely because of shutdown
    • sleep

      public void sleep(long redeliveryDelay) throws InterruptedException
      Sleeps for the given delay
      Parameters:
      redeliveryDelay - the delay
      Throws:
      InterruptedException - is thrown if the sleep is interrupted likely because of shutdown
    • calculateRedeliveryDelay

      public long calculateRedeliveryDelay(long previousDelay, int redeliveryCounter)
      Calculates the new redelivery delay based on the last one
      Parameters:
      previousDelay - previous redelivery delay
      redeliveryCounter - number of previous redelivery attempts
      Returns:
      the calculate delay
    • calculateRedeliverDelayUsingPattern

      protected static long calculateRedeliverDelayUsingPattern(String delayPattern, int redeliveryCounter)
      Calculates the delay using the delay pattern
    • redeliveryDelay

      public RedeliveryPolicy redeliveryDelay(long delay)
      Sets the initial redelivery delay in milliseconds
    • maximumRedeliveries

      public RedeliveryPolicy maximumRedeliveries(int maximumRedeliveries)
      Sets the maximum number of times a message exchange will be redelivered
    • useCollisionAvoidance

      public RedeliveryPolicy useCollisionAvoidance()
      Enables collision avoidance which adds some randomization to the backoff timings to reduce contention probability
    • useExponentialBackOff

      public RedeliveryPolicy useExponentialBackOff()
      Enables exponential backoff using the getBackOffMultiplier() to increase the time between retries
    • backOffMultiplier

      public RedeliveryPolicy backOffMultiplier(double multiplier)
      Enables exponential backoff and sets the multiplier used to increase the delay between redeliveries
    • collisionAvoidancePercent

      public RedeliveryPolicy collisionAvoidancePercent(double collisionAvoidancePercent)
      Enables collision avoidance and sets the percentage used
    • maximumRedeliveryDelay

      public RedeliveryPolicy maximumRedeliveryDelay(long maximumRedeliveryDelay)
      Sets the maximum redelivery delay if using exponential back off. Use -1 if you wish to have no maximum
    • retriesExhaustedLogLevel

      public RedeliveryPolicy retriesExhaustedLogLevel(org.apache.camel.LoggingLevel retriesExhaustedLogLevel)
      Sets the logging level to use for log messages when retries have been exhausted.
    • retryAttemptedLogLevel

      public RedeliveryPolicy retryAttemptedLogLevel(org.apache.camel.LoggingLevel retryAttemptedLogLevel)
      Sets the logging level to use for log messages when retries are attempted.
    • retryAttemptedLogInterval

      public RedeliveryPolicy retryAttemptedLogInterval(int logRetryAttemptedInterval)
      Sets the interval to log retry attempts
    • logRetryAttempted

      public RedeliveryPolicy logRetryAttempted(boolean logRetryAttempted)
      Sets whether to log retry attempts
    • logStackTrace

      public RedeliveryPolicy logStackTrace(boolean logStackTrace)
      Sets whether to log stacktrace for failed messages.
    • logRetryStackTrace

      public RedeliveryPolicy logRetryStackTrace(boolean logRetryStackTrace)
      Sets whether to log stacktrace for failed redelivery attempts
    • logHandled

      public RedeliveryPolicy logHandled(boolean logHandled)
      Sets whether to log errors even if its handled
    • logNewException

      public RedeliveryPolicy logNewException(boolean logNewException)
      Sets whether errors should be logged when a new exception occurred during handling a previous exception
    • logExhausted

      public RedeliveryPolicy logExhausted(boolean logExhausted)
      Sets whether to log exhausted errors
    • logExhaustedMessageHistory

      public RedeliveryPolicy logExhaustedMessageHistory(boolean logExhaustedMessageHistory)
      Sets whether to log exhausted errors including message history
    • logExhaustedMessageBody

      public RedeliveryPolicy logExhaustedMessageBody(boolean logExhaustedMessageBody)
      Sets whether to log exhausted errors including message body (requires message history to be enabled)
    • delayPattern

      public RedeliveryPolicy delayPattern(String delayPattern)
      Sets the delay pattern with delay intervals.
    • disableRedelivery

      public RedeliveryPolicy disableRedelivery()
      Disables redelivery by setting maximum redeliveries to 0.
    • asyncDelayedRedelivery

      public RedeliveryPolicy asyncDelayedRedelivery()
      Allow asynchronous delayed redelivery.
      See Also:
    • allowRedeliveryWhileStopping

      public RedeliveryPolicy allowRedeliveryWhileStopping(boolean redeliverWhileStopping)
      Controls whether to allow redelivery while stopping/shutting down a route that uses error handling.
      Parameters:
      redeliverWhileStopping - true to allow redelivery, false to reject redeliveries
    • exchangeFormatterRef

      public RedeliveryPolicy exchangeFormatterRef(String exchangeFormatterRef)
      Sets the reference of the instance of ExchangeFormatter to generate the log message from exchange.
      Parameters:
      exchangeFormatterRef - name of the instance of ExchangeFormatter
      Returns:
      the builder
    • getRedeliveryDelay

      public long getRedeliveryDelay()
    • setRedeliveryDelay

      public void setRedeliveryDelay(long redeliverDelay)
      Sets the initial redelivery delay in milliseconds
    • getBackOffMultiplier

      public double getBackOffMultiplier()
    • setBackOffMultiplier

      public void setBackOffMultiplier(double backOffMultiplier)
      Sets the multiplier used to increase the delay between redeliveries if setUseExponentialBackOff(boolean) is enabled
    • getCollisionAvoidancePercent

      public long getCollisionAvoidancePercent()
    • setCollisionAvoidancePercent

      public void setCollisionAvoidancePercent(double collisionAvoidancePercent)
      Sets the percentage used for collision avoidance if enabled via setUseCollisionAvoidance(boolean)
    • getCollisionAvoidanceFactor

      public double getCollisionAvoidanceFactor()
    • setCollisionAvoidanceFactor

      public void setCollisionAvoidanceFactor(double collisionAvoidanceFactor)
      Sets the factor used for collision avoidance if enabled via setUseCollisionAvoidance(boolean)
    • getMaximumRedeliveries

      public int getMaximumRedeliveries()
    • setMaximumRedeliveries

      public void setMaximumRedeliveries(int maximumRedeliveries)
      Sets the maximum number of times a message exchange will be redelivered. Setting a negative value will retry forever.
    • getMaximumRedeliveryDelay

      public long getMaximumRedeliveryDelay()
    • setMaximumRedeliveryDelay

      public void setMaximumRedeliveryDelay(long maximumRedeliveryDelay)
      Sets the maximum redelivery delay. Use -1 if you wish to have no maximum
    • isUseCollisionAvoidance

      public boolean isUseCollisionAvoidance()
    • setUseCollisionAvoidance

      public void setUseCollisionAvoidance(boolean useCollisionAvoidance)
      Enables/disables collision avoidance which adds some randomization to the backoff timings to reduce contention probability
    • isUseExponentialBackOff

      public boolean isUseExponentialBackOff()
    • setUseExponentialBackOff

      public void setUseExponentialBackOff(boolean useExponentialBackOff)
      Enables/disables exponential backoff using the getBackOffMultiplier() to increase the time between retries
    • getRandomNumberGenerator

      protected static Random getRandomNumberGenerator()
    • setRetriesExhaustedLogLevel

      public void setRetriesExhaustedLogLevel(org.apache.camel.LoggingLevel retriesExhaustedLogLevel)
      Sets the logging level to use for log messages when retries have been exhausted.
    • getRetriesExhaustedLogLevel

      public org.apache.camel.LoggingLevel getRetriesExhaustedLogLevel()
    • setRetryAttemptedLogLevel

      public void setRetryAttemptedLogLevel(org.apache.camel.LoggingLevel retryAttemptedLogLevel)
      Sets the logging level to use for log messages when retries are attempted.
    • getRetryAttemptedLogLevel

      public org.apache.camel.LoggingLevel getRetryAttemptedLogLevel()
    • getRetryAttemptedLogInterval

      public int getRetryAttemptedLogInterval()
    • setRetryAttemptedLogInterval

      public void setRetryAttemptedLogInterval(int retryAttemptedLogInterval)
      Sets the interval to log retry attempts
    • getDelayPattern

      public String getDelayPattern()
    • setDelayPattern

      public void setDelayPattern(String delayPattern)
      Sets an optional delay pattern to use instead of fixed delay.
    • isLogStackTrace

      public boolean isLogStackTrace()
    • setLogStackTrace

      public void setLogStackTrace(boolean logStackTrace)
      Sets whether stack traces should be logged or not
    • isLogRetryStackTrace

      public boolean isLogRetryStackTrace()
    • setLogRetryStackTrace

      public void setLogRetryStackTrace(boolean logRetryStackTrace)
      Sets whether stack traces should be logged or not
    • isLogHandled

      public boolean isLogHandled()
    • setLogHandled

      public void setLogHandled(boolean logHandled)
      Sets whether errors should be logged even if its handled
    • isLogNewException

      public boolean isLogNewException()
    • setLogNewException

      public void setLogNewException(boolean logNewException)
      Sets whether errors should be logged when a new exception occurred during handling a previous exception
    • isLogContinued

      public boolean isLogContinued()
    • setLogContinued

      public void setLogContinued(boolean logContinued)
      Sets whether errors should be logged even if its continued
    • isLogRetryAttempted

      public boolean isLogRetryAttempted()
    • setLogRetryAttempted

      public void setLogRetryAttempted(boolean logRetryAttempted)
      Sets whether retry attempts should be logged or not
    • isLogExhausted

      public boolean isLogExhausted()
    • setLogExhausted

      public void setLogExhausted(boolean logExhausted)
      Sets whether exhausted exceptions should be logged or not
    • isLogExhaustedMessageHistory

      public boolean isLogExhaustedMessageHistory()
    • getLogExhaustedMessageHistory

      public Boolean getLogExhaustedMessageHistory()
      Whether the option logExhaustedMessageHistory has been configured or not
      Returns:
      null if not configured, or the configured value as true or false
      See Also:
    • setLogExhaustedMessageHistory

      public void setLogExhaustedMessageHistory(boolean logExhaustedMessageHistory)
      Sets whether exhausted exceptions should be logged with message history included.
    • isLogExhaustedMessageBody

      public boolean isLogExhaustedMessageBody()
    • getLogExhaustedMessageBody

      public Boolean getLogExhaustedMessageBody()
      Whether the option logExhaustedMessageBody has been configured or not
      Returns:
      null if not configured, or the configured value as true or false
      See Also:
    • setLogExhaustedMessageBody

      public void setLogExhaustedMessageBody(Boolean logExhaustedMessageBody)
      Sets whether exhausted message body/headers should be logged with message history included (requires logExhaustedMessageHistory to be enabled).
    • isAsyncDelayedRedelivery

      public boolean isAsyncDelayedRedelivery()
    • setAsyncDelayedRedelivery

      public void setAsyncDelayedRedelivery(boolean asyncDelayedRedelivery)
      Sets whether asynchronous delayed redelivery is allowed.

      This is disabled by default.

      When enabled it allows Camel to schedule a future task for delayed redelivery which prevents current thread from blocking while waiting.

      Exchange which is transacted will however always use synchronous delayed redelivery because the transaction must execute in the same thread context.

      Parameters:
      asyncDelayedRedelivery - whether asynchronous delayed redelivery is allowed
    • isAllowRedeliveryWhileStopping

      public boolean isAllowRedeliveryWhileStopping()
    • setAllowRedeliveryWhileStopping

      public void setAllowRedeliveryWhileStopping(boolean allowRedeliveryWhileStopping)
      Controls whether to allow redelivery while stopping/shutting down a route that uses error handling.
      Parameters:
      allowRedeliveryWhileStopping - true to allow redelivery, false to reject redeliveries
    • getExchangeFormatterRef

      public String getExchangeFormatterRef()
    • setExchangeFormatterRef

      public void setExchangeFormatterRef(String exchangeFormatterRef)
      Sets the reference of the instance of ExchangeFormatter to generate the log message from exchange.