You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by dermoritz <ta...@hotmail.com> on 2014/08/06 11:18:24 UTC
How to relay different parts of message to different endpoints
I have to handle csv (gz compressed) files that contain lines with different
number of fields. at the moment i am only interested in lines with 35 fields
(contain 34 ','). I wrote a processor that filters those lines. after the
processor the exchange is multicasted to some endpoints.
Now i also need lines with 65 fields and those lines should be multicasted
to other endpoints. So i need a way to route all 65 field lines and all 35
field lines to different endpoints.
The easy solution would be to multicast the csv to 2 processores one for
each filter. But then i have to scan the file once per filter (there will
com more).
But is there a solution that uses one processor (so each file has only be
scanned once)? I read about splitter pattern and multicast onPrepare, but i
didn't get the clue how to use it in my case.
--
View this message in context: http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How to relay different parts of message to different endpoints
Posted by dermoritz <ta...@hotmail.com>.
Thanks,
the problem with this solution is i need a 2nd processor or a custom
predicate that could be used in choice().when().
Since each "pair" must be routed to a different set of endpoints i need the
"when" to use the integer.
I think it would be much better to let the first producer just send multiple
exchanges with header set. the output could be directly fed into
choice().when(header).
but how to create such a processor? This
http://camel.apache.org/how-do-i-write-a-custom-processor-which-sends-multiple-messages.html
isnt working (don't know what default endpoint means - what to set here)
--
View this message in context: http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5755648.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How to relay different parts of message to different endpoints
Posted by Paul McCulloch <pk...@gmail.com>.
You could implement this in many different ways. One example would be
List<Map<Integer, Byte[]>> or just declare your own class with a count of
fields & a byte[].
I think a custom splitter looks like a much neater solution though.
On 7 August 2014 09:20, dermoritz <ta...@hotmail.com> wrote:
> Thanks
> but please tell what this is "List of pairs (fieldcount,byte[])" or how to
> create it and use with splitter?
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5754882.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
Re: How to relay different parts of message to different endpoints
Posted by dermoritz <ta...@hotmail.com>.
Thanks
but please tell what this is "List of pairs (fieldcount,byte[])" or how to
create it and use with splitter?
--
View this message in context: http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5754882.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How to relay different parts of message to different endpoints
Posted by Paul McCulloch <pk...@gmail.com>.
I guess you could return a List of pairs (fieldcount,byte[]), then split &
then use the CBR to look at the field count & dispatch accordingly.
A simpler approach would be to do what the MySplitterBean example does in
http://camel.apache.org/splitter.html. In your case return the byte[] in
each of the message bodies & set the field count in the header.
On 6 August 2014 15:06, dermoritz <ta...@hotmail.com> wrote:
> process() is a void method but do you mean instead of a byte[] i just
> List<byte[]> into "exchange.getOut().setBody(returnVal)?
>
> The split would then automatically split into 2 byte[]? but how to know the
> number of fields in each one?
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5754862.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
Re: How to relay different parts of message to different endpoints
Posted by dermoritz <ta...@hotmail.com>.
process() is a void method but do you mean instead of a byte[] i just
List<byte[]> into "exchange.getOut().setBody(returnVal)?
The split would then automatically split into 2 byte[]? but how to know the
number of fields in each one?
--
View this message in context: http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5754862.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How to relay different parts of message to different endpoints
Posted by Paul McCulloch <pk...@gmail.com>.
If your processor does something like
byte[] a = ...
byte[] b = ...
List returnVal = new ...
returnVal.add(a);
returnVal.add(b);
return returnVal
Then you can use split(body()) to get one message for each of your byte[]
On 6 August 2014 14:49, Paul McCulloch <pk...@gmail.com> wrote:
> If your processor does:
>
>
>
> On 6 August 2014 14:45, dermoritz <ta...@hotmail.com> wrote:
>
>> How to do this? The 2 byte[] are created into a processor. I have to
>> create 2
>> byte[] from one byte[] and i don't want to read the input byte[] twice.
>>
>> In meantime i found:
>>
>> http://camel.apache.org/how-do-i-write-a-custom-processor-which-sends-multiple-messages.html
>>
>> with this i think i can create 2 messages, set their headers and do the
>> split using "choice".
>>
>>
>>
>>
>>
>> --
>> View this message in context:
>> http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5754858.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>
>
Re: How to relay different parts of message to different endpoints
Posted by Paul McCulloch <pk...@gmail.com>.
If your processor does:
On 6 August 2014 14:45, dermoritz <ta...@hotmail.com> wrote:
> How to do this? The 2 byte[] are created into a processor. I have to
> create 2
> byte[] from one byte[] and i don't want to read the input byte[] twice.
>
> In meantime i found:
>
> http://camel.apache.org/how-do-i-write-a-custom-processor-which-sends-multiple-messages.html
>
> with this i think i can create 2 messages, set their headers and do the
> split using "choice".
>
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5754858.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
Re: How to relay different parts of message to different endpoints
Posted by dermoritz <ta...@hotmail.com>.
How to do this? The 2 byte[] are created into a processor. I have to create 2
byte[] from one byte[] and i don't want to read the input byte[] twice.
In meantime i found:
http://camel.apache.org/how-do-i-write-a-custom-processor-which-sends-multiple-messages.html
with this i think i can create 2 messages, set their headers and do the
split using "choice".
--
View this message in context: http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5754858.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How to relay different parts of message to different endpoints
Posted by Paul McCulloch <pk...@gmail.com>.
Use split() to turn your 2 byte[] in to two messages?
On 6 August 2014 14:08, dermoritz <ta...@hotmail.com> wrote:
> From unmarshal().gzip() comes a byte[] (with mixed number of fields in each
> line).
>
> My processor scans the byte[] line by line some lines have 35 ',' some have
> 65 ',' and i want to scan each byte[] only once.
> So i create 2 byte arrays one with 35er lines and one with 65er lines. My
> question is what to do with this 2 byte array. I think a have to create 2
> exchanges within processor and set the header.
>
> Sorry i don't see how this works with one byte[] and setting 2 different
> headers. Could you probably show a route processor that could do this
> (pseudo code would be sufficient)
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5754854.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
Re: How to relay different parts of message to different endpoints
Posted by dermoritz <ta...@hotmail.com>.
>From unmarshal().gzip() comes a byte[] (with mixed number of fields in each
line).
My processor scans the byte[] line by line some lines have 35 ',' some have
65 ',' and i want to scan each byte[] only once.
So i create 2 byte arrays one with 35er lines and one with 65er lines. My
question is what to do with this 2 byte array. I think a have to create 2
exchanges within processor and set the header.
Sorry i don't see how this works with one byte[] and setting 2 different
headers. Could you probably show a route processor that could do this
(pseudo code would be sufficient)
--
View this message in context: http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5754854.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How to relay different parts of message to different endpoints
Posted by "Walzer, Thomas" <th...@integratix.net>.
You don´t have to unmarshal the csv. As long as your processor sets the header („numberOfFields“) correctly (eg by counting the number of “,“) you can afterwards use choice to route to the appropriate endpoints.
No need to create 2 byte[].
Your processor emits everything and you filter afterwards.
Am 06.08.2014 um 14:28 schrieb dermoritz <ta...@hotmail.com>:
> Thanks,
>
> in my case i don't want to use unmarchal().csv() because i don't need the
> lines to be split (complet csv lines will be sent to "to").
> So my route i something like this:
>
> from("file:src/test/resources/?fileName=foo.csv&noop=true").
> unmarshal().gz().
> // body is byte[]
> at the moment my processor takes the byte array and creates a new one (only
> containing the relevant lines).
> My processor must emit 2 byte[] (or one per filter set). So i have to create
> 1 exchange per filter (at the moment 2) and set the header - the choice part
> would be as you suggested.
>
> But how to create new exchanges in processor?
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5754849.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How to relay different parts of message to different endpoints
Posted by dermoritz <ta...@hotmail.com>.
Thanks,
in my case i don't want to use unmarchal().csv() because i don't need the
lines to be split (complet csv lines will be sent to "to").
So my route i something like this:
from("file:src/test/resources/?fileName=foo.csv&noop=true").
unmarshal().gz().
// body is byte[]
at the moment my processor takes the byte array and creates a new one (only
containing the relevant lines).
My processor must emit 2 byte[] (or one per filter set). So i have to create
1 exchange per filter (at the moment 2) and set the header - the choice part
would be as you suggested.
But how to create new exchanges in processor?
--
View this message in context: http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5754849.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How to relay different parts of message to different endpoints
Posted by Paul McCulloch <pk...@gmail.com>.
How about something along the lines of:
from("file:src/test/resources/?fileName=foo.csv&noop=true").
unmarshal().csv().
split(body()).
setHeader("numberOfFields", bean("FieldCounter")).
choice().
when(header("numberOfFields").isEqualTo(34)
.to("direct:a")
.when(header("numberOfFields").isEqualTo(65)
.to("direct:b")
?
I don't know if using choice without an otherwise is legal, or if you'd
have to use filter first (I'm new to Camel)
On 6 August 2014 11:49, dermoritz <ta...@hotmail.com> wrote:
> Thanks for this hint but how to implement the processor that splits to 2
> types:
> if i scan through the file i'll end up with 2 messages/blocks (1 for each
> content type). So how to implement the splitting - create 2 exchanges (for
> each content type) from on exchange (1 csv file).
>
> Is there a way that a processor just creates a new exchange (based on the
> in-exchange) an emits it? Then i would gather the data for each type and
> emit one exchange per type.
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5754846.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
Re: How to relay different parts of message to different endpoints
Posted by dermoritz <ta...@hotmail.com>.
Thanks for this hint but how to implement the processor that splits to 2
types:
if i scan through the file i'll end up with 2 messages/blocks (1 for each
content type). So how to implement the splitting - create 2 exchanges (for
each content type) from on exchange (1 csv file).
Is there a way that a processor just creates a new exchange (based on the
in-exchange) an emits it? Then i would gather the data for each type and
emit one exchange per type.
--
View this message in context: http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5754846.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How to relay different parts of message to different endpoints
Posted by Paul McCulloch <pk...@gmail.com>.
Perhaps, rather than filtering, your processor could just add a header
which states the number of fields in the file. You could then use Filter &
Content Based Router to send the messages to the appropriate place?
On 6 August 2014 10:18, dermoritz <ta...@hotmail.com> wrote:
> I have to handle csv (gz compressed) files that contain lines with
> different
> number of fields. at the moment i am only interested in lines with 35
> fields
> (contain 34 ','). I wrote a processor that filters those lines. after the
> processor the exchange is multicasted to some endpoints.
>
> Now i also need lines with 65 fields and those lines should be multicasted
> to other endpoints. So i need a way to route all 65 field lines and all 35
> field lines to different endpoints.
>
> The easy solution would be to multicast the csv to 2 processores one for
> each filter. But then i have to scan the file once per filter (there will
> com more).
>
> But is there a solution that uses one processor (so each file has only be
> scanned once)? I read about splitter pattern and multicast onPrepare, but i
> didn't get the clue how to use it in my case.
>
>
>
>
> --
> View this message in context:
> http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
Re: How to relay different parts of message to different endpoints
Posted by dermoritz <ta...@hotmail.com>.
shit happens here the link:
http://stackoverflow.com/questions/25488026/split-csv-file-by-number-of-fields-each-line-different-to-different-target-end
--
View this message in context: http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5755680.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: How to relay different parts of message to different endpoints
Posted by dermoritz <ta...@hotmail.com>.
for sake of completeness here a link to stackoverflow,
my answer is based on pkmcculloch's solution the accepted answer is another
way.
--
View this message in context: http://camel.465427.n5.nabble.com/How-to-relay-different-parts-of-message-to-different-endpoints-tp5754836p5755679.html
Sent from the Camel - Users mailing list archive at Nabble.com.