0

I need to iterate over a list of items in a route. And inside that route I again have to iterate over a list of items. I'm using loop for that, and for getting to know the current iteration using the CamelLoopIndex property.

The problem here is, suppose the outer loop has 50 items to iterate over and the inner loop has 3 items to iterate over. What happens is, after the first iteration of the outer loop it directly skips to the fourth iteration as the inner loop sets the CamelLoopIndex to 2.

I've done a workaround for this:

<outerLoop>
    <!-- do some stuff -->

    <!-- set the CamelLoopIndex value in a custom exchange property -->
    <innerLoop>
        <!-- do some other stuff -->
    </innerloop>
    <!-- reset the CamelLoopIndex value with the one in our custom exchange property -->

</outerloop>

Is there any better way to do this?

arshad.xyz
  • 21
  • 1
  • 3
  • 1
    What are you doing inside of the loops? If you are e.g. iterating over, say, each in the outer loop and then each of said order in the inside loop you could use the splitter EIP instead and then aggregate the messages together again afterwards using an aggregation strategy. http://camel.apache.org/splitter.html – Erik Karlstrand Feb 17 '17 at 12:06
  • Could you share your actual route? – Ralf Feb 17 '17 at 13:12
  • @noMad17 Thanks for suggesting the splitter EIP. I didn't know that camel splitter could be used both for serial as well as parallel processing, not just parallel. I'll try it out! As far as what I'm trying to do is, reading a file which contains a number of records. And for each record, I need to do a bunch of service calls and one of the calls has to be done recursively for a no. of times(Thus nested loops). The records need to be read and processed in an ordered fashion, this lead me to believe I can't use splitter here and so I chose to go with loop – arshad.xyz Feb 18 '17 at 18:17
  • Well, then it sounds as if the outer loop could be replaced by the splitter and the inside processing could probably be done by one or more processors that you can write yourself in Java. – Erik Karlstrand Feb 19 '17 at 22:22

0 Answers0