Arjun Arjun - 2 months ago 15
Apache Configuration Question

Calling a list of rest api's from a route builder of a apache camel

I want to call a list of restApi's from a route builder when an object is being saved.

The list of rest API hosts will be taken from the properties file for time being I had hard coded it in code itself.

I didn't find any forEach loop to be used in


public class userApiRoute extends BaseApiRoute {

private final Logger log = LoggerFactory.getLogger(getClass());

public void configure() throws Exception {

super.configure(); String hosts=",";
List<String> hostList = Arrays.asList(hosts.split("\\s*,\\s*"));

rest("/api/user").post().description("Updates a User document")
.policy("apiUserPolicy").beanRef("userDefService", "save").choice().when(ifCSV)
.recipientList(constant("http://localhost:8081/api/test-api,http://localhost:8081/api/test-api2,http://localhost:8081/api/test-ap3"),",").aggregationStrategy(new userAggregationStrategy())
//.recipientList(constant("properties:{{api.hostList}}"),",").aggregationStrategy(new userAggregationStrategy())

.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(201)).endRest();
public class UserDefinitionApiNotifier implements Processor {

private final Logger log = LoggerFactory.getLogger(getClass());

public void process(Exchange exchange) throws Exception {

String id = (String) exchange.getIn().getHeader("id");
log.debug("creating message for - {}", id);
JSONObject jsonObject = new JSONObject();
jsonObject.put("RECORD_TYPE", "Config_Update");
jsonObject.put("ID", id);
jsonObject.put("TYPE", "UserDefinition");

if (jsonObject != null) {
exchange.getOut().setHeader(Exchange.HTTP_METHOD, "POST");
exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/json");

public class userAggregationStrategy implements AggregationStrategy {

private final Logger log = LoggerFactory.getLogger(getClass());
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
log.debug("Aggregrator Start" );
if (oldExchange == null) {
log.debug("Aggregrator stop" );
return newExchange;
String oldBody = oldExchange.getIn().getBody(String.class);
String newBody = newExchange.getIn().getBody(String.class);
String newUri = newExchange.getProperty(Exchange.RECIPIENT_LIST_ENDPOINT, String.class);
String oldUri = oldExchange.getProperty(Exchange.RECIPIENT_LIST_ENDPOINT, String.class);
log.debug("Aggregrator old body {}",oldBody );
log.debug("Aggregrator body {}", newBody );
log.debug("Aggregrator newuri {}", newUri );
log.debug("Aggregrator olduri {}", oldUri );
oldExchange.getIn().setBody(oldBody + "+" + newBody);
log.debug("Aggregrator End" );
return oldExchange;


The websites I referred to are httpComponent and loop.


You can actually use recipientList to publish the message in one shot to all your addresses

.recipientList(method(<return your list of addresses>))

but if you want to iterate and push ,

Save the message object that is your message body in a exchange property and apply the split over your list of addresses to which you want to post this object. During each iteration simply publish the message from the property to the address in the body.

.split(method(<return your list of addresses>))
.setProperty("toAddress" , body())

Note : make sure the addresses are formated to match the endpoint structure according to your component