Understanding Apache Camel Exception Handling using Example
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-
- Using Do Try block
- Using OnException block
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(); } }
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"); } }