You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "Davis, Daniel" <Da...@si.edu> on 2015/05/15 18:50:34 UTC

Implementing a counter in XML DSL and Simple

>From Claus' 2011 blog I tried to implement a counter but I cannot get it
to work inside a splitter (I need a count of only a subset of items).

The example code is:

<setHeader header="myCounter">
     <simple>${header.myCounter}++</simple>
</setHeader>

By itself this code fails because myCounter is not a number.

So I initialized it outside the Split but inside the same route.

<setHeader headerName="ImageIndex">
     <simple resultType="java.lang.Integer">0</simple>
</setHeader>

No exceptions but it increments once and only once inside the split.  So
I simplified and removed the initialization.

<setHeader headerName="ImageIndex">
     <simple resultType="java.lang.Integer">${header.ImageIndex}++</simple>
</setHeader>

No exceptions but it also increments once and only once inside the
split.  It seems like it is grabbing the original header value off the
exchange each time and incrementing it.

Does anyone see what I am missing?  Is split behavior in some way
causing this?

--
Dan Davis




Re: Implementing a counter in XML DSL and Simple

Posted by Andrew Block <an...@gmail.com>.
Dan,

Each exchange executing within the split is independent of each other. Incrementing the value of a header on one split exchange will have no effect on another. 

If you want to get the number of split items, it is available after the split in Exchange.CamelSplitSize header. 

In addition, take a look at the other headers available within the splitter component [1] documentation for more options available

- Andy

[1] - http://camel.apache.org/splitter.html

-- 
Andrew Block

On May 15, 2015 at 11:50:51 AM, Davis, Daniel (davisda@si.edu) wrote:

From Claus' 2011 blog I tried to implement a counter but I cannot get it
to work inside a splitter (I need a count of only a subset of items).

The example code is:

<setHeader header="myCounter">
<simple>${header.myCounter}++</simple>
</setHeader>

By itself this code fails because myCounter is not a number.

So I initialized it outside the Split but inside the same route.

<setHeader headerName="ImageIndex">
<simple resultType="java.lang.Integer">0</simple>
</setHeader>

No exceptions but it increments once and only once inside the split. So
I simplified and removed the initialization.

<setHeader headerName="ImageIndex">
<simple resultType="java.lang.Integer">${header.ImageIndex}++</simple>
</setHeader>

No exceptions but it also increments once and only once inside the
split. It seems like it is grabbing the original header value off the
exchange each time and incrementing it.

Does anyone see what I am missing? Is split behavior in some way
causing this?

--
Dan Davis