简体   繁体   中英

Multiple error handler in camel

I am new in Camel and i try to understand error / exception handling in Apache Camel. I want to do the following:

  1. If any Exception occurs on the route, the original message should be moved to a special "error directory" for analysis reasons.
  2. If any Exception occurs, additionally the Exception message should be logged / send as mail
  3. If any Exception occurs, additionally the Exception message should be logged / appended to a logging file

For 1. i used the DeadLetterChannel and saved the message to a folder. This works fine.

For 2. and 3. i wanted to use the LoggingErrorHandler which moves the error exception to a direct route, where i send the mail and append a logging file. But 2. and 3. doesn't work, i get in the log4j file:

"ERROR Failed delivery for (MessageId: [...]"

Here my routing code:

public void configure() {
    errorHandler(deadLetterChannel("direct:moveFailedMessage").useOriginalMessage());
    from("direct:moveFailedMessage").to(ftpErrorURI+ftpOptions); 
    //.to(sendMailURI); => this mail works, but here is orig. msg context

    from("sql:select * from v_myView?dataSource=myDataSource")
    .bean("orderSqlToJms")
    .to(jmsURI+"?JmsMessageType=Object")
    .bean("validate")
    // this error handler don't work as i expect 
    .errorHandler(loggingErrorHandler("direct:logExceptionToMailAndFile"))
    .to(ftpOutURI+ftpOptions);

    from("direct:logExceptionToMailAndFile").to(sendMailURI).to(ftpErrorLogURI+ftpErrorOptions);
}

The mail send works in generell, when i try it in the DeadLetterChannel context, but nothing works in the LoggingErrorHandler context. Is this handler limited to contexts like log4j ? If yes, which handler should is use? DefaultErrorHandler routes back to the sender and here is no transaction for TransactionErrorHandler ? Can i maybe catch the exception message inside the DeadLetterChannel ? I didn't find any hint which could help me, has anyone a solution?

I dont't want to catch all Exeception explicit in a different way, i want to do all the things global for all kind of exceptions. Cause of this i didn't use onException() . Mabye this is wrong?

Why i get a minus for this question? If you leave a comment, i can improve my question.

Here is the solution that works for me:

errorHandler(deadLetterChannel("direct:moveFailedMessage").useOriginalMessage());
from("direct:moveFailedMessage").to(ftpErrorURI+ftpOptions).process(new Processor() {
    public void process(Exchange exchange) throws Exception {
        Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
        exchange.getIn().setBody(cause.getMessage());
    }
}).to(sendMailURI).to(ftpErrorLogURI+ftpErrorOptions);

I route everything to the deadLetterChannel with the original message. At first, i write the original message which has thrown the Exception, to a file. After that, i extract the exception message in the processor and replace the original message with the exception message and route this then to the mail and the log file.

My problem was, that i didn't know, that i can access the exception message with Exchange.EXCEPTION_CAUGHT . Maybe this will help someone, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM