【发布时间】:2016-11-18 06:33:56
【问题描述】:
我想在保存对象时从路由构建器调用 restApi 列表。
其余 API 主机列表将从属性文件中获取,暂时我已将其硬编码在代码本身中。
我没有找到任何用于RouteBuilder 的 forEach 循环
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;
}
}
我提到的网站是httpComponent和loop。
【问题讨论】:
-
为什么需要循环?拆分列表并根据您可以在标题属性中设置的每个拆分的值调用 API
-
我想遍历一个 API 地址列表,并通过设置一些对象数据向每个 Api 发送一个发布请求。
-
您也可以使用拆分器模式来做到这一点。
-
@SoucianceEqdamRashti 我不知道如何将拆分后的 rest api 作为 uri 发送
标签: java apache rest routes apache-camel