I am new in Camel and i try to understand error / exception handling in Apache Camel. I want to do the following:
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.