camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Error Handler
Date Wed, 20 Oct 2010 14:18:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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/Error+Handler">Error
Handler</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (6)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-unchanged" >h2. Error Handler <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >Camel supports pluggable <span
class="diff-changed-words">[ErrorHandler|http://camel.apache.org/maven/camel-<span class="diff-added-chars"style="background-color:
#dfd;">2.2.0/</span>c<span class="diff-added-chars"style="background-color: #dfd;">amel-c</span>ore/apidocs/org/apache/camel/processor/ErrorHandler.html]</span>
strategies to deal with errors processing an [Event Driven Consumer]. An alternative is to
specify the error handling directly in the [DSL] using the [Exception Clause]. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>For introduction and background
material see [Error handling in Camel]. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>{tip:title=Exception Clause}
<br></td></tr>
            <tr><td class="diff-changed-lines" >Using [Error Handler] combined
with [Exception Clause] is a very powerful <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">ally.</span>
<span class="diff-added-words"style="background-color: #dfd;">combination.</span>
We encourage end-users to use this combination in your error handling strategies. See samples
and [Exception Clause]. <br></td></tr>
            <tr><td class="diff-unchanged" >{tip}  <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >* [DefaultErrorHandler] is the default
error handler in *Camel 2.0 onwards*. This error handler does not support a deal letter queue,
it will propagate exceptions back to the caller, as if there where no error handler at all.
It has a limited set of features. <br>* [Dead Letter Channel] which supports attempting
to redeliver the message exchange a number of times before sending it to a dead letter endpoint
<br></td></tr>
            <tr><td class="diff-changed-lines" >* <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">[LoggingErrorHandler|http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/processor/LoggingErrorHandler.html]</span>
<span class="diff-added-words"style="background-color: #dfd;">LoggingErrorHandler</span>
for just catching and logging exceptions <br></td></tr>
            <tr><td class="diff-changed-lines" >* <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">[NoErrorHandler|http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/builder/NoErrorHandlerBuilder.html]</span>
<span class="diff-added-words"style="background-color: #dfd;">NoErrorHandler</span>
for no error handling <br></td></tr>
            <tr><td class="diff-unchanged" > <br>h4. Transacted <br></td></tr>
            <tr><td class="diff-changed-lines" >* [TransactionErrorHandler] is
the default error handler in *Camel 2.0 onwards* for transacted routes. <span class="diff-added-words"style="background-color:
#dfd;">See the [Transactional Client] EIP pattern.</span> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">*
[Transaction|http://camel.apache.org/maven/camel-spring/apidocs/org/apache/camel/spring/spi/TransactionErrorHandlerBuilder.html]
for transactional error handling (Spring based). See the [Transactional Client] EIP pattern.
 <br></td></tr>
            <tr><td class="diff-unchanged" > <br>These error handlers can
be applied in the [DSL] to an entire set of rules or a specific routing rule as we show in
the next examples. Error handling rules are inherited on each routing rule within a single
[RouteBuilder] <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="ErrorHandler-ErrorHandler"></a>Error Handler</h2>

<p>Camel supports pluggable <a href="http://camel.apache.org/maven/camel-2.2.0/camel-core/apidocs/org/apache/camel/processor/ErrorHandler.html"
class="external-link" rel="nofollow">ErrorHandler</a> strategies to deal with errors
processing an <a href="/confluence/display/CAMEL/Event+Driven+Consumer" title="Event Driven
Consumer">Event Driven Consumer</a>. An alternative is to specify the error handling
directly in the <a href="/confluence/display/CAMEL/DSL" title="DSL">DSL</a> using
the <a href="/confluence/display/CAMEL/Exception+Clause" title="Exception Clause">Exception
Clause</a>.</p>

<p>For introduction and background material see <a href="/confluence/display/CAMEL/Error+handling+in+Camel"
title="Error handling in Camel">Error handling in Camel</a>.</p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Camel 2.0
has a new default error handler</b><br />In Camel 2.0 onwards there default error
handler is changed from <a href="/confluence/display/CAMEL/Dead+Letter+Channel" title="Dead
Letter Channel">Dead Letter Channel</a> to <a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a>. This error handler does not
support a dead letter queue and will return exceptions back to the caller. This is what you
expects when working with regular Java that exceptions will be thrown back to the caller.</td></tr></table></div>


<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>Exception Clause</b><br
/>Using <a href="/confluence/display/CAMEL/Error+Handler" title="Error Handler">Error
Handler</a> combined with <a href="/confluence/display/CAMEL/Exception+Clause" title="Exception
Clause">Exception Clause</a> is a very powerful combination. We encourage end-users
to use this combination in your error handling strategies. See samples and <a href="/confluence/display/CAMEL/Exception+Clause"
title="Exception Clause">Exception Clause</a>.</td></tr></table></div>


<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>Using try ... catch ...
finally</b><br />Related to error handling is the <a href="/confluence/display/CAMEL/Try+Catch+Finally"
title="Try Catch Finally">Try Catch Finally</a> as DSL you can use directly in your
route. Its basically a mimic of the regular try catch finally in the Java language but with
more power.</td></tr></table></div>

<p>The current implementations Camel provides out of the box are:</p>

<h4><a name="ErrorHandler-Nontransacted"></a>Non transacted</h4>
<ul>
	<li><a href="/confluence/display/CAMEL/DefaultErrorHandler" title="DefaultErrorHandler">DefaultErrorHandler</a>
is the default error handler in <b>Camel 2.0 onwards</b>. This error handler does
not support a deal letter queue, it will propagate exceptions back to the caller, as if there
where no error handler at all. It has a limited set of features.</li>
	<li><a href="/confluence/display/CAMEL/Dead+Letter+Channel" title="Dead Letter Channel">Dead
Letter Channel</a> which supports attempting to redeliver the message exchange a number
of times before sending it to a dead letter endpoint</li>
	<li>LoggingErrorHandler for just catching and logging exceptions</li>
	<li>NoErrorHandler for no error handling</li>
</ul>


<h4><a name="ErrorHandler-Transacted"></a>Transacted</h4>
<ul>
	<li><a href="/confluence/display/CAMEL/TransactionErrorHandler" title="TransactionErrorHandler">TransactionErrorHandler</a>
is the default error handler in <b>Camel 2.0 onwards</b> for transacted routes.
See the <a href="/confluence/display/CAMEL/Transactional+Client" title="Transactional Client">Transactional
Client</a> EIP pattern.</li>
</ul>


<p>These error handlers can be applied in the <a href="/confluence/display/CAMEL/DSL"
title="DSL">DSL</a> to an entire set of rules or a specific routing rule as we show
in the next examples. Error handling rules are inherited on each routing rule within a single
<a href="/confluence/display/CAMEL/RouteBuilder" title="RouteBuilder">RouteBuilder</a></p>

<div class='panelMacro'><table class='noteMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/warning.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Transaction in Camel
1.x</b><br />If the route is transactional then the <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> is disabled. The <tt>exchange.isTransacted()</tt>
is used to determine if an Exchange is transacted or not.<br/>
So if you are using transacted routes then you should configure the TransactionErrorHandler
instread of DeadLetterChannel. See <a href="/confluence/display/CAMEL/Transactional+Client"
title="Transactional Client">Transactional Client</a> for further details and samples.</td></tr></table></div>


<h3><a name="ErrorHandler-ShortSummaryoftheprovidedErrorHandlers"></a>Short
Summary of the provided Error Handlers</h3>

<h4><a name="ErrorHandler-DefaultErrorHandlernewinCamel2.0"></a><a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a> new in Camel 2.0</h4>

<p>The <a href="/confluence/display/CAMEL/DefaultErrorHandler" title="DefaultErrorHandler">DefaultErrorHandler</a>
is the new default error handler in Camel 2.0. Unlike <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> it does not have any dead letter
queue, and do <b>not</b> handle exceptions by default.</p>

<h4><a name="ErrorHandler-DeadLetterChannel"></a><a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a></h4>

<p>The <a href="/confluence/display/CAMEL/Dead+Letter+Channel" title="Dead Letter
Channel">Dead Letter Channel</a> is the default error handler in Camel 1.x, which
is automatically configured for you. By default Camel will redeliver at most 6 times using
1 second delay, and if the exchange failed it will be logged at ERROR level.</p>

<p>You can configure the default dead letter endpoint to use:</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>

<h4><a name="ErrorHandler-LoggingErrorHandler"></a>Logging Error Handler</h4>

<p>The logging error handler will log (by default at ERROR level) whenever an uncaught
exception is thrown. The logging category, logger and level may all be defined in the builder.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
errorHandler(loggingErrorHandler(<span class="code-quote">"mylogger.name"</span>).level(LoggingLevel.INFO));
</pre>
</div></div>

<p>This would create an error handler which logs exceptions using the category mylogger.name
and uses the level INFO for all log messages created.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"seda:a"</span>).errorHandler(loggingErrorHandler(<span
class="code-quote">"mylogger.name"</span>).level(LoggingLevel.DEBUG)).to(<span
class="code-quote">"seda:b"</span>);
</pre>
</div></div>

<p>Loggers may also be defined for specific routes.</p>

<h4><a name="ErrorHandler-NoErrorHandler"></a>No Error Handler</h4>

<p>The no error handler is to be used for disabling error handling. </p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
errorHandler(noErrorHandler());
</pre>
</div></div>

<h4><a name="ErrorHandler-TransactionErrorHandlernewinCamel2.0"></a><a
href="/confluence/display/CAMEL/TransactionErrorHandler" title="TransactionErrorHandler">TransactionErrorHandler</a>
new in Camel 2.0</h4>

<p>The <a href="/confluence/display/CAMEL/TransactionErrorHandler" title="TransactionErrorHandler">TransactionErrorHandler</a>
is the new default error handler in Camel 2.0 for transacted routes. </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>If you have marked a route as
transacted using the <b>transacted</b> DSL then Camel will automatic use a <a
href="/confluence/display/CAMEL/TransactionErrorHandler" title="TransactionErrorHandler">TransactionErrorHandler</a>.
It will try to lookup the global/per route configured error handler and use it if its a <tt>TransactionErrorHandlerBuilder</tt>
instance. If not Camel will automatic create a temporary <a href="/confluence/display/CAMEL/TransactionErrorHandler"
title="TransactionErrorHandler">TransactionErrorHandler</a> that overrules the default
error handler. This is convention over configuration.</td></tr></table></div>

<h3><a name="ErrorHandler-FeaturessupportbyvariousErrorHandlers"></a>Features
support by various <a href="/confluence/display/CAMEL/Error+Handler" title="Error Handler">Error
Handler</a>s</h3>
<p>In Camel 1.x the <a href="/confluence/display/CAMEL/TransactionErrorHandler" title="TransactionErrorHandler">TransactionErrorHandler</a>
only supports the <b>all scopes</b> feature. In Camel 2.0 we have redone it to
be based on the same core class as the <a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a> and hence why it supports the
same feature set as this.</p>

<p>Here is a breakdown of which features is supported by the <a href="/confluence/display/CAMEL/Error+Handler"
title="Error Handler">Error Handler</a>(s):</p>
<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Feature </th>
<th class='confluenceTh'> Supported by the following <a href="/confluence/display/CAMEL/Error+Handler"
title="Error Handler">Error Handler</a> </th>
</tr>
<tr>
<td class='confluenceTd'> all scopes </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a>, <a href="/confluence/display/CAMEL/TransactionErrorHandler"
title="TransactionErrorHandler">TransactionErrorHandler</a>, <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> </td>
</tr>
<tr>
<td class='confluenceTd'> onException </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a>, <a href="/confluence/display/CAMEL/TransactionErrorHandler"
title="TransactionErrorHandler">TransactionErrorHandler</a>, <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> </td>
</tr>
<tr>
<td class='confluenceTd'> onWhen </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a>, <a href="/confluence/display/CAMEL/TransactionErrorHandler"
title="TransactionErrorHandler">TransactionErrorHandler</a>, <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> </td>
</tr>
<tr>
<td class='confluenceTd'> continued </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a>, <a href="/confluence/display/CAMEL/TransactionErrorHandler"
title="TransactionErrorHandler">TransactionErrorHandler</a>, <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> </td>
</tr>
<tr>
<td class='confluenceTd'> handled </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a>, <a href="/confluence/display/CAMEL/TransactionErrorHandler"
title="TransactionErrorHandler">TransactionErrorHandler</a>, <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> </td>
</tr>
<tr>
<td class='confluenceTd'> Custom ExceptionPolicy </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a>, <a href="/confluence/display/CAMEL/TransactionErrorHandler"
title="TransactionErrorHandler">TransactionErrorHandler</a>, <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> </td>
</tr>
<tr>
<td class='confluenceTd'> useOriginalBody </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a>, <a href="/confluence/display/CAMEL/TransactionErrorHandler"
title="TransactionErrorHandler">TransactionErrorHandler</a>, <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> </td>
</tr>
<tr>
<td class='confluenceTd'> retryWhile </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a>, <a href="/confluence/display/CAMEL/TransactionErrorHandler"
title="TransactionErrorHandler">TransactionErrorHandler</a>, <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> </td>
</tr>
<tr>
<td class='confluenceTd'> onRedelivery </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a>, <a href="/confluence/display/CAMEL/TransactionErrorHandler"
title="TransactionErrorHandler">TransactionErrorHandler</a>, <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> </td>
</tr>
<tr>
<td class='confluenceTd'> <a href="http://camel.apache.org/maven/camel-core/apidocs/org/apache/camel/processor/RedeliveryPolicy.html"
class="external-link" rel="nofollow">RedeliveryPolicy</a> </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a>, <a href="/confluence/display/CAMEL/TransactionErrorHandler"
title="TransactionErrorHandler">TransactionErrorHandler</a>, <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> </td>
</tr>
<tr>
<td class='confluenceTd'> asyncDelayedRedelivery </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/DefaultErrorHandler"
title="DefaultErrorHandler">DefaultErrorHandler</a>, <a href="/confluence/display/CAMEL/TransactionErrorHandler"
title="TransactionErrorHandler">TransactionErrorHandler</a>, <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> </td>
</tr>
<tr>
<td class='confluenceTd'> dead letter queue </td>
<td class='confluenceTd'> <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> </td>
</tr>
</tbody></table>
</div>


<p>See <a href="/confluence/display/CAMEL/Exception+Clause" title="Exception Clause">Exception
Clause</a> documentation for documentation of some of the features above.</p>

<h3><a name="ErrorHandler-Scopes"></a>Scopes</h3>
<p>The error handler is scoped as either</p>
<ul class="alternate" type="square">
	<li>global</li>
	<li>per route</li>
</ul>


<p>The following example shows how you can register a global error handler (in this
case using the logging handler)</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">// use logging error handler
</span>        errorHandler(loggingErrorHandler(<span class="code-quote">"com.mycompany.foo"</span>));

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

<p>The following example shows how you can register a route specific error handler;
the customized logging handler is only registered for the route from <a href="/confluence/display/CAMEL/Endpoint"
title="Endpoint">Endpoint</a> <b>seda:a</b></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">// <span class="code-keyword">this</span>
route is using a nested logging error handler
</span>        from(<span class="code-quote">"seda:a"</span>)
            <span class="code-comment">// here we configure the logging error handler
</span>            .errorHandler(loggingErrorHandler(<span class="code-quote">"com.mycompany.foo"</span>))
            <span class="code-comment">// and we <span class="code-keyword">continue</span>
with the routing here
</span>            .to(<span class="code-quote">"seda:b"</span>);

        <span class="code-comment">// <span class="code-keyword">this</span>
route will use the <span class="code-keyword">default</span> error handler (DeadLetterChannel)
</span>        from(<span class="code-quote">"seda:b"</span>).to(<span
class="code-quote">"seda:c"</span>);
    }
};
</pre>
</div></div>


