You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "aum.struts" <au...@gmail.com> on 2011/08/09 14:16:15 UTC

camel Error Handling

Hi All,

i am new to camel and just playing around with this.i was trying to
understand how error handling is working around.
i have the following case
i am picking csv from a location and using build in parsing converting it to
java object and in my Bean class i am throwing "IllegalClassFormatException"
to check how it can be handled

here is my route configuration and other stuff

context.addRoutes(new RouteBuilder() {

			          public void configure() {
						onException(IllegalClassFormatException.class).handled(true).bean(new
MyBean(), "validationFailed").
						to("file:data/failed").useOriginalMessage().stop();
			        	 from("file:data?fileName=myfile.csv").unmarshal().csv()
			        	 .bean(new
Bean(),"csvHandler").to("file:data/output?fileName=xmlfile.xml");
			        	
			          }
			          

			      });
		    context.start();
            Thread.sleep(10000);
            context.stop();

MyBean class has following 2 methods

 public String csvHandler(@Body List&lt;List&lt;String&gt;> data) throws
IllegalClassFormatException{
    	throw new IllegalClassFormatException();
    	
    }

    @SuppressWarnings("unchecked")
    public Object validationFailed(@Headers Map in, @Body String payload,
@OutHeaders Map out) {
         return "Order ERROR";
    }

i can see that moment my Bean class being calle din the route its throwing
IllegalClassFormatException and i have configured onException() as described
in the code above

in the log entries i can even saw that "Order ERROR" is being set in the
message header but i am clueless how i can retrieve it in my class to show
end user this message

like in my main method i am using

route.callRoute();

where route is the class reference where i am adding route,configuring it
and starting camel context as well stopping it.

i am just confused how one can show end user what exactly happened (say in
my case csv was invalid)

thanks in advance
Aum






--
View this message in context: http://camel.465427.n5.nabble.com/camel-Error-Handling-tp4681829p4681829.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel Error Handling

Posted by "aum.struts" <au...@gmail.com>.
Any Pointer in this regard? i am really confused

--
View this message in context: http://camel.465427.n5.nabble.com/camel-Error-Handling-tp4681829p4685202.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel Error Handling

Posted by shekher awasthi <sh...@gmail.com>.
Well yes,
i want that fs there is some exception in my route execution like for e.g my
csv file isinvalid and in my bean class i am throwing an exception to
indicate that csv is not valid

in this case i am handling this exception in my onException() and even in
MyBean class i am setting a message that csv was not valid

but camelContext.startRoute() method have void return type so i am a bit
confused how i will able to get status what happened while executing the
route so that message can be passed on to the end user

i hope  i am able to clear it this time

On Wed, Aug 10, 2011 at 6:06 PM, Claus Ibsen <cl...@gmail.com> wrote:

