camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Dead Letter Channel
Date Mon, 24 Oct 2011 12:50:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/Dead+Letter+Channel">Dead
Letter Channel</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >The [Dead Letter Channel] above will
clear the caused exception ({{setException(null)}}), by moving the caused exception to a property
on the [Exchange], with the key {{Exchange.EXCEPTION_CAUGHT}}. Then the [Exchange] is moved
to the {{&quot;jms:queue:dead&quot;}} destination and the client will not notice the
failure.  <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">h4.
How to let the client notice the error? <br>If you want to move the message to the dead
letter queue *and* also let the client notice the error, then you can configure the [Dead
Letter Channel] to *not* handle the error. For example: <br>{code:java} <br>errorHandler(deadLetterChannel(&quot;jms:queue:dead&quot;)
<br>    .maximumRedeliveries(3).redeliverDealy(5000).handled(false)); <br>{code}
<br> <br>When all attempts of redelivery have failed the [Exchange] is moved to
the dead letter queue (the dead letter endpoint). As the [Dead Letter Channel]  <br>is
configured to *not* handle it, it will mark the [Exchange] as failed so the client will be
notified of this error. <br> <br>{tip:title=Handled} <br>See also [Exception
Clause] for more details on the *handled* policy as this feature was first introduced here
and thus we have more docuemntation and samples there. <br>{tip} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. About moving Exchange to dead
letter queue and using the original message <br>*Available as of Camel 2.0* <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="DeadLetterChannel-DeadLetterChannel"></a>Dead Letter
Channel</h2>

<p>Camel supports the <a href="http://www.enterpriseintegrationpatterns.com/DeadLetterChannel.html"
class="external-link" rel="nofollow">Dead Letter Channel</a> from the <a href="/confluence/display/CAMEL/Enterprise+Integration+Patterns"
title="Enterprise Integration Patterns">EIP patterns</a> using the <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/processor/DeadLetterChannel.html"
class="external-link" rel="nofollow">DeadLetterChannel</a> processor which is an
<a href="/confluence/display/CAMEL/Error+Handler" title="Error Handler">Error Handler</a>.
</p>

<p><span class="image-wrap" style=""><img src="http://www.enterpriseintegrationpatterns.com/img/DeadLetterChannelSolution.gif"
style="border: 0px solid black" /></span></p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Difference between Dead
Letter Channel and Default Error Handler</b><br />The major difference is that
<a href="/confluence/display/CAMEL/Dead+Letter+Channel" title="Dead Letter Channel">Dead
Letter Channel</a> has a dead letter queue that whenever an <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> could not be processed is moved to. It will <b>always</b>
moved failed exchanges to this queue. 

<p>Unlike the <a href="/confluence/display/CAMEL/DefaultErrorHandler" title="DefaultErrorHandler">Default
Error Handler</a> that does <b>not</b> have a dead letter queue. So whenever
an <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
could not be processed the error is propagated back to the client.</p>

<p><b>Notice:</b> You can adjust this behavior of whether the client should
be notified or not with the <b>handled</b> option.</p></td></tr></table></div>

<h3><a name="DeadLetterChannel-Redelivery"></a>Redelivery </h3>

<p>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.</p>

<p>The <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/processor/RedeliveryPolicy.html"
class="external-link" rel="nofollow">RedeliveryPolicy</a> defines how the message
is to be redelivered. You can customize things like</p>

<ul>
	<li>how many times a message is attempted to be redelivered before it is considered
a failure and sent to the dead letter channel</li>
	<li>the initial redelivery timeout</li>
	<li>whether or not exponential backoff is used (i.e. the time between retries increases
using a backoff multiplier)</li>
	<li>whether to use collision avoidance to add some randomness to the timings</li>
	<li>delay pattern a new option in Camel 2.0, see below for details.</li>
</ul>


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

<h3><a name="DeadLetterChannel-AboutmovingExchangetodeadletterqueueandusinghandled"></a>About
moving Exchange to dead letter queue and using handled</h3>
<p><b>Handled</b> on <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> was introduced in Camel 2.0,
this feature does not exist in Camel 1.x</p>

<p>When all attempts of redelivery have failed the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> is moved to the dead letter queue (the dead letter
endpoint). The exchange is then complete and from the client point of view it was processed.
As such the <a href="/confluence/display/CAMEL/Dead+Letter+Channel" title="Dead Letter
Channel">Dead Letter Channel</a> have handled the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>. </p>

<p>For instance configuring the dead letter channel as:</p>

<p><b>Using the <a href="/confluence/display/CAMEL/Fluent+Builders" title="Fluent
Builders">Fluent Builders</a></b></p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
errorHandler(deadLetterChannel(<span class="code-quote">"jms:queue:dead"</span>)
    .maximumRedeliveries(3).redeliveryDelay(5000));
</pre>
</div></div>

<p><b>Using the <a href="/confluence/display/CAMEL/Spring+XML+Extensions" title="Spring
XML Extensions">Spring XML Extensions</a></b></p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;route errorHandlerRef=<span class="code-quote">"myDeadLetterErrorHandler"</span>&gt;</span>
   ...
<span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"myDeadLetterErrorHandler"</span>
class=<span class="code-quote">"org.apache.camel.builder.DeadLetterChannelBuilder"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"deadLetterUri"</span>
value=<span class="code-quote">"jms:queue:dead"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"redeliveryPolicy"</span>
ref=<span class="code-quote">"myRedeliveryPolicyConfig"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"myRedeliveryPolicyConfig"</span>
class=<span class="code-quote">"org.apache.camel.processor.RedeliveryPolicy"</span>&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"maximumRedeliveries"</span>
value=<span class="code-quote">"3"</span>/&gt;</span>
    <span class="code-tag">&lt;property name=<span class="code-quote">"redeliveryDelay"</span>
value=<span class="code-quote">"5000"</span>/&gt;</span>
<span class="code-tag">&lt;/bean&gt;</span>

</pre>
</div></div>

<p>The <a href="/confluence/display/CAMEL/Dead+Letter+Channel" title="Dead Letter
Channel">Dead Letter Channel</a> above will clear the caused exception (<tt>setException(null)</tt>),
by moving the caused exception to a property on the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a>, with the key <tt>Exchange.EXCEPTION_CAUGHT</tt>.
Then the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
is moved to the <tt>"jms:queue:dead"</tt> destination and the client will not
notice the failure. </p>

<h3><a name="DeadLetterChannel-AboutmovingExchangetodeadletterqueueandusingtheoriginalmessage"></a>About
moving Exchange to dead letter queue and using the original message</h3>
<p><b>Available as of Camel 2.0</b><br/>
The option <b>useOriginalMessage</b> is used for routing the original input message
instead of the current message that potentially is modified during routing.</p>

<p>For instance if you have this route:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
   from(<span class="code-quote">"jms:queue:order:input"</span>)
       .to(<span class="code-quote">"bean:validateOrder"</span>)
       .to(<span class="code-quote">"bean:transformOrder"</span>)
       .to(<span class="code-quote">"bean:handleOrder"</span>);
</pre>
</div></div>
<p>The route listen for JMS messages and validates, transforms and handle it. During
this the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
payload is transformed/modified. So in case something goes wrong and we want to move the message
to another JMS destination, then we can configure our <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> with the <b>useOriginalBody</b>
option. But when we move the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
to this destination we do not know in which state the message is in. Did the error happen
in before the transformOrder or after? So to be sure we want to move the original input message
we received from <tt>jms:queue:order:input</tt>. So we can do this by enabling
the <b>useOriginalMessage</b> option as shown below:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
    <span class="code-comment">// will use original body
</span>    errorHandler(deadLetterChannel(<span class="code-quote">"jms:queue:dead"</span>)
       .useOriginalMessage().mamimumRedeliveries(5).redeliverDelay(5000);
</pre>
</div></div>

<p>Then the messages routed to the <tt>jms:queue:dead</tt> is the original
input. If we want to manually retry we can move the JMS message from the failed to the input
queue, with no problem as the message is the same as the original we received.</p>


<h3><a name="DeadLetterChannel-OnRedelivery"></a>OnRedelivery</h3>
<p><b>Available in Camel 1.6.0 onwards</b></p>

<p>When <a href="/confluence/display/CAMEL/Dead+Letter+Channel" title="Dead Letter
Channel">Dead Letter Channel</a> is doing redeliver its possible to configure a <a
href="/confluence/display/CAMEL/Processor" title="Processor">Processor</a> that is
executed just <b>before</b> every redelivery attempt. This can be used for the
situations where you need to alter the message before its redelivered. See below for sample.
</p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>onException and onRedeliver</b><br
/>In Camel 2.0 we also added support for per <a href="/confluence/display/CAMEL/Exception+Clause"
title="Exception Clause"><b>onException</b></a> to set a <b>onRedeliver</b>.
That means you can do special on redelivery for different exceptions, as opposed to onRedelivery
set on <a href="/confluence/display/CAMEL/Dead+Letter+Channel" title="Dead Letter Channel">Dead
Letter Channel</a> can be viewed as a global scope.</td></tr></table></div>


<h3><a name="DeadLetterChannel-Redeliverydefaultvalues"></a>Redelivery default
values</h3>
<p>In <b>Camel 2.0</b> redelivery is disabled by default, as opposed to
Camel 1.x in which <a href="/confluence/display/CAMEL/Dead+Letter+Channel" title="Dead
Letter Channel">Dead Letter Channel</a> is configured with <tt>maximumRedeliveries=5</tt>.</p>

<p>The default redeliver policy will use the following values:</p>
<ul>
	<li>maximumRedeliveries=0 (in Camel 1.x the default value is 5)</li>
	<li>redeliverDelay=1000L (1 second, <b>new as of Camel 2.0</b>)
	<ul>
		<li>use initialRedeliveryDelay for previous versions</li>
	</ul>
	</li>
	<li>maximumRedeliveryDelay = 60 * 1000L (60 seconds)</li>
	<li>And the exponential backoff and collision avoidance is turned off.</li>
	<li>The retriesExhaustedLogLevel are set to LoggingLevel.ERROR</li>
	<li>The retryAttemptedLogLevel are set to LoggingLevel.DEBUG</li>
	<li>Stack traces is logged for exhausted messages from Camel 2.2 onwards.</li>
	<li>Handled exceptions is not logged from Camel 2.3 onwards</li>
</ul>


<p>The maximum redeliver delay ensures that a delay is never longer than the value,
default 1 minute. This can happen if you turn on the exponential backoff.</p>

<p>The maximum redeliveries is the number of <b>re</b> delivery attempts.
By default Camel will try to process the exchange 1 + 5 times. 1 time for the normal attempt
and then 5 attempts as redeliveries.<br/>
Setting the maximumRedeliveries to a negative value such as -1 will then always redelivery
(unlimited).<br/>
Setting the maximumRedeliveries to 0 will disable any re delivery attempt.</p>

<p>Camel will log delivery failures at the DEBUG logging level by default. You can change
this by specifying retriesExhaustedLogLevel and/or retryAttemptedLogLevel. See <a href="http://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/ExceptionBuilderWithRetryLoggingLevelSetTest.java"
class="external-link" rel="nofollow">ExceptionBuilderWithRetryLoggingLevelSetTest</a>
for an example. </p>

<p>In Camel 2.0 you can turn logging of stack traces on/off. If turned off Camel will
still log the redelivery attempt. Its just much less verbose.</p>

<h4><a name="DeadLetterChannel-RedeliverDelayPattern"></a>Redeliver Delay
Pattern</h4>
<p><b>Available as of Camel 2.0</b><br/>
Delay pattern is used as a single option to set a range pattern for delays. If used then the
following options does not apply: (delay, backOffMultiplier, useExponentialBackOff, useCollisionAvoidance,
maximumRedeliveryDelay).</p>

<p>The idea is to set groups of ranges using the following syntax: <tt>limit:delay;limit
2:delay 2;limit 3:delay 3;...;limit N:delay N</tt></p>

<p>Each group has two values separated with colon</p>
<ul class="alternate" type="square">
	<li>limit = upper limit</li>
	<li>delay = delay in millis<br/>
And the groups is again separated with semi colon.<br/>
The rule of thumb is that the next groups should have a higher limit than the previous group.</li>
</ul>


<p>Lets clarify this with an example:<br/>
<tt>delayPattern=5:1000;10:5000;20:20000</tt></p>

<p>That gives us 3 groups:</p>
<ul class="alternate" type="square">
	<li>5:1000</li>
	<li>10:5000</li>
	<li>20:20000</li>
</ul>


<p>Resulting in these delays for redelivery attempt:</p>
<ul class="alternate" type="square">
	<li>Redelivery attempt number 1..4 = 0 millis (as the first group start with 5)</li>
	<li>Redelivery attempt number 5..9 = 1000 millis (the first group)</li>
	<li>Redelivery attempt number 10..19 = 5000 millis (the second group)</li>
	<li>Redelivery attempt number 20.. = 20000 millis (the last group)</li>
</ul>


<p>Note: The first redelivery attempt is 1, so the first group should start with 1 or
higher.</p>

<p>You can start a group with limit 1 to eg have a starting delay: <tt>delayPattern=1:1000;5:5000</tt></p>
<ul class="alternate" type="square">
	<li>Redelivery attempt number 1..4 = 1000 millis (the first group)</li>
	<li>Redelivery attempt number 5.. = 5000 millis (the last group)</li>
</ul>


<p>There is no requirement that the next delay should be higher than the previous. You
can use any delay value you like. For example with <tt>delayPattern=1:5000;3:1000</tt>
we start with 5 sec delay and then later reduce that to 1 second.</p>

<h3><a name="DeadLetterChannel-Redeliveryheader"></a>Redelivery header</h3>

<p>When a message is redelivered the <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/processor/DeadLetterChannel.html"
class="external-link" rel="nofollow">DeadLetterChannel</a> will append a customizable
header to the message to indicate how many times its been redelivered. <br/>
In Camel 1.x: The header is <b>org.apache.camel.redeliveryCount</b>.<br/>
In Camel 2.0: The header is <b>CamelRedeliveryCounter</b>, which is also defined
on the <tt>Exchange.REDELIVERY_COUNTER</tt>.<br/>
In Camel 2.6: The header <b>CamelRedeliveryMaxCounter</b>, which is also defined
on the <tt>Exchange.REDELIVERY_MAX_COUNTER</tt>, contains the maximum redelivery
setting. This header is absent if you use <tt>retryWhile</tt> or have unlimited
maximum redelivery configured.</p>

<p>And a boolean flag whether it is being redelivered or not (first attempt)<br/>
In Camel 1.x: The header <b>org.apache.camel.Redelivered</b> contains a boolean
if the message is redelivered or not.<br/>
In Camel 2.0: The header <b>CamelRedelivered</b> contains a boolean if the message
is redelivered or not, which is also defined on the <tt>Exchange.REDELIVERED</tt>.</p>

<p>Dynamically calculated delay from the exchange<br/>
In Camel 2.9 and 2.8.2: The header is <b>CamelRedeliveryDelay</b>, which is also
defined on the <tt>Exchange.REDELIVERY_DELAY</tt>.<br/>
Is this header is absent, normal redelivery rules apply.</p>

<h4><a name="DeadLetterChannel-Whichendpointfailed"></a>Which endpoint failed</h4>
<p><b>Available as of Camel 2.1</b></p>

<p>When Camel routes messages it will decorate the <a href="/confluence/display/CAMEL/Exchange"
title="Exchange">Exchange</a> with a property that contains the <b>last</b>
endpoint Camel send the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
to:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-object">String</span> lastEndpointUri = exchange.getProperty(Exchange.TO_ENDPOINT,
<span class="code-object">String</span>.class);
</pre>
</div></div>
<p>The <tt>Exchange.TO_ENDPOINT</tt> have the constant value <tt>CamelToEndpoint</tt>.</p>

