Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a file into lines in Camel but process the first line differently

Tags:

apache-camel

I am splitting a file into lines using a tokenizer:

.split().tokenize("\n", 1)

However, some of the files I need to process will contain a header line, which will need to be processed differently to the normal lines. Is there an easy way to read the first line, process that, then split the remaining lines?

like image 859
Hedley Avatar asked Jan 23 '13 10:01

Hedley


People also ask

How split works in camel?

Using Split. The following example shows how to take a request from the direct:a endpoint, then split into sub messages, which each are sent to the direct:b endpoint. The example splits the message body using a tokenizer to split into lines using the new-line character as separator.

How to end split in camel?

end() is needed to avoid anything outside the split block from being included. The desired behavior was to split the body, route each part using a routingslip to a different endpoint. Once the split block was complete, then continue processing with the exchange (and body) being as it was before the split.


1 Answers

You can do something like this. It will use a content based router EIP, then different sub routes for processing.

from(A)
   .split().tokenize("\n",1)
       .choice()
         .when(simple("${property.CamelSplitIndex} > 0"))
           .to("direct:processLine")
         .otherwise()
           .to("direct:processHeader");

from("direct:processLine")
 .bean(processLineBean)
 .to(B);

from("direct:processHeader")
 .bean(processHeaderBean)
 .to(B);
like image 149
Petter Nordlander Avatar answered Sep 30 '22 19:09

Petter Nordlander