OnException appears to roll back message state - Need help in understanding

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

OnException appears to roll back message state - Need help in understanding

bottabing
Hi - I am need of some help in understanding more about exception handling.  I am using Camel 2.2 inside of Servicemix 3.3.2 (along with XMLBeans and Spring).

What I need to happen is that the nature of the exception is populated inside the message and then forwarded to an error queue.  What *appears* to be happening is that the message prior to it being processed by the bean is being forwarded into the queue.  If you have any suggestions, please let me know - Thanks.

Verification Bean
----------------

if ( goodOrder )
  exchange.getOut().setBody(orderDocument.xmlText());
else {
      this.processOrderError(orderDocument, errorLog.toString());
      exchange.getOut().setHeader(Exchange.FILE_NAME,
                                              getOrderNumber(orderDocument)+".error."+System.nanoTime()+".xml");
      System.out.println(orderDocument.getClarisonicWebOrder().getProcessing().toString());
      exchange.getOut().setBody(orderDocument.xmlText());
      throw new Exception("Failed Order Verification");
}

// Note: the processOrderMessage() inserts the error message in a defined area.  The subsequent System.out.println() confirms that the error message is correctly inserted.


My Spring is:

  <camelContext id="camelContext" xmlns="http://camel.apache.org/schema/spring">
       <package>myPackage</package>
       
       <onException>
         <exception>java.lang.Exception</exception>
         <redeliveryPolicy maximumRedeliveries="0" />
         <handled>
          <constant>true</constant>
         </handled>
         <to uri="jms://queue/order-error" />
       </onException>
       
       <route>
          <from uri="jms://queue/order-file-processor"/>
            <pipeline>
              <bean ref="orderVerifier" method="processMessage"/>
              <bean ref="orderRuleEnforcer" method="processMessage"/> 
              <bean ref="orderPostprocessor" method="processMessage"/>               
              <to uri="jms://queue/order-data-processor"/>
            </pipeline> 
       </route>
         
  </camelContext>


The XML that outputs from the order-error queue does not contain the error message (that I have confirmed in the aforementioned System.out method).  Thanks for your help.
Reply | Threaded
Open this post in threaded view
|

Re: OnException appears to roll back message state - Need help in understanding

Claus Ibsen-2
The Camel error handler is covered in chapter 5 in the Camel book if
you want to get to the bottom of understanding it.

Also there is documentation at Camel web site
http://camel.apache.org/error-handling-in-camel.html

The Camel error handler will snapshot the message before its being
processed. And in case the processing failed, it will use the snapshot
message when handling the error. So its correct behavior.

In the JBI/ServiceMix "world" you can use fault messages instead of exceptions.
getOut().setFault(true);

And then if you still want Camel onException to handle the fault
messages you need to enable this using
.handleFault() in the Camel route



On Mon, Nov 1, 2010 at 10:37 PM, paul botta <[hidden email]> wrote:

>
> Hi - I am need of some help in understanding more about exception handling.
> I am using Camel 2.2 inside of Servicemix 3.3.2 (along with XMLBeans and
> Spring).
>
> What I need to happen is that the nature of the exception is populated
> inside the message and then forwarded to an error queue.  What *appears* to
> be happening is that the message prior to it being processed by the bean is
> being forwarded into the queue.  If you have any suggestions, please let me
> know - Thanks.
>
> Verification Bean
> ----------------
>
> if ( goodOrder )
>  exchange.getOut().setBody(orderDocument.xmlText());
> else {
>      this.processOrderError(orderDocument, errorLog.toString());
>      exchange.getOut().setHeader(Exchange.FILE_NAME,
>
> getOrderNumber(orderDocument)+".error."+System.nanoTime()+".xml");
>
> System.out.println(orderDocument.getClarisonicWebOrder().getProcessing().toString());
>      exchange.getOut().setBody(orderDocument.xmlText());
>      throw new Exception("Failed Order Verification");
> }
>
> // Note: the processOrderMessage() inserts the error message in a defined
> area.  The subsequent System.out.println() confirms that the error message
> is correctly inserted.
>
>
> My Spring is:
>
>  <camelContext id="camelContext"
> xmlns="http://camel.apache.org/schema/spring">
>       <package>myPackage</package>
>
>       <onException>
>         <exception>java.lang.Exception</exception>
>         <redeliveryPolicy maximumRedeliveries="0" />
>         <handled>
>          <constant>true</constant>
>         </handled>
>         <to uri="jms://queue/order-error" />
>       </onException>
>
>       <route>
>          <from uri="jms://queue/order-file-processor"/>
>            <pipeline>
>              <bean ref="orderVerifier" method="processMessage"/>
>              <bean ref="orderRuleEnforcer" method="processMessage"/>
>              <bean ref="orderPostprocessor" method="processMessage"/>
>              <to uri="jms://queue/order-data-processor"/>
>            </pipeline>
>       </route>
>
>  </camelContext>
>
>
> The XML that outputs from the order-error queue does not contain the error
> message (that I have confirmed in the aforementioned System.out method).
> Thanks for your help.
> --
> View this message in context: http://camel.465427.n5.nabble.com/OnException-appears-to-roll-back-message-state-Need-help-in-understanding-tp3245784p3245784.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



--
Claus Ibsen
-----------------
FuseSource
Email: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/
Reply | Threaded
Open this post in threaded view
|

Re: OnException appears to roll back message state - Need help in understanding

bottabing
Hi Claus - Thank-you for your quick reply.  I apologize for my lack of experience with camel, but I am unsure how to accomplish the requirement of adding the reason an order failed to the message and sending that message to an error queue.

Is there a way to override the rollback behavior or is there a way I can programatically re-route the message to an error queue from within the POJO's error handling code?

This is a show stopper for me as the client needs to be able to communicate the reason an order failed to an internal user.

Thank-you for any insight you can provide.
Reply | Threaded
Open this post in threaded view
|

Re: OnException appears to roll back message state - Need help in understanding

Claus Ibsen-2
On Tue, Nov 2, 2010 at 5:48 PM, paul botta <[hidden email]> wrote:

>
> Hi Claus - Thank-you for your quick reply.  I apologize for my lack of
> experience with camel, but I am unsure how to accomplish the requirement of
> adding the reason an order failed to the message and sending that message to
> an error queue.
>
> Is there a way to override the rollback behavior or is there a way I can
> programatically re-route the message to an error queue from within the
> POJO's error handling code?
>
> This is a show stopper for me as the client needs to be able to communicate
> the reason an order failed to an internal user.

In the processor you do something like this

// set a custom message why we failed
exchange.getOut().setBody("We failed because of we dont like milk");
// mark it as a fault
exchange.getOut().setFault(true);



In the RouteBuilder you enable fault handling

configure() {
   context.setHandleFault(true);

   from("xxx")
}

See for example this unit test (note that you dont have to set an
exception as the body if you dont like to)
https://svn.apache.org/repos/asf/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/HandleFaultPerRouteTest.java

>
> Thank-you for any insight you can provide.
> --
> View this message in context: http://camel.465427.n5.nabble.com/OnException-appears-to-roll-back-message-state-Need-help-in-understanding-tp3245784p3247023.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



--
Claus Ibsen
-----------------
FuseSource
Email: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/