Sergey
Sergey

Reputation: 191

Is it possible to use Camel loop for Java objects?

I use Spring+Camel and Java application and I can't understand how to use Loop in Camel config. Camel doc suggests:

<route>
  <from uri="direct:b"/>
  <loop>
    <header>loop</header>
    <to uri="mock:result"/>
  </loop>
</route>

How to adjust loop for my case?

<route>
    <from uri="myjms:queue:{{myqueue.name1}}"/>
    ...
    <method bean="myProcessor" method="getSomeMyObjects"> <!-- returns Collection<MyObject> -->
    <loop>
        <header>?????</header> <!-- get single MyObject?.. how???.. -->
        <to uri="myjms:queue:{{myqueue.name2}}"/>
    </loop>
</rout>

Inside bean:

<bean id="myProcessor" class="my.package.MyProcessor">

I've implemented the following methods:

getSomeMyObjects()         - returns Collection<MyObject>;
getSomeMyObject(int index) - returns single MyObject;
getSomeMyObjectsCount()    - returns the number of objects inside Collection<MyObject>;

and can implement any other methods if necessary.

Is it possible to solve this using loop in Camel config?

Upvotes: 2

Views: 3094

Answers (1)

valerii ryzhuk
valerii ryzhuk

Reputation: 174

You do not need to use loop. You need to split each MyObject in different body and send them. Use splitter pattern.

<route>
    <from uri="myjms:queue:{{myqueue.name1}}"/>
    ...
    <method bean="myProcessor" method="getSomeMyObjects"> <!-- returns Collection<MyObject> -->
    <split>
        <simple>${body}</simple>
        <to uri="myjms:queue:{{myqueue.name2}}"/>
    </split>
</rout>

Upvotes: 5

Related Questions