<h3><a name="ErrorHandler-Springbasedconfiguration"></a>Spring based configuration</h3>
<p><b>Available as of Camel 1.4</b></p>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><b>Java DSL
vs. Spring DSL</b><br />The error handler is configured a bit differently in Java
DSL and Spring DSL. Spring DSL relies more on standard Spring bean configuration whereas Java
DSL uses fluent builders.</td></tr></table></div>

<p>In Camel 1.4 the error handler can be configured as a spring bean and scoped in:</p>
<ul class="alternate" type="square">
	<li>global (the camelContext tag)</li>
	<li>per route (the route tag)</li>
	<li>or per policy (the policy/transacted tag)</li>
</ul>


<p>The error handler is configured with the <tt>errorHandlerRef</tt> attribute.</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>Error Handler Hierarchy</b><br
/>The error handlers is inherited, so if you only have set a global error handler then
its use everywhere. But you can override this in a route and use another error handler.</td></tr></table></div>

<h4><a name="ErrorHandler-Springbasedconfigurationsample"></a>Spring based
configuration sample</h4>
<p>In this sample we configure a <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a> on the route that should redeliver
at most 3 times and use a little delay before retrying.<br/>
First we configure the reference to <b>myDeadLetterErrorHandler</b> using the
<tt>errorHandlerRef</tt> attribute on the <tt>route</tt> tag.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">  <span class="code-tag">&lt;camelContext xmlns=<span
class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
      <span class="code-tag">&lt;template id=<span class="code-quote">"myTemplate"</span>/&gt;</span>
<span class="code-tag"><span class="code-comment">&lt;!-- set the errorHandlerRef
to our DeadLetterChannel, this applies for this route only --&gt;</span></span>
      <span class="code-tag">&lt;route errorHandlerRef=<span class="code-quote">"myDeadLetterErrorHandler"</span>&gt;</span>
          <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:in"</span>/&gt;</span>
          <span class="code-tag">&lt;process ref=<span class="code-quote">"myFailureProcessor"</span>/&gt;</span>
          <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
      <span class="code-tag">&lt;/route&gt;</span>
  <span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>Then we configure <b>myDeadLetterErrorHandler</b> that is our <a href="/confluence/display/CAMEL/Dead+Letter+Channel"
title="Dead Letter Channel">Dead Letter Channel</a>. This configuration is standard
Spring using the bean element.<br/>
And finally we have another spring bean for the redelivery policy where we can configure the
options for how many times to redeliver, delays etc.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">   <span class="code-tag"><span class="code-comment">&lt;!--
here we configure our DeadLetterChannel --&gt;</span></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"><span class="code-comment">&lt;!-- exchanges is
routed to mock:dead in cased redelivery failed --&gt;</span></span>
       <span class="code-tag">&lt;property name=<span class="code-quote">"deadLetterUri"</span>
value=<span class="code-quote">"mock:dead"</span>/&gt;</span>
	<span class="code-tag"><span class="code-comment">&lt;!-- reference the redelivery
policy to use --&gt;</span></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"><span class="code-comment">&lt;!-- here we set the
redelivery settings --&gt;</span></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"><span class="code-comment">&lt;!-- try redelivery
at most 3 times, after that the exchange is dead and its routed to the mock:dead endpoint
--&gt;</span></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"><span class="code-comment">&lt;!-- delay 250ms before
redelivery --&gt;</span></span>
       <span class="code-tag">&lt;property name=<span class="code-quote">"redeliveryDelay"</span>
value=<span class="code-quote">"250"</span>/&gt;</span>
   <span class="code-tag">&lt;/bean&gt;</span>
</pre>
</div></div>

<p>From Camel 2.3.0, camel provides a customer bean configuration for the Error Handler,
you can find the examples here.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;errorHandler id=<span
class="code-quote">"loggingErrorHandler"</span> type=<span class="code-quote">"LoggingErrorHandler"</span>
level=<span class="code-quote">"INFO"</span> xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>/&gt;</span>

<span class="code-tag"><span class="code-comment">&lt;!-- If don't specify
type attribute, the type value will be set to DefaultErrorHandler --&gt;</span></span>
<span class="code-tag">&lt;errorHandler id=<span class="code-quote">"errorHandler"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>/&gt;</span>

<span class="code-tag"><span class="code-comment">&lt;!-- You can define the
redeliveryPolicy inside of the errorHandler --&gt;</span></span>
<span class="code-tag">&lt;camel:errorHandler id=<span class="code-quote">"defaultErrorHandler"</span>
type=<span class="code-quote">"DefaultErrorHandler"</span>&gt;</span>
    <span class="code-tag">&lt;camel:redeliveryPolicy maximumRedeliveries=<span
