Apache骆驼 - 拆分列表,调用API并合并身体

发布于 2025-02-02 14:38:56 字数 884 浏览 3 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2025-02-09 14:38:56

在下面,您将找到一个基本示例,如何使用gentrategy返回Java listgentrategy实现.split。适用于您的特定情况应该很简单。

doutes.java

import org.apache.camel.builder.RouteBuilder;

public class Routes {
    public static RouteBuilder routes() {
        return
            new RouteBuilder() {
                public void configure() {
                    from("timer:mainRouteTrigger?repeatCount=1")
                    .routeId("MainRouteTrigger")
                        .setBody(constant("100\n200\n300"))
                        .to("direct:mainRoute")
                    .end()
                    ;

                    from("direct:mainRoute")
                    .routeId("MainRoute")
                        .log("MainRoute BEGINS: BODY: ${body}")
                        .split(body().tokenize("\n"), new Aggregator())
                            .to("direct:singleRoute")
                        .end()
                        .log("MainRoute ENDS: BODY: ${body}")
                    .end()
                    ;

                    from("direct:singleRoute")
                    .routeId("SingleRoute")
                        .log("SingleRoute BEGINS: BODY: ${body}")
                        .setBody(simple("this is a response for id ${body}"))
                        .log("SingleRoute ENDS: BODY: ${body}")
                    .end()
                    ;
                }
            };
    }
}

gentregator.java

import org.apache.camel.AggregationStrategy;
import org.apache.camel.Exchange;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Aggregator implements AggregationStrategy {
    @Override
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        // first iteration
        // oldExchange is null
        // body of newExchange is String
        if (oldExchange == null) {
            List<String> newList = buildListFrom(newExchange);
            newExchange.getMessage().setBody(newList, List.class);
            return newExchange;
        }

        // second and subsequent iterations
        // body of oldExchange is List<String>
        // body of newExchange is String
        List<String> oldList = oldExchange.getMessage().getBody(List.class);
        List<String> newList = buildListFrom(newExchange);

        List<String> combined = Stream.concat(oldList.stream(), newList.stream()).collect(Collectors.toList());
        oldExchange.getMessage().setBody(combined);

        return oldExchange;
    }

    private List<String> buildListFrom(Exchange exchange) {
        String body = exchange.getMessage().getBody(String.class);
        List<String> list = new ArrayList<String>();
        list.add(body);
        return list;
    }
}

执行后,记录了以下预期结果:

