Apache Camel with Json Array split

Selva Shanmugam picture Selva Shanmugam · Dec 13, 2017 · Viewed 7.5k times · Source

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?

I'm using camel 2.17 version.

Sample json array:

[{
    "name": "Ram",
    "email": "[email protected]",
    "age": 23
 }, {
    "name": "Shyam",
    "email": "[email protected]",
    "age": 28
 }, {
    "name": "John",
    "email": "[email protected]",
    "age": 33
 }, {
    "name": "Bob",
    "email": "[email protected]",
    "age": 41
 }, {
    "name": "test1",
    "email": "[email protected]",
    "age": 41
 }, {
    "name": "test2",
    "email": "[email protected]",
    "age": 41
 }, {
    "name": "test3",
    "email": "[email protected]",
    "age": 41
 }, {
    "name": "test4",
    "email": "[email protected]",
    "age": 41
}]

Answer

Ricardo Zanini picture Ricardo Zanini · Dec 14, 2017

You could try something like this:

@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");
        }
    };
}

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) 

A full sample could be viewed in here.

EDIT:

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>