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()
The Direct component provides direct, synchronous invocation of any consumers when a producer sends a message exchange. This endpoint can be used to connect existing routes in the same camel context.
The CamelContext is the runtime system, which holds everything together as depicted in the figure below. The CamelContext provides access to many useful services, the most notable being components, type converters, a registry, endpoints, routes, data formats, and languages.
Many open source projects and closed source technologies did not withstand the tests of time and have disappeared from the middleware stacks for good. After a decade, however, Apache Camel is still here and becoming even stronger for the next decade of integration.
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"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With