3

I want to exit from Camel Loop at some condition. I am using Camel 2.15

 .loop(simple("${exchangeProperty.loopCount}"))
   .to("bean:xxx?method=exitFromLoop")
 .end()

I tried changing CamelLoopIndex and CamelLoopSize in exitFromLoop method, but didn't worked for me.

Is there any alternative to this e.g using choice/when?

рüффп
  • 5,172
  • 34
  • 67
  • 113
Honey Goyal
  • 445
  • 4
  • 22

2 Answers2

4

There is a loop while mode if you use Camel 2.17 onwards.

See the documentation: http://camel.apache.org/loop

Then the loop runs like a while loop until the predicate evaluates to false / null or zero.

As its a predicate it does not automatic decrement a counter, so its like a while loop in java.

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • Great. Thanks Claus. i am using Fuse 6.2, it use Camel 2.15. Is there any alternative in Camel 2.15? – Honey Goyal Aug 09 '16 at 14:40
  • 1
    You can use dynamic router EIP instead - Or if you do not need to do any more routing after the loop you can mark the exchange to stop – Claus Ibsen Aug 09 '16 at 15:54
0

Up to camel 2.16 (and in 2.17 as well) you can use a <camel:stop/> statement in a <camel:when> block inside the <camel:loop>, and set the loop counter to some arbitrary upper limit.

Alternatively your loop may contain a java processor that can conditionally stop the route with: exchange.setProperty(Exchange.ROUTE_STOP, Boolean.TRUE);

Bernard Hauzeur
  • 2,317
  • 1
  • 18
  • 25