I have been working with Apache Camel for a while and I came across splitter functionality. And I was wondering why can't I stop the whole split process on some condition? After hours of googling I didn't find any info about it. The only thing I found was stopOnException()
and stopOnAggregateException()
. But what if I want to stop it on some specific exception or even on some condition depending on message data. Can somebody tell me how I can do that?
Let's consider this peace of code
from("timer://file-poll?period=5s")
.onException(Exception.class)
.log(LoggingLevel.ERROR, "Error message")
.end()
.to("direct:load-file") // load csv
.split(body()).delimiter("\n") // split file into lines
.to("direct:process-line") // throws exceptions
.end()
.to("direct:save-result");
I poll file system and load a csv file with 1,000 lines.
Then I split the file into lines and process each line separately.
direct:process-line
route can throw exceptions and if it throws LineProcessingException
I need to skip the line and continue processing of next lines but if it throws any other exceptions I need to stop the splitter.
For instance:
Processing of line 15th throws LineProcessingException
and I skip this line and continue processing the rest 985 lines.
Processing of line 30th throws IOException
so now I need to stop processing 970 lines that left and go to direct:save-result
route.
I tried stopOnException()
and stopOnAggregateException()
but as I said above it stops splitter on any exceptions and I don't need to stop the splitter and just skip the line if LineProcessingException
was thrown. I also tried calling stop()
on a sub-route that is processing the line but it stops only the sub-route not the splitter.