3

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"?

maxdev
  • 2,491
  • 1
  • 25
  • 50

1 Answers1

5

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.

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • huh? I don't understand what is meant by the entire last paragraph. – Snekse Nov 04 '22 at 21:26
  • I know this is an old question, but for completeness' (?) sake, CS is pointing out that exchanges inside a split block all have an exchange property "CamelSplitIndex" with an incrementing counter, which is from all I can tell exactly the information the OP was looking for. See https://camel.apache.org/components/3.18.x/eips/split-eip.html#_exchange_properties. – Christoph Nov 11 '22 at 14:44
  • Very happy to know that each split message has its own exchange. Do they share the same properties(user/internal) with the main, unsplit exchange? – WesternGun Dec 16 '22 at 13:40