2
votes

I have the following setup of camel routes:

<route id="firstRoute">
    <from uri="..." />

    <!-- This processor puts a list of items as the out body -->
    <process ref="collectItemsProcessor" />

    <!-- Now all items should be processed one by one: -->
    <split>
        <simple>${body}</simple>
        <to uri="direct:secondRoute" />
    </split>
</route>

<route id="secondRoute">
    <from uri="direct:secondRoute" />

    <process ref="itemProcessor" />
</route>

In the itemProcessor I want to count the number of items that were successfully processed by putting a property into the exchange:

exchange.setProperty("PROCESSED_ITEMS", exchange.getProperty("PROCESSED_ITEMS", Integer.class) + 1);

For some reason, on each call to the processor the property is null again. Documentation says:

The Exchange also holds meta-data during its entire lifetime stored as properties accessible using the various getProperty(String) methods.

https://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html

When setting the property initially in the collectItemsProcessor, this value is kept. I suspect that the exchange is copied for each call to the split route, but how can I then really keep "meta-data during the entire lifetime"?

1

1 Answers

1
votes

Split creates a new exchange for each item. The lifetime of this exchange only covers what is inside the split element.

If you only want a counter of the elements processed then simply use the property "CamelSplitIndex". The splitter automatically populates this property.