You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by bu...@apache.org on 2012/04/18 14:20:56 UTC

svn commit: r813513 - in /websites/production/camel/content: book-component-appendix.html book-in-one-page.html cache/main.pageCache file2.html

Author: buildbot
Date: Wed Apr 18 12:20:56 2012
New Revision: 813513

Log:
Production update by buildbot for camel

Modified:
    websites/production/camel/content/book-component-appendix.html
    websites/production/camel/content/book-in-one-page.html
    websites/production/camel/content/cache/main.pageCache
    websites/production/camel/content/file2.html

Modified: websites/production/camel/content/book-component-appendix.html
==============================================================================
--- websites/production/camel/content/book-component-appendix.html (original)
+++ websites/production/camel/content/book-component-appendix.html Wed Apr 18 12:20:56 2012
@@ -4333,6 +4333,99 @@ For instance lets assume a system writes
 </pre>
 </div></div>
 
+<h3><a shape="rect" name="BookComponentAppendix-HowtousetheCamelerrorhandlertodealwithexceptionstriggeredoutsidetheroutingengine"></a>How to use the Camel error handler to deal with exceptions triggered outside the routing engine</h3>
+<p>The file and ftp consumers, will by default try to pickup files. Only if that is successful then a Camel <a shape="rect" href="exchange.html" title="Exchange">Exchange</a> can be created and passed in the Camel routing engine.<br clear="none">
+When the <a shape="rect" href="exchange.html" title="Exchange">Exchange</a> is processed by the routing engine, then the Camel <a shape="rect" href="error-handling-in-camel.html" title="Error handling in Camel">Error Handling</a> takes over (eg the onException / errorHandler in the routes).<br clear="none">
+However outside the scope of the routing engine, any exceptions handling is component specific. Camel offers a <tt>org.apache.camel.spi.ExceptionHandler</tt> that allows components<br clear="none">
+to use that as a pluggable hook for end users to use their own implementation. Camel offers a default <tt>LoggingExceptionHandler</tt> that will log the exception at ERROR/WARN level.<br clear="none">
+For the file and ftp components this would be the case. However if you want to bridge the <tt>ExceptionHandler</tt> so it uses the Camel <a shape="rect" href="error-handling-in-camel.html" title="Error handling in Camel">Error Handling</a>, then<br clear="none">
+you need to implement a custom <tt>ExceptionHandler</tt> that will handle the exception by creating a Camel <a shape="rect" href="exchange.html" title="Exchange">Exchange</a> and send it to the routing engine; then the error handling of the routing engine can get triggered.</p>
+
+<p>Here is such an example based upon an unit test.</p>
+
+<p>First we have a custom <tt>ExceptionHandler</tt> where you can see we deal with the exception by sending it to a Camel <a shape="rect" href="endpoint.html" title="Endpoint">Endpoint</a> named "direct:file-error":</p>
+<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>MyExceptionHandler</b></div><div class="codeContent panelContent">
+<pre class="code-java">/**
+ * Custom {@link ExceptionHandler} to be used on the file consumer, to send
+ * exceptions to a Camel route, to let Camel deal with the error.
+ */
+<span class="code-keyword">private</span> class MyExceptionHandler <span class="code-keyword">implements</span> ExceptionHandler {
+
+    <span class="code-keyword">private</span> ProducerTemplate template;
+
+    /**
+     * We use a producer template to send a message to the Camel route
+     */
+    <span class="code-keyword">public</span> void setTemplate(ProducerTemplate template) {
+        <span class="code-keyword">this</span>.template = template;
+    }
+
+    @Override
+    <span class="code-keyword">public</span> void handleException(Throwable exception) {
+        handleException(exception.getMessage(), exception);
+    }
+
+    @Override
+    <span class="code-keyword">public</span> void handleException(<span class="code-object">String</span> message, Throwable exception) {
+        handleException(exception.getMessage(), <span class="code-keyword">null</span>, exception);
+    }
+
+    @Override
+    <span class="code-keyword">public</span> void handleException(<span class="code-keyword">final</span> <span class="code-object">String</span> message, <span class="code-keyword">final</span> Exchange originalExchange, <span class="code-keyword">final</span> Throwable exception) {
+        <span class="code-comment">// send the message to the special direct:file-error endpoint, which will trigger exception handling
+</span>        <span class="code-comment">//
+</span>        template.send(<span class="code-quote">"direct:file-error"</span>, <span class="code-keyword">new</span> Processor() {
+            @Override
+            <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
+                <span class="code-comment">// set an exception on the message from the start so the error handling is triggered
+</span>                exchange.setException(exception);
+                exchange.getIn().setBody(message);
+            }
+        });
+    }
+}
+</pre>
+</div></div>
+
+<p>Then we have a Camel route that uses the Camel routing error handler, which is the <tt>onException</tt> where we handle any IOException being thrown.<br clear="none">
+We then send the message to the same "direct:file-error" endpoint, where we handle it by transforming it to a message, and then being sent to a <a shape="rect" href="mock.html" title="Mock">Mock</a> endpoint.<br clear="none">
+This is just for testing purpose. You can handle the exception in any custom way you want, such as using a <a shape="rect" href="bean.html" title="Bean">Bean</a> or sending an email etc.</p>
+
+<p>Notice how we configure our custom <tt>MyExceptionHandler</tt> by using the <tt>consumer.exceptionHandler</tt> option to refer to <tt>#myExceptionHandler</tt> which is a id of the bean registered in the <a shape="rect" href="registry.html" title="Registry">Registry</a>. If using Spring XML or OSGi Blueprint, then that would be a &lt;bean id="myExceptionHandler" class="com.foo.MyExceptionHandler"/&gt;:</p>
+<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Camel route with routing engine error handling</b></div><div class="codeContent panelContent">
+<pre class="code-java">@Override
+<span class="code-keyword">protected</span> RouteBuilder createRouteBuilder() <span class="code-keyword">throws</span> Exception {
+    <span class="code-keyword">return</span> <span class="code-keyword">new</span> RouteBuilder() {
+        @Override
+        <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span> Exception {
+            <span class="code-comment">// to handle any IOException being thrown
+</span>            onException(IOException.class)
+                .handled(<span class="code-keyword">true</span>)
+                .log(<span class="code-quote">"IOException occurred due: ${exception.message}"</span>)
+                <span class="code-comment">// as we handle the exception we can send it to direct:file-error,
+</span>                <span class="code-comment">// where we could send out alerts or whatever we want
+</span>                .to(<span class="code-quote">"direct:file-error"</span>);
+
+            <span class="code-comment">// special route that handles file errors
+</span>            from(<span class="code-quote">"direct:file-error"</span>)
+                .log(<span class="code-quote">"File error route triggered to deal with exception ${exception?.class}"</span>)
+                <span class="code-comment">// as <span class="code-keyword">this</span> is based on unit test just transform a message and send it to a mock
+</span>                .transform().simple(<span class="code-quote">"Error ${exception.message}"</span>)
+                .to(<span class="code-quote">"mock:error"</span>);
+
+            <span class="code-comment">// <span class="code-keyword">this</span> is the file route that pickup files, notice how we use our custom exception handler on the consumer
+</span>            <span class="code-comment">// the exclusiveReadLockStrategy is only configured because <span class="code-keyword">this</span> is from an unit test, so we use that to simulate exceptions
+</span>            from(<span class="code-quote">"file:target/nospace?exclusiveReadLockStrategy=#myReadLockStrategy&amp;consumer.exceptionHandler=#myExceptionHandler"</span>)
+                .convertBodyTo(<span class="code-object">String</span>.class)
+                .to(<span class="code-quote">"mock:result"</span>);
+        }
+    };
+}
+</pre>
+</div></div>
+
+<p>The source code for this example can be seen <a shape="rect" class="external-link" href="https://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCustomExceptionHandlerTest.java">here</a></p>
+
 <h3><a shape="rect" name="BookComponentAppendix-Debuglogging"></a>Debug logging</h3>
 <p>This component has log level <b>TRACE</b> that can be helpful if you have problems.</p>
 

