Search code examples
apachepropertiesapache-camelexchange-server

Apache Camel update exchange property


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");

Solution

  • 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
                });