class="code-quote">"2"</span> redeliveryDelay=<span class="code-quote">"0"</span>
logStackTrace=<span class="code-quote">"false"</span>/&gt;</span>
<span class="code-tag">&lt;/camel:errorHandler&gt;</span>

<span class="code-tag">&lt;camel:errorHandler id=<span class="code-quote">"deadLetterErrorHandler"</span>
type=<span class="code-quote">"DeadLetterChannel"</span> deadLetterUri=<span
class="code-quote">"log:dead"</span>&gt;</span>
    <span class="code-tag">&lt;camel:redeliveryPolicy maximumRedeliveries=<span
class="code-quote">"2"</span> redeliveryDelay=<span class="code-quote">"1000"</span>
logHandled=<span class="code-quote">"true"</span> asyncDelayedRedelivery=<span
class="code-quote">"true"</span>/&gt;</span>
<span class="code-tag">&lt;/camel:errorHandler&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"myErrorProcessor"</span>
class=<span class="code-quote">"org.apache.camel.spring.handler.MyErrorProcessor"</span>/&gt;</span>

<span class="code-tag"><span class="code-comment">&lt;!-- TX error handler
can be configured using a template --&gt;</span></span>
&lt;camel:errorHandler id=<span class="code-quote">"transactionErrorHandler"</span>
type=<span class="code-quote">"TransactionErrorHandler"</span>
                    transactionTemplateRef=<span class="code-quote">"PROPAGATION_REQUIRED"</span>