> On Tue, Aug 9, 2011 at 2:16 PM, aum.struts <au...@gmail.com> wrote:
> > Hi All,
> >
> > i am new to camel and just playing around with this.i was trying to
> > understand how error handling is working around.
> > i have the following case
> > i am picking csv from a location and using build in parsing converting it
> to
> > java object and in my Bean class i am throwing
> "IllegalClassFormatException"
> > to check how it can be handled
> >
> > here is my route configuration and other stuff
> >
> > context.addRoutes(new RouteBuilder() {
> >
> >                                  public void configure() {
> >
>  onException(IllegalClassFormatException.class).handled(true).bean(new
> > MyBean(), "validationFailed").
> >
>  to("file:data/failed").useOriginalMessage().stop();
> >
> from("file:data?fileName=myfile.csv").unmarshal().csv()
> >                                         .bean(new
> > Bean(),"csvHandler").to("file:data/output?fileName=xmlfile.xml");
> >
> >                                  }
> >
> >
> >                              });
> >                    context.start();
> >            Thread.sleep(10000);
> >            context.stop();
> >
> > MyBean class has following 2 methods
> >
> >  public String csvHandler(@Body List&lt;List&lt;String&gt;> data) throws
> > IllegalClassFormatException{
> >        throw new IllegalClassFormatException();
> >
> >    }
> >
> >    @SuppressWarnings("unchecked")
> >    public Object validationFailed(@Headers Map in, @Body String payload,
> > @OutHeaders Map out) {
> >         return "Order ERROR";
> >    }
> >
> > i can see that moment my Bean class being calle din the route its
> throwing
> > IllegalClassFormatException and i have configured onException() as
> described
> > in the code above
> >
> > in the log entries i can even saw that "Order ERROR" is being set in the
> > message header but i am clueless how i can retrieve it in my class to
> show
> > end user this message
> >
> > like in my main method i am using
> >
> > route.callRoute();
> >
> > where route is the class reference where i am adding route,configuring it
> > and starting camel context as well stopping it.
> >
> > i am just confused how one can show end user what exactly happened (say
> in
> > my case csv was invalid)
> >
>
> Its a bit unclear what you want to do? Do you want the callRoute()
> method to return some status of how it went? OK or in case of an
> failure, some kind of exception message or what?
>
> In all cases its just java code, so you can just add some state to the
> route class, and use that to compute a response to return from the
> callRoute method.
>
>
>
> > thanks in advance
> > Aum
> >
> >
> >
> >
> >
> >
> > --
> > View this message in context:
> http://camel.465427.n5.nabble.com/camel-Error-Handling-tp4681829p4681829.html
> > Sent from the Camel - Users mailing list archive at Nabble.com.
> >
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: cibsen@fusesource.com
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>

Re: camel Error Handling

Posted by Claus Ibsen <cl...@gmail.com>.
On Tue, Aug 9, 2011 at 2:16 PM, aum.struts <au...@gmail.com> wrote:
> Hi All,
>
> i am new to camel and just playing around with this.i was trying to
> understand how error handling is working around.
> i have the following case
> i am picking csv from a location and using build in parsing converting it to
> java object and in my Bean class i am throwing "IllegalClassFormatException"
> to check how it can be handled
>
> here is my route configuration and other stuff
>
> context.addRoutes(new RouteBuilder() {
>
>                                  public void configure() {
>                                                onException(IllegalClassFormatException.class).handled(true).bean(new
> MyBean(), "validationFailed").
>                                                to("file:data/failed").useOriginalMessage().stop();
>                                         from("file:data?fileName=myfile.csv").unmarshal().csv()
>                                         .bean(new
> Bean(),"csvHandler").to("file:data/output?fileName=xmlfile.xml");
>
>                                  }
>
>
>                              });
>                    context.start();
>            Thread.sleep(10000);
>            context.stop();
>
> MyBean class has following 2 methods
>
>  public String csvHandler(@Body List&lt;List&lt;String&gt;> data) throws
> IllegalClassFormatException{
>        throw new IllegalClassFormatException();
>
>    }
>
>    @SuppressWarnings("unchecked")
>    public Object validationFailed(@Headers Map in, @Body String payload,
> @OutHeaders Map out) {
>         return "Order ERROR";
>    }
>
> i can see that moment my Bean class being calle din the route its throwing
> IllegalClassFormatException and i have configured onException() as described
> in the code above
>
> in the log entries i can even saw that "Order ERROR" is being set in the
> message header but i am clueless how i can retrieve it in my class to show
> end user this message
>
> like in my main method i am using
>
> route.callRoute();
>
> where route is the class reference where i am adding route,configuring it
> and starting camel context as well stopping it.
>
> i am just confused how one can show end user what exactly happened (say in
> my case csv was invalid)
>

Its a bit unclear what you want to do? Do you want the callRoute()
method to return some status of how it went? OK or in case of an
failure, some kind of exception message or what?

In all cases its just java code, so you can just add some state to the
route class, and use that to compute a response to return from the
callRoute method.



> thanks in advance
> Aum
>
>
>
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-Error-Handling-tp4681829p4681829.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: camel Error Handling

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

