带有Json Array拆分的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数组:
[{
"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 Array拆分的Apache Camel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!