You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by majiedahamed <ma...@cognizant.com> on 2011/11/23 07:57:33 UTC

Catch camel exception in java code

Hi,

I have the following exception handling in one of the route,Camel is
catching the exception given in the list into the log file.All i want is to
catch the same exception in java code to handle with some logic.I wanted to
format a response message with the faliure flag and exception description to
the failed messages in the route using a spring bean.Please help me on this
issue

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<onException useOriginalMessage="true">
			
<exception>org.apache.camel.processor.validation.SchemaValidationException</exception>	
<exception>org.xml.sax.SAXParseException</exception>	
<exception>org.apache.camel.ValidationException</exception>
<exception>java.io.IOException</exception>
<exception>org.springframework.mail.MailSendException</exception>
<exception>javax.xml.xpath.XPathExpressionException</exception>
<exception>org.apache.camel.ResolveEndpointFailedException</exception>
<exception>java.text.ParseException</exception>
<exception>java.lang.NullPointerException</exception>
<exception>javax.xml.transform.TransformerException</exception>


	<exception>java.lang.Exception</exception>		
	 
<redeliveryPolicy maximumRedeliveries="0" />				
<handled>
<constant>true</constant>
</handled>

				
<to
uri="log:xml?level=INFO&amp;showException=true&amp;showCaughtException=true&amp;showStackTrace=true"
/>

<to uri="file:{{retry_loc}}" />

</onException>

+++++++++++++++++++++++++++++++++++++++++++++


--
View this message in context: http://camel.465427.n5.nabble.com/Catch-camel-exception-in-java-code-tp5015714p5015714.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Catch camel exception in java code

Posted by majiedahamed <ma...@cognizant.com>.
Sorry class is like this

public class MyBean {
	
	public void doSomething(Object body, Exception cause) { 
		cause.printStackTrace();
	}

}

--
View this message in context: http://camel.465427.n5.nabble.com/Catch-camel-exception-in-java-code-tp5015714p5016574.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Catch camel exception in java code

Posted by majiedahamed <ma...@cognizant.com>.
Thanks ..it is working,there was problem in my war


--
View this message in context: http://camel.465427.n5.nabble.com/Catch-camel-exception-in-java-code-tp5015714p5016667.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Catch camel exception in java code

Posted by majiedahamed <ma...@cognizant.com>.
Also please suggest how can i use

@ExchangeException  To bind to an Exception set on the exchange (Camel 2.0) 
here ..!!

--
View this message in context: http://camel.465427.n5.nabble.com/Catch-camel-exception-in-java-code-tp5015714p5016642.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Catch camel exception in java code

Posted by Claus Ibsen <cl...@gmail.com>.
On Wed, Nov 23, 2011 at 2:18 PM, majiedahamed
<ma...@cognizant.com> wrote:
> The camel version is 2.4
>

There could be an issue with that old Camel release.

If you bean has a signature of Exchange as below:

public void doSomething(Exchange exchange) {
   Object caught = exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
   System.out.println(caught);
}

Then you can see if the caught exception is stored as exchange
property as it should be, to allow you to get access to it.

Upgrading to a newer Camel release should work.


> 1)the original code is
> +++++++++++++++
> <onException useOriginalMessage="true">
>
> <exception>org.apache.camel.processor.validation.SchemaValidationException</exception>
>  <exception>org.xml.sax.SAXParseException</exception>
>  <exception>org.apache.camel.ValidationException</exception>
>
>  <exception>java.lang.Exception</exception>
>  <redeliveryPolicy maximumRedeliveries="0" />
>  <handled>
>  <constant>true</constant>
>  </handled>
>  <to
>  uri="log:xml?
> level=INFO&amp;showException=true&amp;showCaughtException=true&amp;showStackTrace=true"
> />
>  <to uri="file:{{retry_loc}}" />
>  <to uri="bean:myBean"/>
> </onException>
>
> 2) My class is
>
> public class MyBean {
>
>        public void doSomething(Object body, Exception cause) {
>                cause.printStackTrace();
>        }
>
> }
>
> 3)Bean decleration is <bean id="myBean" class="MyBean"></bean>
>
> Please suggest should i use parameter to the below and try ?
>
> <to uri="bean:myBean?methodName=doSomething"/>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Catch-camel-exception-in-java-code-tp5015714p5016627.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: Catch camel exception in java code

Posted by majiedahamed <ma...@cognizant.com>.
The camel version is 2.4

1)the original code is 
+++++++++++++++
<onException useOriginalMessage="true">			

<exception>org.apache.camel.processor.validation.SchemaValidationException</exception>	
 <exception>org.xml.sax.SAXParseException</exception>	   
 <exception>org.apache.camel.ValidationException</exception>
 
 <exception>java.lang.Exception</exception>		
 <redeliveryPolicy maximumRedeliveries="0" />				
 <handled>
  <constant>true</constant>
 </handled>
  <to
  uri="log:xml? 
level=INFO&amp;showException=true&amp;showCaughtException=true&amp;showStackTrace=true"
/>
  <to uri="file:{{retry_loc}}" />
  <to uri="bean:myBean"/>
</onException>

2) My class is 

public class MyBean {
	
	public void doSomething(Object body, Exception cause) { 
		cause.printStackTrace();
	}

}

3)Bean decleration is <bean id="myBean" class="MyBean"></bean> 

Please suggest should i use parameter to the below and try ?

<to uri="bean:myBean?methodName=doSomething"/>


--
View this message in context: http://camel.465427.n5.nabble.com/Catch-camel-exception-in-java-code-tp5015714p5016627.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Catch camel exception in java code