In the bean that handles the error handling, I think that is
CSVConverterBean in the method validationFailed.
What you can do is to pass error information back to the CallRoute.
(or vise versa if you want to).

So instead of using the .class you use an instance of the bean

CSVConverterBean myConverterBean = new ...
myConverterBean.setCallRoute(this);

onException(IllegalClassFormatException.class).handled(true).bean(myConverterBean,
"validationFailed).end();

And then in the validationFailed method, you can access the CallRoute
instance as you passed to it using the setter setCallRoute.
Then you can call a method on CallRoute to pass in error details.




On Thu, Aug 11, 2011 at 9:40 AM, aum.struts <au...@gmail.com> wrote:
> While going through the Documents it has been wriiten that the exception or
> custom message will be show back to caller
>
> i am confused what exactly caller in this case.
> Since my route is being executed by the camel itself so its camel who is
> caller?
>
> For e.g
> in my routebuilder class
>
> public class CallRoute {
> public void callRoute() throws Exception{
> CamelContext context = new DefaultCamelContext();
> context.addRoutes(new RouteBuilder() {
>
>                                  public void configure() {
>
> onException(IllegalClassFormatException.class).handled(true).bean(CSVConverterBean.class,
> "validationFailed).end();
>
> from("file:data/csv?fileName=input-customer2.csv").unmarshal().csv()
>                                         .bean(new
> CSVConverterBean(),"processCSVInvoice").to("xslt:http://localhost:7777/struts2/styles/XMLConverter.xsl").to("file:data/csvoutput?fileName=test11.xml");
>                                 }
>                            });
>                context.start();
>            Thread.sleep(10000);
>
>            context.stop();
>        }
> }
> and i am calling callRoute() method from my main method in another class.
> so in case IllegalClassFormatException onException() method will be called
> and which in turn will pass the things to CSVConverterBean.class,
> "validationFailed method.
>
> So i can i pass the status back to main method?
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/camel-Error-Handling-tp4681829p4688705.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: camel Error Handling

Posted by "aum.struts" <au...@gmail.com>.
Thanks Claus for the help
but i am still doubtful as it seems a work around to me to handle the
exception in camel routing.
Since in this case on exception i am sending back a custom message to the
caller while in a normal flow if something happens inside a flow we can
handle that inside that block and can throw same or new exception to the
caller.

But in this case camel is handling it all internally and not giving
exception details to the caller.

May be i have understood the flow in wrong way but this is my first
observation how camel handling exception(s)

--
View this message in context: http://camel.465427.n5.nabble.com/camel-Error-Handling-tp4681829p4705536.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: camel Error Handling

Posted by "aum.struts" <au...@gmail.com>.
While going through the Documents it has been wriiten that the exception or
custom message will be show back to caller

i am confused what exactly caller in this case.
Since my route is being executed by the camel itself so its camel who is
caller?

For e.g
in my routebuilder class

public class CallRoute {
public void callRoute() throws Exception{
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {

			          public void configure() {
					
onException(IllegalClassFormatException.class).handled(true).bean(CSVConverterBean.class,
"validationFailed).end();
			        	
from("file:data/csv?fileName=input-customer2.csv").unmarshal().csv()
			        	 .bean(new
CSVConverterBean(),"processCSVInvoice").to("xslt:http://localhost:7777/struts2/styles/XMLConverter.xsl").to("file:data/csvoutput?fileName=test11.xml");
			         }
			    });
		context.start();
            Thread.sleep(10000);
            
            context.stop();
        }
}
and i am calling callRoute() method from my main method in another class.
so in case IllegalClassFormatException onException() method will be called
and which in turn will pass the things to CSVConverterBean.class,
"validationFailed method.

So i can i pass the status back to main method?

--
View this message in context: http://camel.465427.n5.nabble.com/camel-Error-Handling-tp4681829p4688705.html
Sent from the Camel - Users mailing list archive at Nabble.com.