Peter
Peter

Reputation: 620

Infinite loop in Apache Camel

I need to create a route, which periodically calls some process with a small delay between iterations.

When I look at documentation for looping:

The Loop allows for processing a message a number of times, possibly in a different way for each iteration. Useful mostly during testing.

So this is not useful for me, since I need to do an infinite loop (without the CamelLoopSize explicitly specified).

My second idea was using kind of a "recursion":

from("direct:updateLoop").routeId("updateLoop")
  .process(someProcess)
  ...
  .filter(someFilter)  // Can be used to stop the workflow
  .delay(18000000)  // Wait 5 hours and start again
  .to("direct:updateLoop")

This works well for a few days, however after about 600 iterations, this fails with StackOverflowException

Is there a better way to run my process in an infinite loop?

Upvotes: 2

Views: 2617

Answers (2)

Ben Keil
Ben Keil

Reputation: 1183

For recursion you can use camel's seda component.

from("seda:updateLoop").routeId("updateLoop")
    .process(someProcess)
    ...
    .filter(someFilter)  // Can be used to stop the workflow
    .delay(18000000)  // Wait 5 hours and start again
    .to("seda:updateLoop");

Upvotes: 2

Peter Keller
Peter Keller

Reputation: 7636

Use Camel Timer component:

from("timer://foo?fixedRate=false&period=18000000")
     .process(someProcess);

If fixedRate is false, then no overlapping will occur, see Apache Camel timer: "period" vs "fixedRate"

Upvotes: 4

Related Questions