'How to call multiple routes sequentially without messing with body

I have something like the following:

.from("direct:start")
    .to("direct:a")
    .to("direct:b")
    .to("direct:c");

The issue I have is direct:a modifies the body and then passes that modified body to b and c. I want a, b, and c to all get the same body. I was able to achieve this by doing .multicast():

.from("direct:start")
    .multicast()
    .to("direct:a",
        "direct:b",
        "direct:c");

However, it appears the .to(...) blocks are not executing in sequential order anymore (even though I am not specifying .parallelProcessing()) and also the multicast is messing up my tx policy. I cannot find anything that exactly fits this need. A wireTap would achieve this I think, but that is async and I need synchronous and sequential. I have even tried brute force saving the body to a property against the exchange and then restoring it and that does not seem to work.



Solution 1:[1]

You can use enrich for this:

from("direct:start")
    .enrich("direct:a", AggregationStrategies.useOriginal(true))
    .enrich("direct:b", AggregationStrategies.useOriginal(true))
    .enrich("direct:c", AggregationStrategies.useOriginal(true));

The true argument there means that exceptions thrown by your child routes will be propagated to the main route. If you do not want that, just pass false or omit the argument altogether.

Solution 2:[2]


You Should use the Wire Tap:

from("direct:start")
    .wireTap("direct:a")
    .wireTap("direct:b")
    .wireTap("direct:c");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Laurent Chabot
Solution 2 Sergii Pozharov