【发布时间】:2016-10-30 23:34:04
【问题描述】:
我有一个 Camel 应用程序,我正在尝试根据来自不同响应(REST Web 服务)的一些输出进行一些聚合。
这是我目前所拥有的(骆驼路线):
@Component
public final class AggregationRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
rest("/aggregation")
.get()
.to("direct:retrieve");
from("direct:retrieve")
.multicast(/*new BodyAggregationStrategy(), true*/)
.to("direct:foo")
.to("direct:foo1");
from("direct:foo")
.to("seda:http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?sector=conus")
.to("direct:aggregate");
from("direct:foo1")
.to("seda:http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdXMLclient.php?sector=conus")
.to("direct:aggregate");
from("direct:aggregate")
.aggregate(header("id"), new BodyAggregationStrategy())
.log(LoggingLevel.WARN, simple("${body}").getText());
}
}
...“聚合策略”
public final class BodyAggregationStrategy implements AggregationStrategy {
@Override
public Exchange aggregate(final Exchange oldExchange, final Exchange newExchange) {
if (null == oldExchange) {
return newExchange;
}
String oldBody = oldExchange.getIn().getBody(String.class);
String newBody = newExchange.getIn().getBody(String.class);
oldExchange.getIn().setBody(oldBody + "+" + newBody);
return oldExchange;
}
}
...最终,Web 服务会有所不同,但我现在只是想看看能否先解决这个基本问题。
我定义了一个 REST 端点,当GET /aggregation 被点击时(在我这边),我想咨询两个或多个 REST Web 服务并汇总来自这些服务的响应;然后“回答”回来。
有什么线索吗?
【问题讨论】:
-
看看分散-聚集模式camel.apache.org/scatter-gather.html
-
我同意@Itsallas 和 Claus 的观点,如果您的休息调用将是连续的,那么请考虑将每个响应存储在标头/属性中,然后使用简单的处理器聚合值。跨度>
标签: java rest apache-camel integration