Camel: how to use log in ErrorHandler

718 Views Asked by At

I've defined an Error Handler, which applies to multiple routes i.e.

errorHandler(transactionErrorHandler() 
    .maximumRedeliveries(5) 
    .log("SOMETHING USELESS"); 

from(file1) 
    .id(route1) 
    .transacted() 
    .process(new SpecificProcessor1()); 

from(file2) 
    .id(route2) 
    .transacted() 
    .process(new SpecificProcessor2()); 

When an Exception occurs inside one of the SpecificProcessor classes, the following is logged:

[10-Jan-2014 15:08:59.449] [Error] SOMETHING USELESS: Failed delivery for (MessageId: ID-BLAH BLAH). On delivery attempt: 1 caught: java.lang.Exception: cannot do whatever I'm supposed to do 

Now I would like to print something useful to help identify whether the Exception has happened in route1 or route2 i.e.:

[10-Jan-2014 15:08:59.449] [Error] ROUTE 1: Failed delivery for (MessageId: ID-BLAH BLAH). On delivery attempt: 1 caught: java.lang.Exception: cannot do whatever I'm supposed to do 

How can I achieve that?

I have tried things like .log(${routeId}), but it doesn't work.

Many thanks!

2

There are 2 best solutions below

0
On

You can add thread name to your appender configuration. For example %t for log4j:

<appender name="..." class="...">
....
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{dd/MM/yyyy HH:mm:ss,SSS} %p %t %c - %m%n" />
    </layout>
</appender>

You will see something like:

13/01/2014 12:52:33,535 INFO Camel (mainCamelContext) thread #8 - JmsConsumer[jms/customer]  .....

I guess it is enough to identify the route

0
On

To specify the route name, you should ideally use .routeid(), rather than .id() - the later sets the node name, rather than the route name:

from(file1).routeId(route1)

You can extract the route id, you can use exchange.getFromRouteId() which will return the route id that created the exchange. If you are nesting routes using direct component, you can use the Unit of Work, as below:

exchange.getUnitOfWork().getRouteContext().getRoute().getId()

Not sure whether there is mechanism for retrieving this info using DSL.

I hope this helps.