Posted by Claus Ibsen <cl...@gmail.com>.
What version of Camel are you using?


On Wed, Nov 23, 2011 at 1:57 PM, majiedahamed
<ma...@cognizant.com> wrote:
> Thanks..
>
> I went through the link
>
> 1)<bean id="myBean" class="MyBean"></bean>
>
> 2)<onException>
>   ..
>   <to uri="bean:myBean"/>
>  </onException>
>
> 3)My class goes like this
>        public void doSomething(Object body, Exception cause) {
>                cause.printStackTrace();
>        }
>
>
> And in the route when the exeption occurs,It is not printing the exception
>
> I also tried
>
> <to uri="bean:myBean?methodName=doSomething"/>
>
> I need help in binding the Exception object from Camel to the bean
>
> It not working  Am i missing some thing here ,Please suggest !!!!
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Catch-camel-exception-in-java-code-tp5015714p5016570.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: Catch camel exception in java code

Posted by majiedahamed <ma...@cognizant.com>.
Thanks..

I went through the link

1)<bean id="myBean" class="MyBean"></bean>

2)<onException>  
   .. 
   <to uri="bean:myBean"/>  
  </onException>  

3)My class goes like this
	public void doSomething(Object body, Exception cause) { 
		cause.printStackTrace();
	}


And in the route when the exeption occurs,It is not printing the exception

I also tried 

<to uri="bean:myBean?methodName=doSomething"/>

I need help in binding the Exception object from Camel to the bean

It not working  Am i missing some thing here ,Please suggest !!!!

--
View this message in context: http://camel.465427.n5.nabble.com/Catch-camel-exception-in-java-code-tp5015714p5016570.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Catch camel exception in java code

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

The bean parameter binding can happen automatic for certain types and
conventions.

Read more here
http://camel.apache.org/bean-binding.html

Also chapter 4 in the Camel in Action book covers all about using
beans with Camel, incl. the parameter binding rules.

So you only need to declare the <bean id="myBean" class="com.foo.MyBeanClass"/>

And Camel figures out to invoke your bean. You can specify a method
name to invoke, in case of ambiguity etc.
But its all documented on that link above, and other wiki pages. (sort
of, the book explains it more friendly of course).


On Wed, Nov 23, 2011 at 12:17 PM, majiedahamed
<ma...@cognizant.com> wrote:
> Thank you so much
>
> Can you please elaborate on bean paramater binding with camel for Exception
> object
>
> Fot this case what will be the below details
>
> <onException>
>   ..
>  <to uri="bean:myBean"/>
> </onException>
>
>
> <bean id="myBean" class="MyBean">
>        <property name="??" value="??" />
> </bean>
>
>
>
> Public class MyBean{
>
>  // ?? variables types in beans
>  public void doSomething(Object body, Exception cause) {
>
>  }
> }
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Catch-camel-exception-in-java-code-tp5015714p5016288.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: Catch camel exception in java code

Posted by majiedahamed <ma...@cognizant.com>.
Thank you so much

Can you please elaborate on bean paramater binding with camel for Exception
object

Fot this case what will be the below details

<onException> 
   .. 
  <to uri="bean:myBean"/> 
</onException> 


<bean id="myBean" class="MyBean">
	<property name="??" value="??" />
</bean>



Public class MyBean{

  // ?? variables types in beans
  public void doSomething(Object body, Exception cause) { 

  } 
}

--
View this message in context: http://camel.465427.n5.nabble.com/Catch-camel-exception-in-java-code-tp5015714p5016288.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Catch camel exception in java code

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

Just route to a bean in the <onException> section.

<onException>
   ..
  <to uri="bean:myBean"/>
</onException>

And the spring bean
<bean id="myBean" class=...">

And the bean signature uses bean parameter binding with Camel

For example the 1st parameter is the message body, the 2nd the caused
exception etc.
public void doSomething(Object body, Exception cause) {

}




On Wed, Nov 23, 2011 at 7:57 AM, majiedahamed
<ma...@cognizant.com> wrote:
> Hi,
>
> I have the following exception handling in one of the route,Camel is
> catching the exception given in the list into the log file.All i want is to
> catch the same exception in java code to handle with some logic.I wanted to
> format a response message with the faliure flag and exception description to
> the failed messages in the route using a spring bean.Please help me on this
> issue
>
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> <onException useOriginalMessage="true">
>
> <exception>org.apache.camel.processor.validation.SchemaValidationException</exception>
> <exception>org.xml.sax.SAXParseException</exception>
> <exception>org.apache.camel.ValidationException</exception>
> <exception>java.io.IOException</exception>
> <exception>org.springframework.mail.MailSendException</exception>
> <exception>javax.xml.xpath.XPathExpressionException</exception>
> <exception>org.apache.camel.ResolveEndpointFailedException</exception>
> <exception>java.text.ParseException</exception>
> <exception>java.lang.NullPointerException</exception>
> <exception>javax.xml.transform.TransformerException</exception>
>
>
>        <exception>java.lang.Exception</exception>
>
> <redeliveryPolicy maximumRedeliveries="0" />
> <handled>
> <constant>true</constant>
> </handled>
>
>
> <to
> uri="log:xml?level=INFO&amp;showException=true&amp;showCaughtException=true&amp;showStackTrace=true"
> />
>
> <to uri="file:{{retry_loc}}" />
>
> </onException>
>
> +++++++++++++++++++++++++++++++++++++++++++++
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Catch-camel-exception-in-java-code-tp5015714p5015714.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/