<p>This information is updated when Camel sends a message to any endpoint. So if it
exists its the <b>last</b> endpoint which Camel send the Exchange to.</p>

<p>When for example processing the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
at a given <a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a>
and the message is to be moved into the dead letter queue, then Camel also decorates the Exchange
with another property that contains that <b>last</b> endpoint:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
<span class="code-object">String</span> failedEndpointUri = exchange.getProperty(Exchange.FAILURE_ENDPOINT,
<span class="code-object">String</span>.class);
</pre>
</div></div>
<p>The <tt>Exchange.FAILURE_ENDPOINT</tt> have the constant value <tt>CamelFailureEndpoint</tt>.</p>

<p>This allows for example you to fetch this information in your dead letter queue and
use that for error reporting.<br/>
This is useable if the Camel route is a bit dynamic such as the dynamic <a href="/confluence/display/CAMEL/Recipient+List"
title="Recipient List">Recipient List</a> so you know which endpoints failed.</p>

<p><b>Notice:</b> These information is kept on the Exchange even if the
message was successfully processed by a given endpoint, and then later fails for example in
a local <a href="/confluence/display/CAMEL/Bean" title="Bean">Bean</a> processing
instead. So beware that this is a hint that helps pinpoint errors.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:queue:foo"</span>)
    .to(<span class="code-quote">"http:<span class="code-comment">//someserver/somepath"</span>)
