Camel allows you to register a Processor instance with the redelivery policy that can change any part of the message and its headers before each redelivery attempt. This can help if there are ways you can tweak the message in response to certain errors.
This recipe will show you how to change the contents of the exchange during each redelivery attempt.
The Java code for this recipe is located in the org.camelcookbook.error.retrycustom
package. The Spring XML files are located under src/main/resources/META-INF/spring
and prefixed with retrycustom
.
Configure an error handler for redelivering a message, and set its onRedelivery
option to reference a Camel processer that you implement.
org.apache.camel.Processor
that will get called to process the message before each redelivery attempt:import org.apache.camel.Exchange; import org.apache.camel.Processor; public class RetryCustomProcessor implements Processor { @Override public void process(Exchange exchange) { exchange.setProperty("optimizeBit", true); } }
RetryCustomProcessor
function within the XML DSL, register it using the Spring bean
element, and refer to its id
through the onRedeliveryRef
attribute of an errorHandler
element, either within the camelContext
or route
element (see the Dead Letter Channel – handle errors later recipe):<bean id="myRetryProcessor" class="org.camelcookbook.error.retrycustom.RetryCustomProcessor"/> <camelContext errorHandlerRef="myErrorHandler" xmlns="http://camel.apache.org/schema/spring"> <errorHandler id="myErrorHandler" type="DefaultErrorHandler" onRedeliveryRef="myRetryProcessor"> <redeliveryPolicy maximumRedeliveries="2"/> </errorHandler> <!-- ... --> </camelContext>
In the Java DSL, within RouteBuilder.configure()
method define an errorHandler
instance, and using its fluent API, set the onRedelivery
attribute to an instance of a Processor:
public class RetryCustomRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
errorHandler(
defaultErrorHandler()
.onRedelivery(new RetryCustomProcessor())
.maximumRedeliveries(2)
);
from("direct:start") //...
}
}
Before each redelivery attempt, Camel will call your referenced processor giving you an opportunity to refine the exchange values.
You can combine this recipe with the Conditional retry recipe, allowing you both to have access to the message before each redelivery attempt, and to control exactly when to stop trying to redeliver.
You can also set route-specific error handlers. See the Dead Letter Channel – handle errors later recipe.
3.143.203.96