onRedeliveryRef=<span class="code-quote">"myErrorProcessor"</span>/&gt;

<span class="code-tag"><span class="code-comment">&lt;!-- or using a transaction
manager --&gt;</span></span>
<span class="code-tag">&lt;camel:errorHandler id=<span class="code-quote">"txEH"</span>
type=<span class="code-quote">"TransactionErrorHandler"</span> transactionManagerRef=<span
class="code-quote">"txManager"</span>/&gt;</span>

<span class="code-tag"><span class="code-comment">&lt;!-- You can also define
the errorHandler inside the camelContext --&gt;</span></span>
<span class="code-tag">&lt;camelContext errorHandlerRef=<span class="code-quote">"noErrorHandler"</span>
xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;errorHandler id=<span class="code-quote">"noErrorHandler"</span>
type=<span class="code-quote">"NoErrorHandler"</span>/&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>


<h3><a name="ErrorHandler-Usingthetransactionalerrorhandler"></a>Using the
transactional error handler</h3>
<p>The transactional error handler is introduced in Camel 1.4 and is based on spring
transaction. This requires the usage of the camel-spring component.<br/>
See <a href="/confluence/display/CAMEL/Transactional+Client" title="Transactional Client">Transactional
Client</a> that has many samples for how to use and transactional behavior and configuration
with this error handler.</p>

<h3><a name="ErrorHandler-Seealso"></a>See also</h3>
<ul class="alternate" type="square">
	<li><a href="/confluence/display/CAMEL/Error+handling+in+Camel" title="Error handling
in Camel">Error handling in Camel</a> for introduction and background material on
error handling in Camel</li>
	<li><a href="/confluence/display/CAMEL/Dead+Letter+Channel" title="Dead Letter Channel">Dead
Letter Channel</a> for the default error handler in Camel 1.x</li>
	<li><a href="/confluence/display/CAMEL/DefaultErrorHandler" title="DefaultErrorHandler">DefaultErrorHandler</a>
for the default error handler in Camel 2.0 onwards</li>
	<li><a href="/confluence/display/CAMEL/TransactionErrorHandler" title="TransactionErrorHandler">TransactionErrorHandler</a>
for the default error handler for transacted routes in Camel 2.0 onwards</li>
	<li><a href="/confluence/display/CAMEL/Transactional+Client" title="Transactional
Client">Transactional Client</a> for transactional behavior</li>
	<li><a href="/confluence/display/CAMEL/Exception+Clause" title="Exception Clause">Exception
Clause</a> as it supports <b>handling</b> thrown exceptions</li>
	<li><a href="/confluence/display/CAMEL/Try+Catch+Finally" title="Try Catch Finally">Try
Catch Finally</a> for try ... catch ... finally as DSL you can use in the routing</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/Error+Handler">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=49513&revisedVersion=49&originalVersion=48">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Error+Handler?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message