Search Tutorials


Apache Camel Exception Handling Using Simple Example | JavaInUse

Understanding Apache Camel Exception Handling using Example

In this post we will implement Exception Handling for Apache Camel. In previous Apache Camel + Spring we had written a integrated Camel Java DSL and Spring.
We can write the exception handling code either in the spring configuration file or the Java DSL class. We will implement exception handling using Java DSL in this post.

Apache Camel - Table of Contents

File Transfer Using Java DSL Apache Camel Apache Camel Java DSL + Spring Integration Hello World Example Apache Camel Exception Handling Using Simple Example Apache Camel Redelivery policy using example Integrate Apache Camel and ActiveMQ EIP patterns using Apache Camel Apache Camel Tutorial- Integrate Spring Boot+ Apache Camel Apache Camel Tutorial- Integrate with MySQL DB using SQL query Apache Camel EIP - Splitter and Aggregator pattern Apache Camel Unit Testing Apache Camel + Spring + Quartz Hello World Example Camel application deployment on JBoss Fuse Apache Camel + Apache CXF SOAP Webservices Apache Camel + JAX-RS REST Webservice Apache Camel + CXFRS REST Webservice Apache Camel Routing Slip EIP Pattern Apache Camel Dynamic Router Pattern Apache Camel Load Balancer EIP Pattern Apache Camel Interceptors Apache Camel + Kafka Hello World Example Apache Camel - Marshalling/Unmarshalling XML/JSON Data Example Calling and Consuming Webservices using Apache Camel Apache Camel Tutorial - Send SMTP Email Using Gmail Apache Camel Tutorial - SEDA component Hello World Example Spring Boot + Apache Camel + RabbitMQ - Hello World Example Apache Camel Tutorial - Idempotent Consumer using MemoryIdempotentRepository and FileIdempotentRepository Spring Boot + Apache Camel JDBC component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + Transaction Management Example

Video

This tutorial is explained in the below Youtube Video.

Lets Begin

The project in the previous Apache Camel + Spring would be the starting point for this tutorial.
The project structure will be as follows-

Apache Camel Exception Handling Example
The exception handling for Apache camel can be implemented in 2 ways.
  • Using Do Try block
  • Using OnException block
Define a custom exception as follows-
package com.javainuse.exception;
public class CamelCustomException extends Exception {
    private static final long serialVersionUID = 1L;
}

Next we throw this custom exception once from the MyProcessor.java which will be called from our Java DSL route.
package com.javainuse.processor;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import com.javainuse.exception.CamelCustomException;

public class MyProcessor implements Processor {

    public void process(Exchange exchange) throws Exception {
        System.out.println("Exception Thrown");
        throw new CamelCustomException();
    }

}
Currently our SimpleRouteBuilder code is as follows. There is no exception handling code written. So the message will keep retrying as the exception is not handled.
package com.javainuse.route;

import org.apache.camel.builder.RouteBuilder;
import com.javainuse.processor.MyProcessor;

public class SimpleRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("file:C:/inputFolder?noop=true").process(new MyProcessor()).to("file:C:/outputFolder");
    }

}


Apache Camel Exception Handling Tutorial



Now lets have a look at the exception handling code
  • Using Do Try block
    This approach is similar to the Java try catch block. So the thrown exception will be immediately caught and the message wont keep on retrying.
    package com.javainuse.route;
    
    import org.apache.camel.builder.RouteBuilder;
    import com.javainuse.processor.MyProcessor;
    
    public class SimpleRouteBuilder extends RouteBuilder {
    
        @Override
        public void configure() throws Exception {
             from("file:C:/inputFolder?noop=true").doTry().process(new MyProcessor()).to("file:C:/outputFolder")
                .doCatch(CamelCustomException.class).process(new Processor() {
    
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("handling ex");
                    }
                }).log("Received body ");
    
    }
    


    camel17_6
    The limitation of this approach is that it is applicable only for the single route.
    Suppose we have one more seperate route. Then if an exception occurs in that route then it will not be handled by the current doTry block.
    package com.javainuse.route;
    
    import org.apache.camel.Exchange;
    import org.apache.camel.Processor;
    import org.apache.camel.builder.RouteBuilder;
    import com.javainuse.exception.CamelCustomException;
    import com.javainuse.processor.MyProcessor;
    
    public class SimpleRouteBuilder extends RouteBuilder {
    
        @Override
        public void configure() throws Exception {
            from("file:C:/inputFolder?noop=true").doTry().process(new MyProcessor()).to("file:C:/outputFolder")
                .doCatch(CamelCustomException.class).process(new Processor() {
    
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("handling ex");
                    }
                }).log("Received body ");
    
            from("file:C:/inbox?noop=true").process(new MyProcessor()).to("file:C:/outbox");
        }
    
    }
    


    camel17_7
    So the exception thrown in the route from from("file:C:/inbox?noop=true") will not be handled.
  • Using OnException block
    The OnException block is written as a separate block from the routes.
    This applies to all the routes. Below for both route starting with from("file:C:/inputFolder?noop=true") and from("file:C:/inbox?noop=true") the exception handling will be applied.
    package com.javainuse.route;
    
    import org.apache.camel.Exchange;
    import org.apache.camel.Processor;
    import org.apache.camel.builder.RouteBuilder;
    import com.javainuse.exception.CamelCustomException;
    import com.javainuse.processor.MyProcessor;
    
    public class SimpleRouteBuilder extends RouteBuilder {
    
        @Override
        public void configure() throws Exception {
    
            onException(CamelCustomException.class).process(new Processor() {
    
                public void process(Exchange exchange) throws Exception {
                    System.out.println("handling ex");
                }
            }).log("Received body ").handled(true);
    
            from("file:C:/inputFolder?noop=true").process(new MyProcessor()).to("file:C:/outputFolder");
    
            from("file:C:/inbox?noop=true").process(new MyProcessor()).to("file:C:/outputFolder");
        }
    
    }
    
We get the output as follows
camel17_8
So Exception thrown from both routes are handled by the onException Block.

Download Source Code

Download it - Apache Camel Exception Handling Example

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions