You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

Dead Letter Channel

Camel supports the Dead Letter Channel from the EIP patterns using the DeadLetterChannel processor which is an Error Handler.

Redelivery

It is common for a temporary outage or database deadlock to cause a message to fail to process; but the chances are if its tried a few more times with some time delay then it will complete fine. So we typically wish to use some kind of redelivery policy to decide how many times to try redeliver a message and how long to wait before redelivery attempts.

The RedeliveryPolicy defines how the message is to be redelivered. You can customize things like

  • how many times a message is attempted to be redelivered before it is considered a failure and sent to the dead letter channel
  • the initial redelivery timeout
  • whether or not exponential backoff is used (i.e. the time between retries increases using a backoff multiplier)
  • whether to use collision avoidance to add some randomness to the timings

Once all attempts at redelivering the message fails then the message is forwarded to the dead letter queue.

Redelivery default values

The default redeliver policy will use the following values:

  • maximumRedeliveries=6
  • initialRedeliveryDelay=1000L (1 second)
  • And the backoff and collision avoidance is turned off.

Redelivery header

When a message is redelivered the DeadLetterChannel will append a customizable header to the message to indicate how many times its been redelivered. The default value is org.apache.camel.redeliveryCount.

Configuring via the DSL

The following example shows how to configure the Dead Letter Channel configuration using the DSL

Error formatting macro: snippet: java.lang.NullPointerException

You can also configure the RedeliveryPolicy as this example shows

Error formatting macro: snippet: java.lang.NullPointerException

ExceptionPolicyStrategy

ExceptionPolicyStrategy is a strategy for resolving witch rule (ExceptionType) should handle the given thrown exception.

DeadLetterChannel supports pluggable strategies for resolving how exceptions should be handled. It is common to how different strategies for different types of exceptions. For instance network and IO related exceptions is more prone for network outages so the redeliver policy could have a higher attempts, timeouts etc. Where as a NullPointerException is typically a programming error so these kind of exception is severe and should not be redelivered. Camel uses a default strategy DefaultExceptionPolicyStrategy that applies the following rules:

  • The exception type must be configured with an Exception that is an instance of the thrown exception
  • If the exception type has exactly the thrown exception then its selected
  • Otherwise the type that has an exception that is super of the thrown exception is selected (recurring up the exception hierarchy)

The example below illustrates a common exception handling configuration in Camel:

Error formatting macro: snippet: java.lang.NullPointerException

Here we have configured the handling of exceptions into three categories:

  • NullPointerException (for special handling of these hard to track down bugs)
  • IOException (for IO and network related issues we can attempt many times)
  • Exception (fallback exception handling for all other kind of exceptions)

Camel will with the default strategy try to select the best suited category from above for any thrown exception.
So if a java.net.ScoketException is thrown then the IOException category will handle it. If a NumberFormatException or CamelExchangeException is thrown it is handled by the general purpose Exception category.

Camel supports pluggable exception policy strategies. See Error Handler for such details.

Using This Pattern

If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.

  • No labels