Arjun Arjun - 10 days ago 5
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

RouteBuilder


public class userApiRoute extends BaseApiRoute {

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

@Override
public void configure() throws Exception {

super.configure(); String hosts="http://127.0.0.1:10080/api.notification,http://123.123.123.123:10080";
List<String> hostList = Arrays.asList(hosts.split("\\s*,\\s*"));

rest("/api/user").post().description("Updates a User document")
.type(User.class).consumes("application/json").route().routeId("UpdateUser").to("authService")
.policy("apiUserPolicy").beanRef("userDefService", "save").choice().when(ifCSV)
.to("userDefinitionFileNotifier").marshal(csv)
.to("file://{{user.notification.location}}").when(ifMessaging)
.to("userDefinitionMessageNotifier").wireTap("jms:queue:{{user.notification.location}}").
.to("userDefinitionApiNotifier")
.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();
}
}
@Component("userDefinitionApiNotifier")
public class UserDefinitionApiNotifier implements Processor {

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

@Override
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");
exchange.getOut().setBody(jsonObject.toString());
}

}
public class userAggregationStrategy implements AggregationStrategy {

private final Logger log = LoggerFactory.getLogger(getClass());
@Override
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.

Answer

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.

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

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

Comments