javaxmlapache-camelspring-dslspring-camel

how to iterate through an arraylist inside apache camel route , xml?


I want to iterate through a java arraylist passed as message header to a camel route via bean so that each string item which is basically an url can be passed as uri argument inside tag in camel route.

I am passing an array list as message header to camel route through java bean as follows

ArrayList<String> list=new ArrayList<String>();//Creating arraylist  
              list.add("http://www.google.com");//Adding object in arraylist  
              list.add("http://www.stackoverflow.com");  
              list.add("http://www.tutorialspoint.com");  
              list.add("http://localhost:8080/sampleExample/query"); 
                exchange.getOut().setHeader("endpoints",list);

and, inside camel route i want to iterate through this list and retrieve each list item one by one so that i can pass these items in uri. here's my camel route:

<route id="myroute">
        <from id="sedp" uri="cxfrs:http://{{env:POC_HOST}}/{{env:POC_PATH}}"/>
        <log id="_log1" message="Received query request from consumers"/>
        <bean beanType="com.company.myapp.poc.logic.ProcessRequest"
            id="queryProcessor" method="checkRequestType"/>

          // I want to iterate through the list here as <toD uri="${header.endpoints.item}" />


    </route>

But i am not able to iterate through each item in list recieved as header.endpoints inside camel route.


Solution

  • This is the recipient list EIP pattern than can send messages to N+ destinations: http://camel.apache.org/recipient-list.html

    The recipient list EIP is basically a toD but it can do 1..N endpoints. Where as toD can only do 1.

    And it should be able to take your message header as-is, eg a List or Collection and send to each destination.

    So do

    <recipientList>
      <header>endpoints</header>
    </recipientList>