Customizing each redelivery attempt

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.

Getting ready

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.

How to do it...

Configure an error handler for redelivering a message, and set its onRedelivery option to reference a Camel processer that you implement.

  1. Implement a 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);
      }
    }
  2. To use the 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") //...
  }
}

Note

If you are using the DefaultErrorHandler function, do not forget that, by default, it does not attempt redeliveries. In order for this recipe to work, you need to set maximumRedeliveries to a value greater than or equal to 1. See the Retrying an operation recipe for more details.

How it works...

Before each redelivery attempt, Camel will call your referenced processor giving you an opportunity to refine the exchange values.

There's more...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.203.96