带有 Json 数组拆分的 Apache Camel [英] Apache Camel with Json Array split
问题描述
我有一个骆驼应用程序,它从一个大小为 13000 的 jms 队列接收一个 json 数组请求,json 数组请求的结构如下.我想使用一组 5 来流式传输和拆分 json 数组.例如,如果我收到一个大小为 100 的 json 数组,我想将其分组为 5 并将其拆分为 20 个请求.是否有内置的骆驼功能来分组和拆分 json 数组,还是我需要编写自定义拆分器?
I have a camel application which receives a json array request from a jms queue upto size 13000,the structure of the json array request is as below. I would like to stream and split the json array with a group of 5. For example if I receive a json array of size 100 I would like to group as 5 and split it as 20 requests. Is there a inbuilt camel functionality to group and split json array or do I need to write a custom splitter?
我使用的是骆驼 2.17 版本.
I'm using camel 2.17 version.
示例 json 数组:
Sample json array:
[{
"name": "Ram",
"email": "ram@gmail.com",
"age": 23
}, {
"name": "Shyam",
"email": "shyam23@gmail.com",
"age": 28
}, {
"name": "John",
"email": "john@gmail.com",
"age": 33
}, {
"name": "Bob",
"email": "bob32@gmail.com",
"age": 41
}, {
"name": "test1",
"email": "test1@gmail.com",
"age": 41
}, {
"name": "test2",
"email": "test2@gmail.com",
"age": 41
}, {
"name": "test3",
"email": "test3@gmail.com",
"age": 41
}, {
"name": "test4",
"email": "test4@gmail.com",
"age": 41
}]
推荐答案
你可以试试这样的:
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start")
.split().jsonpath("$")
.streaming()
.aggregate(AggregationStrategies.groupedExchange())
.constant("true")
.completionSize(5)
.completionTimeout(1000)
.log("${body}")
.to("mock:result");
}
};
}
如果消息的大小不是 5 的倍数,则路由应在聚合前等待 1 秒并继续.使用您的输入,结果将是分别包含 5 项和 3 项的两条消息:
If the message doesn't have a size multiple of five, the route should wait 1 sec before aggregating and go ahead. Using your input, the result will be two messages with 5 and 3 items respectively:
INFO 5419 --- [ main] route1 : List<Exchange>(5 elements)
INFO 5419 --- [eTimeoutChecker] route1 : List<Exchange>(3 elements)
可以查看完整示例在这里.
根据要求,一个 Spring DSL 示例:
As requested, a Spring DSL example:
<camel:route>
<camel:from uri="direct:start" />
<camel:split streaming="true">
<camel:jsonpath>$</camel:jsonpath>
<camel:aggregate completionSize="5"
completionTimeout="1000" groupExchanges="true">
<camel:correlationExpression>
<camel:constant>true</camel:constant>
</camel:correlationExpression>
<camel:log message="${body}"></camel:log>
<camel:to uri="mock:result"></camel:to>
</camel:aggregate>
</camel:split>
</camel:route>
这篇关于带有 Json 数组拆分的 Apache Camel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!