Reputation: 61
I have a xml where I'm using the split tag to process in Spring DSL. What I'm doing is basically seaching for a value in the xml, when I find this value I need to get the value of another tag, child of the same element and save to a header. This operations seems simple, but I can't recover my headers outside split and I need to. I tried with headers and properties and the result was the same.
Please help me to figure out what I'm doing wrong.
Code sample:
<route>
...
<split>
<xpath>//FatherTag/ChildTag</xpath>
<to uri="direct:processingRoute"/>
</split>
</route>
<route>
<from uri="direct:processingRoute"/>
<choice>
<when>
<simple>....</simple>
<setHeader headerName="foo">
<constant>test</constant>
</setHeader>
</when>
</choice>
</route>
Upvotes: 4
Views: 5109
Reputation: 1
To preserve headers or properties set inside split function aggregation strategy needs to be used, if its simple header and need to avoid aggregation strategy then before split function set an exchange property with ArrayList as value
List<String> list = new ArrayList<>();
list.add("one");
exchange.setProperty("list", list);
inside split function now if you update this property again with one more item to the list you will be able to retrieve it after split function is ended and will be available in each split exchange and parent exchange.
from("direct:main)
.split(body())
.to("direct:sub")
.end().process(ex -> {
System.Out.Println(ex.getProperty("list"));
}).end();
from("direct:sub")
.process(ex -> {
List list = exchange.getProperty(list);
list.add("two");
exchange.setProperty(list);
}).end();
Output: [one,two]
Upvotes: 0
Reputation: 3155
You need to define an AggregationStrategy
. From Camel Splitter:
What the Splitter returns
Camel 2.3 and newer:
The Splitter will by default return the original input message.
For all versions
You can override this by suppling your own strategy as an AggregationStrategy.
Your AggregationStrategy
needs to check the appropriate header set for each child tag and pass it on the resulting Exchange output message for the split operation.
Upvotes: 2