You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by borgel <bo...@gmail.com> on 2008/10/14 12:38:26 UTC

Error Handler and OutHeaders

I have set up a route with an error handler like this:
@Override
	public void configure() throws Exception
	{			
		onException(TransformException.class)
		.handled(true)
		.beanRef("listTransformer", "transformFailed")
		.to("bean:errorDao");
		from(fromRoute)
		.beanRef("listTransformer", "fromStringToFundList")
		.to("bean:fundListDao?methodName=save");			
	}

in the listTransformer I add an id value to the map:
public List<Fund> fromStringToFundList(@Headers Map in,
@org.apache.camel.Body String payload, @OutHeaders Map out)
{
Header  header = getHeader(payload);
id = header.getId();
out.put("headerId", id);
logger.info("Id in transformer: " + id);
List<Fund> funds = getFunds(payload)

return funds
}

When an error occurs in getFunds the errorHandler kicks in and calls
transformFailed:
public Object transformFailed(@Headers Map in, @org.apache.camel.Body String
payload, @OutHeaders Map out)
{
	logger.info("Id in transformFailed: " + in.get("headerId"));
	return "ERROR";
}

This will produce the following output:
Id in transformer: 586
Id in transformFailed: null

Why is id in transformFailed null?
-- 
View this message in context: http://www.nabble.com/Error-Handler-and-OutHeaders-tp19970966s22882p19970966.html
Sent from the Camel - Users mailing list archive at Nabble.com.


RE: Error Handler and OutHeaders

Posted by Claus Ibsen <ci...@silverbullet.dk>.
Hi

The ticket for the annotation is: CAMEL-985

Med venlig hilsen
 
Claus Ibsen
......................................
Silverbullet
Skovsgårdsvænget 21
8362 Hørning
Tlf. +45 2962 7576
Web: www.silverbullet.dk

-----Original Message-----
From: Claus Ibsen [mailto:ci@silverbullet.dk] 
Sent: 14. oktober 2008 14:10
To: camel-user@activemq.apache.org
Subject: RE: Error Handler and OutHeaders

Hi

Ah I see. This is interesting.
You can get the caused exception using Exchange.getException()

So your method transformFailed needs the Exchange parameter

Public void transformFailed(Exchange exchange, @Body String payload, ...) {
  Throwable caused = exchange.getException();
}

We currently don't have an @ annotation to bean bind the caused exception. But we will add it in the future. So you can do something like this:

Public void transformFailed(@Exception Exception caused, @Body String payload, ...) {



Med venlig hilsen
 
Claus Ibsen
......................................
Silverbullet
Skovsgårdsvænget 21
8362 Hørning
Tlf. +45 2962 7576
Web: www.silverbullet.dk

-----Original Message-----
From: borgel [mailto:borge.lotre@gmail.com] 
Sent: 14. oktober 2008 13:51
To: camel-user@activemq.apache.org
Subject: RE: Error Handler and OutHeaders


I see. 
What I was planning to do was to handle the exception by storing information
about it in the database. Is it possible to use the Exception itself in the
route?
Of course I can handle this in the code (catch the Exception and store all I
need), but I thought it was better to just throw the Exception and then
handle it in the route.


Claus Ibsen wrote:
> 
> Hi
> 
> Yeah the sample is identical. But when the exchange fails, and onException
> is taking over, then it's the *original* exchange (= copy, aka snapshot)
> that is used, just before the node that fails.
> 
> So its kinda like this
> 
>  		from(fromRoute)
> TAKE SNAPSHOT (A)
>  		.beanRef("listTransformer", " fromStringToFundList initId") 
> TAKE SNAPSHOT (B)
>             .to("bean:fundListDao?methodName=save");			
> 
> So when the beanRef fails and onException is kicked in, then the exchange
> that is used there is from SNAPSHOT A. It need to do a SNAPSHOT when it
> does retries.
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Error-Handler-and-OutHeaders-tp19970966s22882p19971930.html
Sent from the Camel - Users mailing list archive at Nabble.com.


RE: Error Handler and OutHeaders

Posted by Claus Ibsen <ci...@silverbullet.dk>.
Hi

Ah I see. This is interesting.
You can get the caused exception using Exchange.getException()

So your method transformFailed needs the Exchange parameter

Public void transformFailed(Exchange exchange, @Body String payload, ...) {
  Throwable caused = exchange.getException();
}

We currently don't have an @ annotation to bean bind the caused exception. But we will add it in the future. So you can do something like this:

Public void transformFailed(@Exception Exception caused, @Body String payload, ...) {



Med venlig hilsen
 
Claus Ibsen
......................................
Silverbullet
Skovsgårdsvænget 21
8362 Hørning
Tlf. +45 2962 7576
Web: www.silverbullet.dk

-----Original Message-----
From: borgel [mailto:borge.lotre@gmail.com] 
Sent: 14. oktober 2008 13:51
To: camel-user@activemq.apache.org
Subject: RE: Error Handler and OutHeaders


I see. 
What I was planning to do was to handle the exception by storing information
about it in the database. Is it possible to use the Exception itself in the
route?
Of course I can handle this in the code (catch the Exception and store all I
need), but I thought it was better to just throw the Exception and then
handle it in the route.


Claus Ibsen wrote:
> 
> Hi
> 
> Yeah the sample is identical. But when the exchange fails, and onException
> is taking over, then it's the *original* exchange (= copy, aka snapshot)
> that is used, just before the node that fails.
> 
> So its kinda like this
> 
>  		from(fromRoute)
> TAKE SNAPSHOT (A)
>  		.beanRef("listTransformer", " fromStringToFundList initId") 
> TAKE SNAPSHOT (B)
>             .to("bean:fundListDao?methodName=save");			
> 
> So when the beanRef fails and onException is kicked in, then the exchange
> that is used there is from SNAPSHOT A. It need to do a SNAPSHOT when it
> does retries.
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Error-Handler-and-OutHeaders-tp19970966s22882p19971930.html
Sent from the Camel - Users mailing list archive at Nabble.com.


RE: Error Handler and OutHeaders

Posted by borgel <bo...@gmail.com>.
I see. 
What I was planning to do was to handle the exception by storing information
about it in the database. Is it possible to use the Exception itself in the
route?
Of course I can handle this in the code (catch the Exception and store all I
need), but I thought it was better to just throw the Exception and then
handle it in the route.


Claus Ibsen wrote:
> 
> Hi
> 
> Yeah the sample is identical. But when the exchange fails, and onException
> is taking over, then it's the *original* exchange (= copy, aka snapshot)
> that is used, just before the node that fails.
> 
> So its kinda like this
> 
>  		from(fromRoute)
> TAKE SNAPSHOT (A)
>  		.beanRef("listTransformer", " fromStringToFundList initId") 
> TAKE SNAPSHOT (B)
>             .to("bean:fundListDao?methodName=save");			
> 
> So when the beanRef fails and onException is kicked in, then the exchange
> that is used there is from SNAPSHOT A. It need to do a SNAPSHOT when it
> does retries.
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Error-Handler-and-OutHeaders-tp19970966s22882p19971930.html
Sent from the Camel - Users mailing list archive at Nabble.com.


RE: Error Handler and OutHeaders

Posted by Claus Ibsen <ci...@silverbullet.dk>.
BTW: Maybe initId was a bad method name.

The idea is to split the beanRef into smaller piece of work, so when the "dangerous" part fails you have already set the id and related information you need.


Med venlig hilsen
 
Claus Ibsen
......................................
Silverbullet
Skovsgårdsvænget 21
8362 Hørning
Tlf. +45 2962 7576
Web: www.silverbullet.dk

-----Original Message-----
From: Claus Ibsen [mailto:ci@silverbullet.dk] 
Sent: 14. oktober 2008 13:14
To: camel-user@activemq.apache.org
Subject: RE: Error Handler and OutHeaders

Hi

Yeah the sample is identical. But when the exchange fails, and onException is taking over, then it's the *original* exchange (= copy, aka snapshot) that is used, just before the node that fails.

So its kinda like this

 		from(fromRoute)
TAKE SNAPSHOT (A)
 		.beanRef("listTransformer", " fromStringToFundList initId") 
TAKE SNAPSHOT (B)
            .to("bean:fundListDao?methodName=save");			

So when the beanRef fails and onException is kicked in, then the exchange that is used there is from SNAPSHOT A. It need to do a SNAPSHOT when it does retries.





Med venlig hilsen
 
Claus Ibsen
......................................
Silverbullet
Skovsgårdsvænget 21
8362 Hørning
Tlf. +45 2962 7576
Web: www.silverbullet.dk

-----Original Message-----
From: borgel [mailto:borge.lotre@gmail.com] 
Sent: 14. oktober 2008 13:07
To: camel-user@activemq.apache.org
Subject: RE: Error Handler and OutHeaders


What if I want to add other values to the header that i cannot initialize?
I have looked at the example "Example using handled" here:
http://activemq.apache.org/camel/exception-clause.html
and I don't see the difference. In the example a customerid is added to the
header without initializing it first.


Claus Ibsen wrote:
> 
> Hi
> 
> Id is null because the exchange that is caught by onException is a
> snapshot of the exchange just before your bean listTransformer is invoked. 
> 
> Any changes to the exchange that happens in listTransformer is not
> effective/visible for the error handler.
> 
> You can use 2 bean calls to let the id be visible
> 		from(fromRoute)
> 		.beanRef("listTransformer", "initId")
> 		.beanRef("listTransformer", "fromStringToFundList")	
> .to("bean:fundListDao?methodName=save");			
> 
> For instance a initId() method that is simple and just set the id.
> 
> Then if there is a failure in the 2nd bean then the snapshot is from just
> before the 2nd bean call.
> 
> 

-- 
View this message in context: http://www.nabble.com/Error-Handler-and-OutHeaders-tp19970966s22882p19971372.html
Sent from the Camel - Users mailing list archive at Nabble.com.


RE: Error Handler and OutHeaders

Posted by Claus Ibsen <ci...@silverbullet.dk>.
Hi

Yeah the sample is identical. But when the exchange fails, and onException is taking over, then it's the *original* exchange (= copy, aka snapshot) that is used, just before the node that fails.

So its kinda like this

 		from(fromRoute)
TAKE SNAPSHOT (A)
 		.beanRef("listTransformer", " fromStringToFundList initId") 
TAKE SNAPSHOT (B)
            .to("bean:fundListDao?methodName=save");			

So when the beanRef fails and onException is kicked in, then the exchange that is used there is from SNAPSHOT A. It need to do a SNAPSHOT when it does retries.





Med venlig hilsen
 
Claus Ibsen
......................................
Silverbullet
Skovsgårdsvænget 21
8362 Hørning
Tlf. +45 2962 7576
Web: www.silverbullet.dk

-----Original Message-----
From: borgel [mailto:borge.lotre@gmail.com] 
Sent: 14. oktober 2008 13:07
To: camel-user@activemq.apache.org
Subject: RE: Error Handler and OutHeaders


What if I want to add other values to the header that i cannot initialize?
I have looked at the example "Example using handled" here:
http://activemq.apache.org/camel/exception-clause.html
and I don't see the difference. In the example a customerid is added to the
header without initializing it first.


Claus Ibsen wrote:
> 
> Hi
> 
> Id is null because the exchange that is caught by onException is a
> snapshot of the exchange just before your bean listTransformer is invoked. 
> 
> Any changes to the exchange that happens in listTransformer is not
> effective/visible for the error handler.
> 
> You can use 2 bean calls to let the id be visible
> 		from(fromRoute)
> 		.beanRef("listTransformer", "initId")
> 		.beanRef("listTransformer", "fromStringToFundList")	
> .to("bean:fundListDao?methodName=save");			
> 
> For instance a initId() method that is simple and just set the id.
> 
> Then if there is a failure in the 2nd bean then the snapshot is from just
> before the 2nd bean call.
> 
> 

-- 
View this message in context: http://www.nabble.com/Error-Handler-and-OutHeaders-tp19970966s22882p19971372.html
Sent from the Camel - Users mailing list archive at Nabble.com.


RE: Error Handler and OutHeaders

Posted by borgel <bo...@gmail.com>.
What if I want to add other values to the header that i cannot initialize?
I have looked at the example "Example using handled" here:
http://activemq.apache.org/camel/exception-clause.html
and I don't see the difference. In the example a customerid is added to the
header without initializing it first.


Claus Ibsen wrote:
> 
> Hi
> 
> Id is null because the exchange that is caught by onException is a
> snapshot of the exchange just before your bean listTransformer is invoked. 
> 
> Any changes to the exchange that happens in listTransformer is not
> effective/visible for the error handler.
> 
> You can use 2 bean calls to let the id be visible
> 		from(fromRoute)
> 		.beanRef("listTransformer", "initId")
> 		.beanRef("listTransformer", "fromStringToFundList")	
> .to("bean:fundListDao?methodName=save");			
> 
> For instance a initId() method that is simple and just set the id.
> 
> Then if there is a failure in the 2nd bean then the snapshot is from just
> before the 2nd bean call.
> 
> 

-- 
View this message in context: http://www.nabble.com/Error-Handler-and-OutHeaders-tp19970966s22882p19971372.html
Sent from the Camel - Users mailing list archive at Nabble.com.


RE: Error Handler and OutHeaders

Posted by Claus Ibsen <ci...@silverbullet.dk>.
Hi

Id is null because the exchange that is caught by onException is a snapshot of the exchange just before your bean listTransformer is invoked. 

Any changes to the exchange that happens in listTransformer is not effective/visible for the error handler.

You can use 2 bean calls to let the id be visible
		from(fromRoute)
		.beanRef("listTransformer", "initId")
		.beanRef("listTransformer", "fromStringToFundList")		.to("bean:fundListDao?methodName=save");			

For instance a initId() method that is simple and just set the id.

Then if there is a failure in the 2nd bean then the snapshot is from just before the 2nd bean call.


Med venlig hilsen
 
Claus Ibsen
......................................
Silverbullet
Skovsgårdsvænget 21
8362 Hørning
Tlf. +45 2962 7576
Web: www.silverbullet.dk

-----Original Message-----
From: borgel [mailto:borge.lotre@gmail.com] 
Sent: 14. oktober 2008 12:38
To: camel-user@activemq.apache.org
Subject: Error Handler and OutHeaders


I have set up a route with an error handler like this:
@Override
	public void configure() throws Exception
	{			
		onException(TransformException.class)
		.handled(true)
		.beanRef("listTransformer", "transformFailed")
		.to("bean:errorDao");
		from(fromRoute)
		.beanRef("listTransformer", "fromStringToFundList")
		.to("bean:fundListDao?methodName=save");			
	}

in the listTransformer I add an id value to the map:
public List<Fund> fromStringToFundList(@Headers Map in,
@org.apache.camel.Body String payload, @OutHeaders Map out)
{
Header  header = getHeader(payload);
id = header.getId();
out.put("headerId", id);
logger.info("Id in transformer: " + id);
List<Fund> funds = getFunds(payload)

return funds
}

When an error occurs in getFunds the errorHandler kicks in and calls
transformFailed:
public Object transformFailed(@Headers Map in, @org.apache.camel.Body String
payload, @OutHeaders Map out)
{
	logger.info("Id in transformFailed: " + in.get("headerId"));
	return "ERROR";
}

This will produce the following output:
Id in transformer: 586
Id in transformFailed: null

Why is id in transformFailed null?
-- 
View this message in context: http://www.nabble.com/Error-Handler-and-OutHeaders-tp19970966s22882p19970966.html
Sent from the Camel - Users mailing list archive at Nabble.com.