François Beausoleil
François Beausoleil

Reputation: 16525

How to split existing collection in Camel

I have a producer which sends a Set<Status>. I want to split the Set so downstream components have to process single Status objects. My route looks like this:

public Set<Status> loadStatus() { ... }

from("direct:start").
    split().
    to("mock:end");

ProducerTemplate template = context.createProducerTemplate();
template.sendBody("direct:start", loadStatuses());

How is this achieved?

Upvotes: 1

Views: 1987

Answers (2)

Ben ODay
Ben ODay

Reputation: 21015

the Camel Splitter documentation describes how a Collection, Iterator or Array can be used...

A common use case is to split a Collection, Iterator or Array from the message. In the sample below we simply use an Expression to identify the value to split.

from("direct:splitUsingBody").split(body()).to("mock:result");

from("direct:splitUsingHeader").split(header("foo")).to("mock:result"); 

In Spring XML you can use the Simple language to identify the value to split.

<split>
   <simple>${body}</simple>
   <to uri="mock:result"/>
</split>

<split>
   <simple>${header.foo}</simple>
   <to uri="mock:result"/>
</split>  

Upvotes: 4

Fran&#231;ois Beausoleil
Fran&#231;ois Beausoleil

Reputation: 16525

Turns out the docs are a bit unclear: none of the examples explicitly mention having a body that's a Java collection. The solution is to split the body without any expression. In that case, Camel will use it's internal conversion engine to return an Iterator from a Set. The final route is:

from("direct:start").
   split(body()).
   to("mock:end");

Found a reference in the Camel JDBC samples documentation:

from("direct:hello")
    // here we split the data from the testdb into new messages one by one
    // so the mock endpoint will receive a message per row in the table
    .to("jdbc:testdb").split(body()).to("mock:result");

Upvotes: 1

Related Questions