Camel - using end()

13,192

Solution 1

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().

Solution 2

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"/>

Share:
13,192
saravana_pc
Author by

saravana_pc

Updated on August 05, 2022

Comments

  • saravana_pc
    saravana_pc over 1 year

    Is it a best practice to use end() for every route?

    The following works:

    from("jms:some-queue")      
        .beanRef("bean1", "method1")
        .beanRef("bean2", "method2")
    

    and so is this,

    from("jms:some-queue")      
        .beanRef("bean1", "method1")
        .beanRef("bean2", "method2")
        .end()
    
  • saravana_pc
    saravana_pc over 8 years
    thanks, I understand there are scenarios where we must use end() (it might even result in compilation error, otherwise). My doubt is, in the scenario that I have mentioned in the question, does that make any difference at all?
  • Hector
    Hector over 7 years
    Hi, Are there some documentation that explain the end() method? Thanks
  • the hand of NOD
    the hand of NOD about 6 years
    Hi, I did not find an official documentation about the end() method, but I think the following link is enough verification for the answer: camel users: question about end()