Modified: websites/production/camel/content/book-in-one-page.html
==============================================================================
--- websites/production/camel/content/book-in-one-page.html (original)
+++ websites/production/camel/content/book-in-one-page.html Wed Apr 18 12:20:56 2012
@@ -24497,6 +24497,99 @@ For instance lets assume a system writes
 </pre>
 </div></div>
 
+<h3><a shape="rect" name="BookInOnePage-HowtousetheCamelerrorhandlertodealwithexceptionstriggeredoutsidetheroutingengine"></a>How to use the Camel error handler to deal with exceptions triggered outside the routing engine</h3>
+<p>The file and ftp consumers, will by default try to pickup files. Only if that is successful then a Camel <a shape="rect" href="exchange.html" title="Exchange">Exchange</a> can be created and passed in the Camel routing engine.<br clear="none">
+When the <a shape="rect" href="exchange.html" title="Exchange">Exchange</a> is processed by the routing engine, then the Camel <a shape="rect" href="error-handling-in-camel.html" title="Error handling in Camel">Error Handling</a> takes over (eg the onException / errorHandler in the routes).<br clear="none">
+However outside the scope of the routing engine, any exceptions handling is component specific. Camel offers a <tt>org.apache.camel.spi.ExceptionHandler</tt> that allows components<br clear="none">
+to use that as a pluggable hook for end users to use their own implementation. Camel offers a default <tt>LoggingExceptionHandler</tt> that will log the exception at ERROR/WARN level.<br clear="none">
+For the file and ftp components this would be the case. However if you want to bridge the <tt>ExceptionHandler</tt> so it uses the Camel <a shape="rect" href="error-handling-in-camel.html" title="Error handling in Camel">Error Handling</a>, then<br clear="none">
+you need to implement a custom <tt>ExceptionHandler</tt> that will handle the exception by creating a Camel <a shape="rect" href="exchange.html" title="Exchange">Exchange</a> and send it to the routing engine; then the error handling of the routing engine can get triggered.</p>
+
+<p>Here is such an example based upon an unit test.</p>
+
+<p>First we have a custom <tt>ExceptionHandler</tt> where you can see we deal with the exception by sending it to a Camel <a shape="rect" href="endpoint.html" title="Endpoint">Endpoint</a> named "direct:file-error":</p>
+<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>MyExceptionHandler</b></div><div class="codeContent panelContent">
+<pre class="code-java">/**
+ * Custom {@link ExceptionHandler} to be used on the file consumer, to send
+ * exceptions to a Camel route, to let Camel deal with the error.
+ */
+<span class="code-keyword">private</span> class MyExceptionHandler <span class="code-keyword">implements</span> ExceptionHandler {
+
+    <span class="code-keyword">private</span> ProducerTemplate template;
+
+    /**
+     * We use a producer template to send a message to the Camel route
+     */
+    <span class="code-keyword">public</span> void setTemplate(ProducerTemplate template) {
+        <span class="code-keyword">this</span>.template = template;
+    }
+
+    @Override
+    <span class="code-keyword">public</span> void handleException(Throwable exception) {
+        handleException(exception.getMessage(), exception);
+    }
+
+    @Override
+    <span class="code-keyword">public</span> void handleException(<span class="code-object">String</span> message, Throwable exception) {
+        handleException(exception.getMessage(), <span class="code-keyword">null</span>, exception);
+    }
+
+    @Override
+    <span class="code-keyword">public</span> void handleException(<span class="code-keyword">final</span> <span class="code-object">String</span> message, <span class="code-keyword">final</span> Exchange originalExchange, <span class="code-keyword">final</span> Throwable exception) {
+        <span class="code-comment">// send the message to the special direct:file-error endpoint, which will trigger exception handling
+</span>        <span class="code-comment">//
+</span>        template.send(<span class="code-quote">"direct:file-error"</span>, <span class="code-keyword">new</span> Processor() {
+            @Override
+            <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
+                <span class="code-comment">// set an exception on the message from the start so the error handling is triggered
+</span>                exchange.setException(exception);
+                exchange.getIn().setBody(message);
+            }
+        });
+    }
+}
+</pre>
+</div></div>
+
+<p>Then we have a Camel route that uses the Camel routing error handler, which is the <tt>onException</tt> where we handle any IOException being thrown.<br clear="none">
+We then send the message to the same "direct:file-error" endpoint, where we handle it by transforming it to a message, and then being sent to a <a shape="rect" href="mock.html" title="Mock">Mock</a> endpoint.<br clear="none">
+This is just for testing purpose. You can handle the exception in any custom way you want, such as using a <a shape="rect" href="bean.html" title="Bean">Bean</a> or sending an email etc.</p>
+
+<p>Notice how we configure our custom <tt>MyExceptionHandler</tt> by using the <tt>consumer.exceptionHandler</tt> option to refer to <tt>#myExceptionHandler</tt> which is a id of the bean registered in the <a shape="rect" href="registry.html" title="Registry">Registry</a>. If using Spring XML or OSGi Blueprint, then that would be a &lt;bean id="myExceptionHandler" class="com.foo.MyExceptionHandler"/&gt;:</p>
+<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Camel route with routing engine error handling</b></div><div class="codeContent panelContent">
+<pre class="code-java">@Override
+<span class="code-keyword">protected</span> RouteBuilder createRouteBuilder() <span class="code-keyword">throws</span> Exception {
+    <span class="code-keyword">return</span> <span class="code-keyword">new</span> RouteBuilder() {
+        @Override
+        <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span> Exception {
+            <span class="code-comment">// to handle any IOException being thrown
+</span>            onException(IOException.class)
+                .handled(<span class="code-keyword">true</span>)
+                .log(<span class="code-quote">"IOException occurred due: ${exception.message}"</span>)
+                <span class="code-comment">// as we handle the exception we can send it to direct:file-error,
+</span>                <span class="code-comment">// where we could send out alerts or whatever we want
+</span>                .to(<span class="code-quote">"direct:file-error"</span>);
+
+            <span class="code-comment">// special route that handles file errors
+</span>            from(<span class="code-quote">"direct:file-error"</span>)
+                .log(<span class="code-quote">"File error route triggered to deal with exception ${exception?.class}"</span>)
+                <span class="code-comment">// as <span class="code-keyword">this</span> is based on unit test just transform a message and send it to a mock
+</span>                .transform().simple(<span class="code-quote">"Error ${exception.message}"</span>)
+                .to(<span class="code-quote">"mock:error"</span>);
+
+            <span class="code-comment">// <span class="code-keyword">this</span> is the file route that pickup files, notice how we use our custom exception handler on the consumer
+</span>            <span class="code-comment">// the exclusiveReadLockStrategy is only configured because <span class="code-keyword">this</span> is from an unit test, so we use that to simulate exceptions
+</span>            from(<span class="code-quote">"file:target/nospace?exclusiveReadLockStrategy=#myReadLockStrategy&amp;consumer.exceptionHandler=#myExceptionHandler"</span>)
+                .convertBodyTo(<span class="code-object">String</span>.class)
+                .to(<span class="code-quote">"mock:result"</span>);
+        }
+    };
+}
+</pre>
+</div></div>
+
+<p>The source code for this example can be seen <a shape="rect" class="external-link" href="https://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCustomExceptionHandlerTest.java">here</a></p>
+
 <h3><a shape="rect" name="BookInOnePage-Debuglogging"></a>Debug logging</h3>
 <p>This component has log level <b>TRACE</b> that can be helpful if you have problems.</p>
 