</span>    .beanRef(<span class="code-quote">"foo"</span>);
</pre>
</div></div>

<p>Now suppose the route above and a failure happens in the <tt>foo</tt>
bean. Then the <tt>Exchange.TO_ENDPOINT</tt> and <tt>Exchange.FAILURE_ENDPOINT</tt>
will still contain the value of <tt><a href="http://someserver/somepath" class="external-link"
rel="nofollow">http://someserver/somepath</a></tt>.</p>


<h3><a name="DeadLetterChannel-Samples"></a>Samples</h3>
<p>The following example shows how to configure the Dead Letter Channel configuration
using the <a href="/confluence/display/CAMEL/DSL" title="DSL">DSL</a></p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">RouteBuilder builder = <span class="code-keyword">new</span>
RouteBuilder() {
    <span class="code-keyword">public</span> void configure() {
        <span class="code-comment">// using dead letter channel with a seda queue <span
class="code-keyword">for</span> errors
</span>        errorHandler(deadLetterChannel(<span class="code-quote">"seda:errors"</span>));

        <span class="code-comment">// here is our route
</span>        from(<span class="code-quote">"seda:a"</span>).to(<span
class="code-quote">"seda:b"</span>);
    }
};
</pre>
</div></div>

<p>You can also configure the <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/processor/RedeliveryPolicy.html"
class="external-link" rel="nofollow">RedeliveryPolicy</a> as this example shows</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">RouteBuilder builder = <span class="code-keyword">new</span>
RouteBuilder() {
    <span class="code-keyword">public</span> void configure() {
        <span class="code-comment">// configures dead letter channel to use seda queue
<span class="code-keyword">for</span> errors and use at most 2 redelveries
</span>        <span class="code-comment">// and exponential backoff
</span>        errorHandler(deadLetterChannel(<span class="code-quote">"seda:errors"</span>).maximumRedeliveries(2).useExponentialBackOff());

        <span class="code-comment">// here is our route
</span>        from(<span class="code-quote">"seda:a"</span>).to(<span
class="code-quote">"seda:b"</span>);
    }
};
</pre>
</div></div>

