Search Tutorials


Understanding Apache Camel Redelivery Policy using example | JavaInUse

Understanding Apache Camel Redelivery policy

In this post we will implement Apache Camel Redelivery policy.
In previous Apache Camel Exception Handling we had written code to implement exception handling.
A redelivery policy defines rules when Camel Error Handler perform redelivery attempts. For example you can setup rules that state how many times to try redelivery, and the delay in between attempts, and so forth. Suppose we have a scenario where an exception is caused due to network issue, we will want to retry such messages again before the exception is handled.

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 Exception Handling will be the starting point.
The project structure will be as follows-

Apache Camel Redelivery Policy Example
We modify the MyProcessor as follows. We check if the input contains the text "test", then an exception is thrown.
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 {
        String a = exchange.getIn().getBody(String.class);
        System.out.println("hello " + a);
        if (a.equalsIgnoreCase("test"))
            throw new CamelCustomException();
    }

}
Our SimpleRoutebuilder class is as before-
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");

    }

}




On running the MainApp.java we get the output as follows-

Apache Camel Redelivery Policy Tutorial
So after the exception is thrown it is caught by the MyException Block.
We will now define the redelivery policy. So now each message will be tried 5 times before being caught.
	<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd          
		http://camel.apache.org/schema/spring 
		http://camel.apache.org/schema/spring/camel-spring.xsd">

	<bean id="routeBuilder" class="com.javainuse.route.SimpleRouteBuilder" />

	<camelContext xmlns="http://camel.apache.org/schema/spring">
		<routeBuilder ref="routeBuilder" />
		<redeliveryPolicyProfile id="testRedeliveryPolicyProfile"
			retryAttemptedLogLevel="WARN" maximumRedeliveries="5"
			redeliveryDelay="5" />
	</camelContext>
</beans>
	
Next configure the defined redelivery policy in our route.
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");
            }
        }).redeliveryPolicyRef("testRedeliveryPolicyProfile").log("Received body ").handled(true);

        from("file:C:/inputFolder?noop=true").process(new MyProcessor()).to("file:C:/outputFolder");

    }

}
On running this the message gets retried 5 times when the exception occurs and after these attempts are exhausted it is finally caught by the catch block.

Apache Camel Redelivery Policy
Next we may want in some cases to correct some data if the exception is thrown first time and before the first redelivery attempt is made. For this make use of onRedelivery processor. Define the MyRedelivery processor which corrects the data before the first redelivery attempt.
package com.javainuse.processor;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class RedeliveryProcessor implements Processor {

    public void process(Exchange exchange) throws Exception {     
        exchange.getIn().setBody("test1");
    }

}
Next in the route we configure the onRedelivery processor.
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;
import com.javainuse.processor.RedeliveryProcessor;

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");
            }
        }).onRedelivery(new RedeliveryProcessor()).redeliveryPolicyRef("testRedeliveryPolicyProfile")
            .log("Received body ").handled(true);

        from("file:C:/inputFolder?noop=true").process(new MyProcessor()).to("file:C:/outputFolder");

    }

}

In the Redelivery Policy bean we have changed the exchange data so the exception will not thrown again.

Apache Camel Redelivery Policy Output

Download Source Code

Download it - Apache Camel Retry Policy Example 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