org.apache.camel.processor
Class RedeliveryPolicy

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

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:

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:

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.

Version:
$Revision: 22118 $
See Also:
Serialized Form

Field Summary
protected  boolean asyncDelayedRedelivery
           
protected  double backOffMultiplier
           
protected  double collisionAvoidanceFactor
           
protected  String delayPattern
           
protected  boolean logContinued
           
protected  boolean logExhausted
           
protected  boolean logHandled
           
protected  boolean logRetryAttempted
           
protected  boolean logRetryStackTrace
           
protected  boolean logStackTrace
           
protected  int maximumRedeliveries
           
protected  long maximumRedeliveryDelay
           
protected static Random randomNumberGenerator
           
protected  long redeliveryDelay
           
protected  LoggingLevel retriesExhaustedLogLevel
           
protected  LoggingLevel retryAttemptedLogLevel
           
protected  boolean useCollisionAvoidance
           
protected  boolean useExponentialBackOff
           
 
Constructor Summary
RedeliveryPolicy()
           
 
Method Summary
 RedeliveryPolicy asyncDelayedRedelivery()
          Allow asynchronous delayed redelivery.
 RedeliveryPolicy backOffMultiplier(double multiplier)
          Enables exponential backoff and sets the multiplier used to increase the delay between redeliveries
protected static long calculateRedeliverDelayUsingPattern(String delayPattern, int redeliveryCounter)
          Calculates the delay using the delay pattern
 long calculateRedeliveryDelay(long previousDelay, int redeliveryCounter)
          Calculates the new redelivery delay based on the last one
 RedeliveryPolicy collisionAvoidancePercent(double collisionAvoidancePercent)
          Enables collision avoidance and sets the percentage used
 RedeliveryPolicy copy()
           
 RedeliveryPolicy delayPattern(String delayPattern)
          Sets the delay pattern with delay intervals.
 RedeliveryPolicy disableRedelivery()
          Disables redelivery by setting maximum redeliveries to 0.
 double getBackOffMultiplier()
           
 double getCollisionAvoidanceFactor()
           
 long getCollisionAvoidancePercent()
           
 String getDelayPattern()
           
 int getMaximumRedeliveries()
           
 long getMaximumRedeliveryDelay()
           
protected static Random getRandomNumberGenerator()
           
 long getRedeliverDelay()
          Deprecated. 
 long getRedeliveryDelay()
           
 LoggingLevel getRetriesExhaustedLogLevel()
           
 LoggingLevel getRetryAttemptedLogLevel()
           
 boolean isAsyncDelayedRedelivery()
           
 boolean isLogContinued()
           
 boolean isLogExhausted()
           
 boolean isLogHandled()
           
 boolean isLogRetryAttempted()
           
 boolean isLogRetryStackTrace()
           
 boolean isLogStackTrace()
           
 boolean isUseCollisionAvoidance()
           
 boolean isUseExponentialBackOff()
           
 RedeliveryPolicy logExhausted(boolean logExhausted)
          Sets whether to log exhausted errors
 RedeliveryPolicy logHandled(boolean logHandled)
          Sets whether to log errors even if its handled
 RedeliveryPolicy logRetryAttempted(boolean logRetryAttempted)
          Sets whether to log retry attempts
 RedeliveryPolicy logRetryStackTrace(boolean logRetryStackTrace)
          Sets whether to log stacktrace for failed redelivery attempts
 RedeliveryPolicy logStackTrace(boolean logStackTrace)
          Sets whether to log stacktrace for failed messages.
 RedeliveryPolicy maximumRedeliveries(int maximumRedeliveries)
          Sets the maximum number of times a message exchange will be redelivered
 RedeliveryPolicy maximumRedeliveryDelay(long maximumRedeliveryDelay)
          Sets the maximum redelivery delay if using exponential back off.
 RedeliveryPolicy redeliverDelay(long delay)
          Deprecated. use redeliveryDelay instead
 RedeliveryPolicy redeliveryDelay(long delay)
          Sets the initial redelivery delay in milliseconds
 RedeliveryPolicy retriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel)
          Sets the logging level to use for log messages when retries have been exhausted.
 RedeliveryPolicy retryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel)
          Sets the logging level to use for log messages when retries are attempted.
 void setAsyncDelayedRedelivery(boolean asyncDelayedRedelivery)
          Sets whether asynchronous delayed redelivery is allowed.
 void setBackOffMultiplier(double backOffMultiplier)
          Sets the multiplier used to increase the delay between redeliveries if setUseExponentialBackOff(boolean) is enabled
 void setCollisionAvoidanceFactor(double collisionAvoidanceFactor)
          Sets the factor used for collision avoidance if enabled via setUseCollisionAvoidance(boolean)
 void setCollisionAvoidancePercent(double collisionAvoidancePercent)
          Sets the percentage used for collision avoidance if enabled via setUseCollisionAvoidance(boolean)
 void setDelayPattern(String delayPattern)
          Sets an optional delay pattern to use instead of fixed delay.
 void setLogContinued(boolean logContinued)
          Sets whether errors should be logged even if its continued
 void setLogExhausted(boolean logExhausted)
          Sets whether exhausted exceptions should be logged or not
 void setLogHandled(boolean logHandled)
          Sets whether errors should be logged even if its handled
 void setLogRetryAttempted(boolean logRetryAttempted)
          Sets whether retry attempts should be logged or not
 void setLogRetryStackTrace(boolean logRetryStackTrace)
          Sets whether stack traces should be logged or not
 void setLogStackTrace(boolean logStackTrace)
          Sets whether stack traces should be logged or not
 void setMaximumRedeliveries(int maximumRedeliveries)
          Sets the maximum number of times a message exchange will be redelivered.
 void setMaximumRedeliveryDelay(long maximumRedeliveryDelay)
          Sets the maximum redelivery delay.
 void setRedeliverDelay(long redeliveryDelay)
          Deprecated. 
 void setRedeliveryDelay(long redeliverDelay)
          Sets the initial redelivery delay in milliseconds
 void setRetriesExhaustedLogLevel(LoggingLevel retriesExhaustedLogLevel)
          Sets the logging level to use for log messages when retries have been exhausted.
 void setRetryAttemptedLogLevel(LoggingLevel retryAttemptedLogLevel)
          Sets the logging level to use for log messages when retries are attempted.
 void setUseCollisionAvoidance(boolean useCollisionAvoidance)
          Enables/disables collision avoidance which adds some randomization to the backoff timings to reduce contention probability
 void setUseExponentialBackOff(boolean useExponentialBackOff)
          Enables/disables exponential backoff using the getBackOffMultiplier() to increase the time between retries
 boolean shouldRedeliver(Exchange exchange, int redeliveryCounter, Predicate retryWhile)
          Returns true if the policy decides that the message exchange should be redelivered.
 void sleep(long redeliveryDelay)
          Sleeps for the given delay
 long sleep(long redeliveryDelay, int redeliveryCounter)
          Calculates the new redelivery delay based on the last one and then sleeps for the necessary amount of time.
 String toString()
           
 RedeliveryPolicy useCollisionAvoidance()
          Enables collision avoidance which adds some randomization to the backoff timings to reduce contention probability
 RedeliveryPolicy useExponentialBackOff()
          Enables exponential backoff using the getBackOffMultiplier() to increase the time between retries
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

randomNumberGenerator

protected static transient 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 LoggingLevel retriesExhaustedLogLevel

retryAttemptedLogLevel

protected LoggingLevel retryAttemptedLogLevel

logStackTrace

protected boolean logStackTrace

logRetryStackTrace

protected boolean logRetryStackTrace

logHandled

protected boolean logHandled

logContinued

protected boolean logContinued

logExhausted

protected boolean logExhausted

logRetryAttempted

protected boolean logRetryAttempted

delayPattern

protected String delayPattern

asyncDelayedRedelivery

protected boolean asyncDelayedRedelivery
Constructor Detail

RedeliveryPolicy

public RedeliveryPolicy()
Method Detail

toString

public String toString()
Overrides:
toString in class Object

copy

public RedeliveryPolicy copy()

shouldRedeliver

public boolean shouldRedeliver(Exchange exchange,
                               int redeliveryCounter,
                               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


redeliverDelay

@Deprecated
public RedeliveryPolicy redeliverDelay(long delay)
Deprecated. use redeliveryDelay instead

Sets the initial redelivery delay in milliseconds


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(LoggingLevel retriesExhaustedLogLevel)
Sets the logging level to use for log messages when retries have been exhausted.


retryAttemptedLogLevel

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


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


logExhausted

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


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:
setAsyncDelayedRedelivery(boolean)

getRedeliverDelay

@Deprecated
public long getRedeliverDelay()
Deprecated. 


setRedeliverDelay

@Deprecated
public void setRedeliverDelay(long redeliveryDelay)
Deprecated. 


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(LoggingLevel retriesExhaustedLogLevel)
Sets the logging level to use for log messages when retries have been exhausted.


getRetriesExhaustedLogLevel

public LoggingLevel getRetriesExhaustedLogLevel()

setRetryAttemptedLogLevel

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


getRetryAttemptedLogLevel

public LoggingLevel getRetryAttemptedLogLevel()

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


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


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


Apache CAMEL