2

I need some help with update of exchange property in Apache Camel.

Use case: I have route which gets some Ids from API endpoint, after that i need to get info for every id from another endpoint.

I need to keep responses somewhere in order to create some JSON array later.

Can someone give me some working route with similar use case or just point me in right direction?

sample route

from("direct:getIds") .setProperty("ValueToUpdate").constant("") 
.to("endpontWhichReturns ids")
.split().jsonpath("$.Data") .log("${property.xrefCode}") 
.toD("getInfoById) .log("${body}") 
.choice() .when(header("CamelHttpResponseCode").isEqualTo("200")) 
.setProperty("body").body() 
.setProperty("updateBody",method("PrepareUpdate","prepare")) 
.aggregate(property("ValueToUpdate"), new Aggreagation()) 
.to("direct:someEndpoint") .end() .to("mock:nestdo11");
Santosh Kumar
  • 77
  • 2
  • 14
djoleb
  • 47
  • 1
  • 7

2 Answers2

5

Hope this simple route will help you:

from("jms://somequeue")
            .split(simple("${body}"), (oldExchange, newExchange) -> {
                Response response = newExchange.getIn().getBody(Response.class);
                LinkedList<Response> responseCollection = oldExchange.getProperty("responseCollection", LinkedList.class);
                if (responseCollection == null) {
                    newExchange.setProperty("responseCollection", new LinkedList<Response>(Collections.singletonList(response)));
                } else {
                    responseCollection.add(response);
                    newExchange.setProperty("responseCollection", responseCollection);
                }
                return newExchange;
            })
            .process(exchange -> {
                String id = exchange.getIn().getBody(String.class);
                Response response = receiveResponse(id);
                exchange.getIn().setBody(response);
            })
            .end()
            .process(exchange -> {
                LinkedList<Response> collection = exchange.getProperty("responseCollection", LinkedList.class);
                //create your json
            });
c0ld
  • 770
  • 4
  • 15
1

You can use Simple. You can use setProperty on the Exchange API.

Steve Huston
  • 1,432
  • 13
  • 20
  • Hello Steve! Let me explain what i actually need. 1.get ids from endpoit. 2.split that response by id 3. Get info by id from another endpoint 4. Update old exchange with response body from #3. I tried to use aggregator, but i didn't manage to append that response body to exchange property. – djoleb Oct 03 '18 at 06:12
  • Hi Djordje. You need to apply some simple route in your question describing your logic, so others can show you how to modify it. – c0ld Oct 03 '18 at 07:18
  • Here is some route. from("direct:getIds") .setProperty("ValueToUpdate").constant("") .to("endpontWhichReturns ids") .split().jsonpath("$.Data") .log("${property.xrefCode}") .toD("getInfoById) .log("${body}") .choice() .when(header("CamelHttpResponseCode").isEqualTo("200")) .setProperty("body").body() .setProperty("updateBody",method("PrepareUpdate","prepare")) .aggregate(property("ValueToUpdate"), new Aggreagation()) .to("direct:someEndpoint") .end() .to("mock:nestdo11"); – djoleb Oct 03 '18 at 07:40
  • Apply to your question in code block not in comment =) Ok, i think i understand what you need look at my answer. – c0ld Oct 03 '18 at 07:47