Apache骆驼 - 拆分列表,调用API并合并身体
我是Apache Camel的新手,我想问几件事。基本上,我想根据我拥有的资源ID列表来调用API,并将API的所有响应组合到一个消息中。我已经尝试了聚合策略,但似乎并没有起作用。消息主体总是返回API的最后一个响应,并且不会保留旧列表。如果您能给我一些例子,我将非常感谢。
路线
from("file:C:\\Inbound")
//Get resourceID list and split it to call the API
.split(simple("${body}"))
.aggregationStrategy(new ArrayListAggregationStrategy())
.toD("{{api_url}}${body.resourceID}")
.log("After API call ${body}")
.end();
聚合策略
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
Object newBody = newExchange.getIn().getBody();
ArrayList<Object> list = null;
if (oldExchange == null) {
list = new ArrayList<Object>();
list.add(newBody);
newExchange.getIn().setBody(list);
return newExchange;
} else {
list = oldExchange.getIn().getBody(ArrayList.class);
list.add(newBody);
return oldExchange;
}}}
I am new to Apache Camel and I would like to ask a few things. Basically, I want to call the API based on the resource ID list I have and combine all the responses from the API into one message. I have already tried AggregationStrategy, but it doesn't seem to be working. The message body always returns the last response from API and it doesn't keep the old list. I would be very grateful if you could give me some examples.
Route
from("file:C:\\Inbound")
//Get resourceID list and split it to call the API
.split(simple("${body}"))
.aggregationStrategy(new ArrayListAggregationStrategy())
.toD("{{api_url}}${body.resourceID}")
.log("After API call ${body}")
.end();
AggregationStrategy
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
Object newBody = newExchange.getIn().getBody();
ArrayList<Object> list = null;
if (oldExchange == null) {
list = new ArrayList<Object>();
list.add(newBody);
newExchange.getIn().setBody(list);
return newExchange;
} else {
list = oldExchange.getIn().getBody(ArrayList.class);
list.add(newBody);
return oldExchange;
}}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在下面,您将找到一个基本示例,如何使用
gentrategy
返回Javalist
的gentrategy
实现.split
。适用于您的特定情况应该很简单。doutes.java
gentregator.java
执行后,记录了以下预期结果:
注意是
Java
list> list
已自动转换为字符串通过.log
。Below you'll find a basic example how to implement
.split
withAggregationStrategy
that returns a JavaList
. It should be straightforward to apply to your specific scenario.Routes.java
Aggregator.java
When executed the following expected outcome is logged:
Note that
Is a Java
List
that has been converted to a string automatically by.log
.如果您想在拆分之后查看结果消息,则必须在
.log(...)
语句 .end> .end> .end()'split&amp;加入'过程。为了更好的清晰度,我建议您缩进您的代码,例如:
If you want to see the resulting message AFTER the split, you have to put your
.log(...)
statement after the.end()
of the 'split & join' process.For better clarity, I recommend you to indent your code, eg: