Error Handling of Apache camel framework

Source: Internet
Author: User
Tags try catch apache camel

Apache camel provides many exception handling mechanisms in the routing process. This article briefly introduces how to handle exceptions.

1. By default, exceptions that are not handled during the routing process will be thrown to the initiator of the route, and the route in the abnormal process will be stopped for subsequent steps.

For example, if the following route has an error in process (P1), the current route is stopped and the file does not reach "D:/temp/outbox", and D: the file that causes an exception in/temp/inbox remains in the D:/temp/inbox folder. Because camel polls this folder, the exception will continue when processing this file.

from("file:d:/temp/inbox?delay=30000").process(p0).process(p1).to("file:d:/temp/outbox");public class TProcessor0 implements Processor{    public void process(Exchange exchange) throws Exception {                System.out.println("what if here has a transaction,later processing failed?");    }}public class TProcessor0 implements Processor{    public void process(Exchange exchange) throws Exception {                String nullStr=null;        nullStr.length();    }}

If the route is changed to the following: Because the exception occurs in process (P1), the file will arrive at "D:/temp/outbox", but D: the file that causes an exception in/temp/inbox is still in the D:/temp/inbox folder. The exception will continue during the next round-robin.

From ("file: D:/temp/inbox? Delay= 30000 "). To (" file: D:/temp/outbox "). Process (P0). Process (P1 );

By default, there are basically no rollback operations for the steps that have been done. If transaction control is required, it will be even worse. [I will write another article about how camel implements transaction control transactionerrorhandler]

2. Use the deadletterchannel provided by camel to route the error message to "Dead queue" and stop the current route. The following figure shows an example: (the image is from camel in action)

Errorhandler (deadletterchannel ("file: D:/temp/error "));
From ("file: D:/temp/inbox? Delay= 30000 "). Process (P0). Process (P1). To (" file: D:/temp/outbox ");
If an error occurs in process (P1), the process stops the process after process (P1). files that cause exceptions in D:/temp/inbox will be placed in D: /temp/error folder. Files in D:/temp/inbox will be removed.

Errorhandler (deadletterchannel ("file: D:/temp/error"); when written in a routebuilder, It is the error handler of all routebuilder routes. if you want to specify error handler for one of the routes, the example is as follows: from ("file: D:/temp/inbox? Delay= 30000 "). errorhandler (deadletterchannel ("file: D:/temp/error ")). process (P0 ). process (P1 ). to ("file: D:/temp/outbox ");

3. Using the onexception function provided by camel, when an exception occurs, it will jump to the specified exception matching step in onexception for Processing Based on Different exception types.

Onexception (nullpointerexception. Class). Process (P2). Handled (true). To ("file: D:/temp/nullerror"). End ();
Onexception (testexception. Class). Process (P2). Continued (true). To ("file: D:/temp/teerror"). End ();

From ("file: D:/temp/inbox? Delay= 30000 "). Process (P0). Process (P1). To (" file: D:/temp/outbox "); // route xx

As defined in route builder above, when nullpointerexception occurs, it will be switched to the first onexception. At this time, it will stop processing the subsequent steps of Route XX (handled (true) to save the error message to: D:/temp/nullerror. when testexception occurs, it is redirected to the second onexception. In this case, the error message is saved to: D:/temp/teerror, ignore the exception and continue the subsequent steps of routexx (continued (Role of true setting ).

Camel also supports syntax similar to Java try catch, as follows: testexception is handled by process (P2.

From ("file: D:/temp/inbox? Delay= 30000 "). dotry (). Process (P0). Process (P1). To (" file: D:/temp/outbox ")
. Docatch (testexception. Class). Process (P2)
. Docatch (nullpointerexception. Class). Process (P2). End ();

The onexception of camel can also be used with onwhen, onredeliver, and retrywhile.

Onexception (xxxexception. Class)
. Onwhen (BEAN (XXX. class, "isillegaldata "))
. Handled (true)
. To ("file:/Acme/files/illegal ");

Xxxexception is an exception type class. XXX. class has an isillegaldata method that returns true or false to handle exceptions in more detail.

Errorhandler (defaulterrorhandler ()
. Maximumredeliveries (3)
. Onredeliver (New myonredeliveryprocessor ());

Onexception (ioexception. Class)
. Maximumredeliveries (5)
. Onredeliver (New myotheronredeliveryprocessor ());

Use onredeliver when you make some changes to the content in exchange before reprocessing.

Public class myretryruleset {
Public Boolean shouldretry (@ header (exchange. redelivery_counter) integer counter, exception causedby ){
...
}

Onexception (ioexception. Class). retrywhile (BEAN (myretryruletset. Class ));

After an exception occurs, capture the abnormal vertex and retry until the shouldretry method of myretryruletset returns false.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.