'how to repeat a task maximum five times using apache camel

I want to repeat a task (calling external API) till either of following condition is satisfied.

  • Response code is success(200)
  • Tried 5 times.

For repeating a task we can use loop in camel. Unfortunately loop does not support conditions till camel version 2.17 and I have some restrictions that force me to use an older version of camel.

How can I solve above problem using apache camel?



Solution 1:[1]

You can use direct like unconditional jump goto. Here is an example:

    from("direct:executeHttpRequest").routeId("executeHttpRequest")
            .to("http4://test.com?throwExceptionOnFailure=false&httpClientConfigurerRef=#customHttpConfigurator" + ((proxySettings == null || "none".equals(proxySettings)) ? "" : "&" + proxySettings))//?httpClientConfigurerRef=#customHttpConfigurator
            .log("HTTP response code: ${in.header.CamelHttpResponseCode}")
            .choice()
            .when(PredicateBuilder.and(header(Exchange.HTTP_RESPONSE_CODE).isNotNull(),
                    constant(HttpStatus.SC_OK).isEqualTo(header(Exchange.HTTP_RESPONSE_CODE))))
                    .log(LoggingLevel.INFO, logger, "200 is OK!")
                    .removeHeader(Exchange.HTTP_RESPONSE_CODE)
                    .removeHeader("LoopIndex")
                    //Something useful here
            .otherwise()
                .log(LoggingLevel.INFO, logger, "Repeat request, waiting ${header.DELAY} millis...")
                .delay(5000)
                .removeHeader(Exchange.HTTP_RESPONSE_CODE)
                .process(new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                            int ind = (exchange.getIn().getHeader("LoopIndex") == null ? 0 : exchange.getIn().getHeader("LoopIndex", Integer.class));
                            ind++;
                            exchange.getIn().setHeader("LoopIndex", ind);
                            if (ind >= repeatCount) {
                               throw new RuntimeCamelException("Server not response without error " + repeatCount+" time ");
                            }
                        }
                })
                .to("direct:executeHttpRequest")
            .end();
}

Look at work with direct:executeHttpRequest. Looks a bit horrible, but it works.

Solution 2:[2]

I would make use of Camels error handling & redelivery - http://camel.apache.org/redeliverypolicy.html.

You might need to throw exceptions yourself, depending on how the endpoint used to access the API deals with non 200 responses. The throwExceptionOnFailure setting of the HTTP component (http://camel.apache.org/http.html) might do what you want (though I suspect it would consider any 2xx or 3xx response a success, rather then your very strict 200 only requirement)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Paul M