Search code examples
javaspringapache-camel

Apache Camel spilt, tokenize & streaming - last element


I have the following Camel route

from("file://target/input/?delete=true")
    .log("Started processing [${header.CamelFileNameOnly}] ...")
    .split(body().tokenize("\n")).streaming()
    .log("ELEMENT ${body}")
    .process(this::doStuffWithElement);

I'd like to know somehow when the last line in the file is reached, and then do further processing. I have tried to call end() however in this case it doesn't work as I want.

I'd like to find a solution that could be applied to all sort of sources, eg. an incoming HTTP request, a file downloaded from S3 or SFTP - to name a few.

The idea is to implement a streaming-style processing solution, that doesn't spike in memory consumption, but that it's aware of when the end of the stream is reached correctly.


Solution

  • After .split() is called the exchange will have a property called CamelSplitComplete.

    More importantly though, if you want to make .end() work as intended by common sense, you need to call it twice in a row: .end().end() will effectively mean the stream is finished, and after that call you can do further processing. Found this solution because of this (camel-4.6.x).