Modified: websites/production/camel/content/cache/main.pageCache
==============================================================================
Binary files - no diff available.

Modified: websites/production/camel/content/file2.html
==============================================================================
--- websites/production/camel/content/file2.html (original)
+++ websites/production/camel/content/file2.html Wed Apr 18 12:20:56 2012
@@ -800,6 +800,99 @@ For instance lets assume a system writes
 </pre>
 </div></div>
 
+<h3><a shape="rect" name="File2-HowtousetheCamelerrorhandlertodealwithexceptionstriggeredoutsidetheroutingengine"></a>How to use the Camel error handler to deal with exceptions triggered outside the routing engine</h3>
+<p>The file and ftp consumers, will by default try to pickup files. Only if that is successful then a Camel <a shape="rect" href="exchange.html" title="Exchange">Exchange</a> can be created and passed in the Camel routing engine.<br clear="none">
+When the <a shape="rect" href="exchange.html" title="Exchange">Exchange</a> is processed by the routing engine, then the Camel <a shape="rect" href="error-handling-in-camel.html" title="Error handling in Camel">Error Handling</a> takes over (eg the onException / errorHandler in the routes).<br clear="none">
+However outside the scope of the routing engine, any exceptions handling is component specific. Camel offers a <tt>org.apache.camel.spi.ExceptionHandler</tt> that allows components<br clear="none">
+to use that as a pluggable hook for end users to use their own implementation. Camel offers a default <tt>LoggingExceptionHandler</tt> that will log the exception at ERROR/WARN level.<br clear="none">
+For the file and ftp components this would be the case. However if you want to bridge the <tt>ExceptionHandler</tt> so it uses the Camel <a shape="rect" href="error-handling-in-camel.html" title="Error handling in Camel">Error Handling</a>, then<br clear="none">
+you need to implement a custom <tt>ExceptionHandler</tt> that will handle the exception by creating a Camel <a shape="rect" href="exchange.html" title="Exchange">Exchange</a> and send it to the routing engine; then the error handling of the routing engine can get triggered.</p>
+
+<p>Here is such an example based upon an unit test.</p>
+
+<p>First we have a custom <tt>ExceptionHandler</tt> where you can see we deal with the exception by sending it to a Camel <a shape="rect" href="endpoint.html" title="Endpoint">Endpoint</a> named "direct:file-error":</p>
+<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>MyExceptionHandler</b></div><div class="codeContent panelContent">
+<pre class="code-java">/**
+ * Custom {@link ExceptionHandler} to be used on the file consumer, to send
+ * exceptions to a Camel route, to let Camel deal with the error.
+ */
+<span class="code-keyword">private</span> class MyExceptionHandler <span class="code-keyword">implements</span> ExceptionHandler {
+
+    <span class="code-keyword">private</span> ProducerTemplate template;
+
+    /**
+     * We use a producer template to send a message to the Camel route
+     */
+    <span class="code-keyword">public</span> void setTemplate(ProducerTemplate template) {
+        <span class="code-keyword">this</span>.template = template;
+    }
+
+    @Override
+    <span class="code-keyword">public</span> void handleException(Throwable exception) {
+        handleException(exception.getMessage(), exception);
+    }
+
+    @Override
+    <span class="code-keyword">public</span> void handleException(<span class="code-object">String</span> message, Throwable exception) {
+        handleException(exception.getMessage(), <span class="code-keyword">null</span>, exception);
+    }
+
+    @Override
+    <span class="code-keyword">public</span> void handleException(<span class="code-keyword">final</span> <span class="code-object">String</span> message, <span class="code-keyword">final</span> Exchange originalExchange, <span class="code-keyword">final</span> Throwable exception) {
+        <span class="code-comment">// send the message to the special direct:file-error endpoint, which will trigger exception handling
+</span>        <span class="code-comment">//
+</span>        template.send(<span class="code-quote">"direct:file-error"</span>, <span class="code-keyword">new</span> Processor() {
+            @Override
+            <span class="code-keyword">public</span> void process(Exchange exchange) <span class="code-keyword">throws</span> Exception {
+                <span class="code-comment">// set an exception on the message from the start so the error handling is triggered
+</span>                exchange.setException(exception);
+                exchange.getIn().setBody(message);
+            }
+        });
+    }
+}
+</pre>
+</div></div>
+
+<p>Then we have a Camel route that uses the Camel routing error handler, which is the <tt>onException</tt> where we handle any IOException being thrown.<br clear="none">
+We then send the message to the same "direct:file-error" endpoint, where we handle it by transforming it to a message, and then being sent to a <a shape="rect" href="mock.html" title="Mock">Mock</a> endpoint.<br clear="none">
+This is just for testing purpose. You can handle the exception in any custom way you want, such as using a <a shape="rect" href="bean.html" title="Bean">Bean</a> or sending an email etc.</p>
+
+<p>Notice how we configure our custom <tt>MyExceptionHandler</tt> by using the <tt>consumer.exceptionHandler</tt> option to refer to <tt>#myExceptionHandler</tt> which is a id of the bean registered in the <a shape="rect" href="registry.html" title="Registry">Registry</a>. If using Spring XML or OSGi Blueprint, then that would be a &lt;bean id="myExceptionHandler" class="com.foo.MyExceptionHandler"/&gt;:</p>
+<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Camel route with routing engine error handling</b></div><div class="codeContent panelContent">
+<pre class="code-java">@Override
+<span class="code-keyword">protected</span> RouteBuilder createRouteBuilder() <span class="code-keyword">throws</span> Exception {
+    <span class="code-keyword">return</span> <span class="code-keyword">new</span> RouteBuilder() {
+        @Override
+        <span class="code-keyword">public</span> void configure() <span class="code-keyword">throws</span> Exception {
+            <span class="code-comment">// to handle any IOException being thrown
+</span>            onException(IOException.class)
+                .handled(<span class="code-keyword">true</span>)
+                .log(<span class="code-quote">"IOException occurred due: ${exception.message}"</span>)
+                <span class="code-comment">// as we handle the exception we can send it to direct:file-error,
+</span>                <span class="code-comment">// where we could send out alerts or whatever we want
+</span>                .to(<span class="code-quote">"direct:file-error"</span>);
+
+            <span class="code-comment">// special route that handles file errors
+</span>            from(<span class="code-quote">"direct:file-error"</span>)
+                .log(<span class="code-quote">"File error route triggered to deal with exception ${exception?.class}"</span>)
+                <span class="code-comment">// as <span class="code-keyword">this</span> is based on unit test just transform a message and send it to a mock
+</span>                .transform().simple(<span class="code-quote">"Error ${exception.message}"</span>)
+                .to(<span class="code-quote">"mock:error"</span>);
+
+            <span class="code-comment">// <span class="code-keyword">this</span> is the file route that pickup files, notice how we use our custom exception handler on the consumer
+</span>            <span class="code-comment">// the exclusiveReadLockStrategy is only configured because <span class="code-keyword">this</span> is from an unit test, so we use that to simulate exceptions
+</span>            from(<span class="code-quote">"file:target/nospace?exclusiveReadLockStrategy=#myReadLockStrategy&amp;consumer.exceptionHandler=#myExceptionHandler"</span>)
+                .convertBodyTo(<span class="code-object">String</span>.class)
+                .to(<span class="code-quote">"mock:result"</span>);
+        }
+    };
+}
+</pre>
+</div></div>
+
+<p>The source code for this example can be seen <a shape="rect" class="external-link" href="https://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCustomExceptionHandlerTest.java">here</a></p>
+
 <h3><a shape="rect" name="File2-Debuglogging"></a>Debug logging</h3>
 <p>This component has log level <b>TRACE</b> that can be helpful if you have problems.</p>