Camel - using end()

No! Calling end() to "end" a Camel route is not a best practice and won't yield any functional benefits.

For common ProcessorDefinition functions like to(), bean() or log() it simply leads to a call to the endParent() method, which as one can see from the Camel source code, does very little:

public ProcessorDefinition<?> endParent() { return this; }

The call to end() is required, once you have called processor definitions that start their own block and most prominently includes TryDefinitions aka doTry() and ChoiceDefinitions aka choice(), but also well know functions like split(), loadBalance(), onCompletion() or recipientList().


You must use the end() when you want to end specific route which is in action. It can be best explained in example of onCompletion

from("direct:start")
.onCompletion()
    // this route is only invoked when the original route is complete as a kind
    // of completion callback
    .to("log:sync")
    .to("mock:sync")
// must use end to denote the end of the onCompletion route
.end()
// here the original route contiues
.process(new MyProcessor())
.to("mock:result");

Here you must put end to indicate operation related to onCompletion is done and you are resuming operation on the original rote.

This becomes more lucid and easy to understand if you are using XML DSL instead of java. Because in this you don't have to use end tag. The closing tags of XML will take care of writing end(). Below is exactly same example written in XML DSL

<route>
<from uri="direct:start"/>
<!-- this onCompletion block will only be executed when the exchange is done being routed -->
<!-- this callback is always triggered even if the exchange failed -->
<onCompletion>
    <!-- so this is a kinda like an after completion callback -->
    <to uri="log:sync"/>
    <to uri="mock:sync"/>
</onCompletion>
<process ref="myProcessor"/>
<to uri="mock:result"/>