<h3><a name="DeadLetterChannel-HowcanImodifytheExchangebeforeredelivery%3F"></a>How
can I modify the Exchange before redelivery?</h3>
<p>In <b>Camel 1.6.0</b> we added support directly in <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> to set a <a href="/confluence/display/CAMEL/Processor"
title="Processor">Processor</a> that is executed <b>before</b> each redelivery
attempt. </p>

<p>When <a href="/confluence/display/CAMEL/Dead+Letter+Channel" title="Dead Letter
Channel">Dead Letter Channel</a> is doing redeliver its possible to configure a <a
href="/confluence/display/CAMEL/Processor" title="Processor">Processor</a> that is
executed just <b>before</b> every redelivery attempt. This can be used for the
situations where you need to alter the message before its redelivered.  </p>

<p>Here we configure the <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> to use our processor <tt>MyRedeliveryProcessor</tt>
to be executed before each redelivery.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// we configure our Dead Letter
Channel to invoke
</span><span class="code-comment">// MyRedeliveryProcessor before a redelivery
is
</span><span class="code-comment">// attempted. This allows us to alter the message
before
</span>errorHandler(deadLetterChannel(<span class="code-quote">"mock:error"</span>).maximumRedeliveries(5)
        .onRedelivery(<span class="code-keyword">new</span> MyRedeliverProcessor())
        <span class="code-comment">// setting delay to zero is just to make unit testing
faster
</span>        .redeliveryDelay(0L));
</pre>
</div></div>

<p>And this is the processor <tt>MyRedeliveryProcessor</tt> where we alter
the message. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java"><span class="code-comment">// This is our processor that
is executed before every redelivery attempt
</span><span class="code-comment">// here we can <span class="code-keyword">do</span>
what we want in the java code, such as altering the message
</span><span class="code-keyword">public</span> class MyRedeliverProcessor
<span class="code-keyword">implements</span> Processor {

    <span class="code-keyword">public</span> void process(Exchange exchange) <span
class="code-keyword">throws</span> Exception {
        <span class="code-comment">// the message is being redelivered so we can alter
it
</span>
        <span class="code-comment">// we just append the redelivery counter to the body
</span>        <span class="code-comment">// you can of course <span class="code-keyword">do</span>
all kind of stuff instead
</span>        <span class="code-object">String</span> body = exchange.getIn().getBody(<span
class="code-object">String</span>.class);
        <span class="code-object">int</span> count = exchange.getIn().getHeader(Exchange.REDELIVERY_COUNTER,
<span class="code-object">Integer</span>.class);

        exchange.getIn().setBody(body + count);

        <span class="code-comment">// the maximum redelivery was set to 5
</span>        <span class="code-object">int</span> max = exchange.getIn().getHeader(Exchange.REDELIVERY_MAX_COUNTER,
<span class="code-object">Integer</span>.class);
        assertEquals(5, max);
    }
}
</pre>
</div></div>

<h4><a name="DeadLetterChannel-UsingThisPattern"></a>Using This Pattern</h4>

<p>If you would like to use this EIP Pattern then please read the <a href="/confluence/display/CAMEL/Getting+Started"
title="Getting Started">Getting Started</a>, you may also find the <a href="/confluence/display/CAMEL/Architecture"
title="Architecture">Architecture</a> useful particularly the description of <a
href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a> and <a
href="/confluence/display/CAMEL/URIs" title="URIs">URIs</a>. Then you could try out
some of the <a href="/confluence/display/CAMEL/Examples" title="Examples">Examples</a>
first before trying this pattern out.</p>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Error+Handler" title="Error Handler">Error
Handler</a></li>
	<li><a href="/confluence/display/CAMEL/Exception+Clause" title="Exception Clause">Exception
Clause</a></li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Dead+Letter+Channel">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=49482&revisedVersion=47&originalVersion=46">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Dead+Letter+Channel?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message