AbstractCamelContext  INFO  Routes startup (total:3 started:3)
AbstractCamelContext  INFO      Started MainRouteTrigger (timer://mainRouteTrigger)
AbstractCamelContext  INFO      Started MainRoute (direct://mainRoute)
AbstractCamelContext  INFO      Started SingleRoute (direct://singleRoute)
AbstractCamelContext  INFO  Apache Camel 3.14.2 (camel-1) started in 133ms (build:14ms init:105ms start:14ms)
MainSupport           INFO  Waiting until complete: Duration max 5 seconds
MainRoute             INFO  MainRoute BEGINS: BODY: 100
200
300
SingleRoute           INFO  SingleRoute BEGINS: BODY: 100
SingleRoute           INFO  SingleRoute ENDS: BODY: this is a response for id 100
SingleRoute           INFO  SingleRoute BEGINS: BODY: 200
SingleRoute           INFO  SingleRoute ENDS: BODY: this is a response for id 200
SingleRoute           INFO  SingleRoute BEGINS: BODY: 300
SingleRoute           INFO  SingleRoute ENDS: BODY: this is a response for id 300
MainRoute             INFO  MainRoute ENDS: BODY: [this is a response for id 100, this is a response for id 200, this is a response for id 300]
MainSupport           INFO  Duration max seconds triggering shutdown of the JVM
AbstractCamelContext  INFO  Apache Camel 3.14.2 (camel-1) shutting down (timeout:45s)
AbstractCamelContext  INFO  Routes stopped (total:3 stopped:3)
AbstractCamelContext  INFO      Stopped SingleRoute (direct://singleRoute)
AbstractCamelContext  INFO      Stopped MainRoute (direct://mainRoute)
AbstractCamelContext  INFO      Stopped MainRouteTrigger (timer://mainRouteTrigger)
AbstractCamelContext  INFO  Apache Camel 3.14.2 (camel-1) shutdown in 34ms (uptime:5s54ms)

注意是

[this is a response for id 100, this is a response for id 200, this is a response for id 300]

Java list> list已自动转换为字符串通过.log

Below you'll find a basic example how to implement .split with AggregationStrategy that returns a Java List. It should be straightforward to apply to your specific scenario.

Routes.java

import org.apache.camel.builder.RouteBuilder;

public class Routes {
    public static RouteBuilder routes() {
        return
            new RouteBuilder() {
                public void configure() {
                    from("timer:mainRouteTrigger?repeatCount=1")
                    .routeId("MainRouteTrigger")
                        .setBody(constant("100\n200\n300"))
                        .to("direct:mainRoute")
                    .end()
                    ;

                    from("direct:mainRoute")
                    .routeId("MainRoute")
                        .log("MainRoute BEGINS: BODY: ${body}")
                        .split(body().tokenize("\n"), new Aggregator())
                            .to("direct:singleRoute")
                        .end()
                        .log("MainRoute ENDS: BODY: ${body}")
                    .end()
                    ;

                    from("direct:singleRoute")
                    .routeId("SingleRoute")
                        .log("SingleRoute BEGINS: BODY: ${body}")
                        .setBody(simple("this is a response for id ${body}"))
                        .log("SingleRoute ENDS: BODY: ${body}")
                    .end()
                    ;
                }
            };
    }
}

Aggregator.java

import org.apache.camel.AggregationStrategy;
import org.apache.camel.Exchange;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Aggregator implements AggregationStrategy {
    @Override
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        // first iteration
        // oldExchange is null
        // body of newExchange is String
        if (oldExchange == null) {
            List<String> newList = buildListFrom(newExchange);
            newExchange.getMessage().setBody(newList, List.class);
            return newExchange;
        }

        // second and subsequent iterations
        // body of oldExchange is List<String>
        // body of newExchange is String
        List<String> oldList = oldExchange.getMessage().getBody(List.class);
        List<String> newList = buildListFrom(newExchange);

        List<String> combined = Stream.concat(oldList.stream(), newList.stream()).collect(Collectors.toList());
        oldExchange.getMessage().setBody(combined);

        return oldExchange;
    }

    private List<String> buildListFrom(Exchange exchange) {
        String body = exchange.getMessage().getBody(String.class);
        List<String> list = new ArrayList<String>();
        list.add(body);
        return list;
    }
}

When executed the following expected outcome is logged:

AbstractCamelContext  INFO  Routes startup (total:3 started:3)
AbstractCamelContext  INFO      Started MainRouteTrigger (timer://mainRouteTrigger)
AbstractCamelContext  INFO      Started MainRoute (direct://mainRoute)
AbstractCamelContext  INFO      Started SingleRoute (direct://singleRoute)
AbstractCamelContext  INFO  Apache Camel 3.14.2 (camel-1) started in 133ms (build:14ms init:105ms start:14ms)
MainSupport           INFO  Waiting until complete: Duration max 5 seconds
MainRoute             INFO  MainRoute BEGINS: BODY: 100
200
300
SingleRoute           INFO  SingleRoute BEGINS: BODY: 100
SingleRoute           INFO  SingleRoute ENDS: BODY: this is a response for id 100
SingleRoute           INFO  SingleRoute BEGINS: BODY: 200
SingleRoute           INFO  SingleRoute ENDS: BODY: this is a response for id 200
SingleRoute           INFO  SingleRoute BEGINS: BODY: 300
SingleRoute           INFO  SingleRoute ENDS: BODY: this is a response for id 300
MainRoute             INFO  MainRoute ENDS: BODY: [this is a response for id 100, this is a response for id 200, this is a response for id 300]
MainSupport           INFO  Duration max seconds triggering shutdown of the JVM
AbstractCamelContext  INFO  Apache Camel 3.14.2 (camel-1) shutting down (timeout:45s)
AbstractCamelContext  INFO  Routes stopped (total:3 stopped:3)
AbstractCamelContext  INFO      Stopped SingleRoute (direct://singleRoute)
AbstractCamelContext  INFO      Stopped MainRoute (direct://mainRoute)
AbstractCamelContext  INFO      Stopped MainRouteTrigger (timer://mainRouteTrigger)
AbstractCamelContext  INFO  Apache Camel 3.14.2 (camel-1) shutdown in 34ms (uptime:5s54ms)

Note that

[this is a response for id 100, this is a response for id 200, this is a response for id 300]

Is a Java List that has been converted to a string automatically by .log.

ι不睡觉的鱼゛ 2025-02-09 14:38:56

如果您想在拆分之后查看结果消息,则必须在.log(...)语句 .end> .end> .end()'split&amp;加入'过程。

为了更好的清晰度,我建议您缩进您的代码,例如:

.split(simple("${body}"))
  .aggregationStrategy(new ArrayListAggregationStrategy())
  .toD("{{api_url}}${body.resourceID}")
  .log("Individual API call response: ${body}")
.end()
.log("Complete aggregation result: ${body}");

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:

.split(simple("${body}"))
  .aggregationStrategy(new ArrayListAggregationStrategy())
  .toD("{{api_url}}${body.resourceID}")
  .log("Individual API call response: ${body}")
.end()
.log("Complete aggregation result: ${body}");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文