2

I am new to Apache Camel and I am having problems understanding how to implement a simple integration task:

So the use case is

  1. Get list identifiers from request body ${body.ids} contains the list of my id on which i want to iterate. Example : 1, 3, 65, 6 How to iterate over these ( as in java foreach ) ?
  2. Loop over my identifiers list and call the endpoint with the current identifier; e.g a get person/{id} AND add the response Person to my person collections. Which EIP to use to collect each response of rest call and aggregate them into a list ?
  3. Transform my collection of person to another type.
  4. Return the response

Thanks for your help

Pracede
  • 4,226
  • 16
  • 65
  • 110

2 Answers2

2

You have to write a Camel Route that listens for requests and does the following

  1. Iterate over IDs with Camel Split EIP
  2. Make HTTP calls, for example with Camel HTTP 4
  3. Merge the HTTP responses with your message body with Enrich EIP
  4. Use Aggregate EIP to collect multiple parts, in your case where you want to re-collect the former splitted parts, you can use Split-Aggregate

If you are new to Camel, get a copy of Camel in Action 2nd edition. It takes you step by step from the basics to advanced topics.

burki
  • 6,741
  • 1
  • 15
  • 31
  • thanks for detailed list first of all. How could send requests based on previous ones? – ddsultan Dec 16 '20 at 22:52
  • Hello @ddsultan Please ask a new question with more details and tag it with apache-camel. It is impossible to answer to a one-sentence-question - except when the answer "it depends" is OK :-) – burki Dec 17 '20 at 06:55
  • thank you for your feedback. I have post a separate [question](https://stackoverflow.com/questions/65338202/sending-multiple-http-requests-with-apache-camel) – ddsultan Dec 17 '20 at 09:47
0

Here is example:(Which is to call Rest API in loop and prints response - using camel spring-DSL & groovy script)

 <loop doWhile="true">
   <simple>${property.loopCounter} < ${body.ids} </simple>
    <script>
        <groovy><![CDATA[
        def loopCounter = exchange.getProperty("loopCounter")
        exchange.setProperty("loopCounter", loopCounter + 1)
    ]]></groovy>
    </script>
    <setHeader headerName="Exchange.HTTP_URI">
        <simple>https://${property.host}/api/<your api endpoint url complete here!!}</simple>
    </setHeader>
    <toD uri="http4:host"/>
    <script>
        <groovy><![CDATA[
                import groovy.json.*;
                def response = new JsonSlurper().parseText(exchange.getIn().getBody(String.class))
                println(" Response is :"+response));
                ]]></groovy>
    </script>
</loop>
Hpalle
  • 16
  • 2