Search code examples
javaapache-cameleip

Camel pattern for multiple sources specific messages aggregation and redirect to destination


I have one problem, and don't know how to solve it using camel. I searched for related EIP in camel documentation, but without results.

Now I have simple route:

<route id="routeId">
    <from uri="Source1"/>
    <from uri="Source2"/>
    <to   uri="Destination"/>
</route>

Both sources sends JMS messages to Destination and at some point when Source finish its job it send specific end message, with some flag. What I need to do is to collect or count those end messages and send single end message to destination when I receive end messages from both sources. Only when i receive two end messages (imagine that its just simple message with some header flag) then i should send single one to destination.

Sorry if problem explanation isn't clear enough.

Thanks in advance.


Solution

  • the Camel aggregator and filter patterns can be used for this scenario...

    • use a filter to detect "end" messages and route them through an aggregator
    • use a custom aggregation strategy to build up the single end message with a count
    • use a custom completion predicate to trigger the completion message

    something like this...

    from("source1").to("direct:aggregateRoute"); 
    from("source2").to("direct:aggregateRoute"); 
    from("direct:aggregateRoute")
        .filter(header("isEndMessage").isEqualTo("true"))
            .aggregate(constant(true), new MyAggregationStrategy())
            .completionPredicate(new MyCompletionStrategy())
        .to("destination");