Я хочу вызвать список restApi из построителя маршрутов, когда объект сохраняется.Вызов списка отдыха api из создателя маршрута apache camel
Список хостов API доступа будет заимствован из файла свойств на время, когда я был жестко закодирован в самом коде.
я не нашел петлю Foreach для использования в 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;
}
}
Сайты I, упомянутые в httpComponent и loop.
Зачем вам нужен ло оп? Разделите список и вызовите API на основе значения каждого разделения, которое вы можете установить в свойстве заголовка. –
Я хочу перебирать список адресов API и отправлять почтовый запрос каждому Api с настройкой некоторых данных объекта. – Arjun
Вы можете сделать это с помощью шаблона сплиттера. –