when to use end() and endChoice() in camel DSL - apache-camel

I haven't found any useful explanation about when to use end() and endChoice() ? Also endChoice() is meant for what type of camel nodes (I am aware of only loadBalance) ?

Related

In Apache Camel what does "route()" do in a restful declaration?

Trying to google "route" in relation to Camel is like trying to google "the". Browsing the docs and can't find it either, only an interface called Route.
Inherited some code that looks like
rest("/someRoute")
.description("Some description")
.consumes("text/plain")
.produces("text/plain")
.post()
.route()
.to("direct:toSomewhere");
What does route() do? I have tried with and without route() and it doesn't seem to do anything.
Using .route allows you to define new route(s) within your rest-definition. It can be handy if your route is short or if you just want to process/transform/validate the message in someway before sending it to your actual consumer endpoint.
For example
rest("/someRoute")
.id("someRoute")
.description("Some description")
.post()
.consumes("text/plain")
.produces("text/plain")
.route()
.routeId("someRoutePost")
.process(new SomeMessageProcessor())
.to("direct:toSomewhere")
.end()
.endRest()
.get()
.route()
.routeId("someRouteGet")
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(405))
.setBody(constant("GET not allowed on this route"))
.end()
.endRest()
But if you just want to call direct consumer endpoint and do this stuff there instead you can do that.
it is up to ones preference really.
thanks, I see if I wanted to say call .log() I would have to put .route() first
Yes. Camel uses method-chaining with its Java-DSL where something like this is often required. When defining Rest most methods return RestDefinition but if you look closely .route method returns RouteDefition instead.
To get back to RestDefition from route one can use .endRest() as the .end() in the example doesn't really do anything other than make it easier to see where to RouteDefition block ends.
Update: Note that this example is for Camel 3.14.0. In Newer versions of Camel route() and endRest() methods have been Removed from RestDefition class. Example for Camel 3.18.1 can be found here.

Camel aggregation from two queues in Java DSL

I have two queues which having same type of objects in them. I want to aggregate them into a single queue through java DSL. Could anyone tell me if this is possible? If so, any code references?
If I understand your question correctly, it is possible to do such a thing.
If you need just to drive them into a single route (without any aggregations, enrichments, etc.) you can just proceed with this piece of code:
from('direct:queue1')
.to('direct:start');
from('direct:queue2')
.to('direct:start');
from('direct:start')
//there goes your processing
If you need to aggregate them later on, use Aggregator. Or you can use example from java-addict301's answer if it solves your case.
I believe this may be doable in Camel using the Content Enricher pattern.
Specifically, the following paradigm can be used to retrieve a message from one queue (where direct:start is) and enrich it with a message from the second queue (where direct:resource is). The combined message can then be built in your AggregationStrategy implementation class.
AggregationStrategy aggregationStrategy = ...
from("direct:start")
.enrich("direct:resource", aggregationStrategy)
.to("direct:result");
from("direct:resource")

What is the difference between end() and endChoice() in Apache Camel

Can anyone tell me the difference between end() and endChoice() in camel? Can we use end() in place of endChoice()?
The endChoice() has quite an unfortunate naming. It doesn't end the choice() block, it just ends the current when() clause.
end() on the other hand closes the current choice() block.
Can also be seen in the javadocs:
endChoice
end
end() will take care end of any type of camel Component, whereas endChoice() is specific to end the the choice Component.

Apache Camel - recipientList - need of end

After using recipientList in a camel route, within a choice.when, I would like to route this further to another destination with to("xy").
Syntax-highlighting in a java IDE is showing me that this is not possible.
If I put an end after recipientList, all appears to fit again.
Is that required? I could not find any examples in the docs/net showing s/t similar...
.choice()
.when(aPredicate)
.setHeader(Exchange.FILE_NAME).simple("st")
.recipientList(getAValueBuilder())
.end()
.to("ftp:me#ftpserv//usr/dest")
.when(anotherPredicate)
.to(nirv)
.setHeader(Exchange.FILE_NAME).simple("nt")
.to("ftp:me#ftpserv//usr/anotherdest")
.end()
Generarilly I am not sure, while using choice, when to use end. And to make it more difficult, there is an endChoice...
I tryed to use the formatting, to show the way I think it should be use above.
Thanks for feedback.

Best way of expressing Camel route

If X is false I want to route to A, if X is true I want to route to A and B
I tried to write something like
from(?)
.choice()
.when( X )
.multicast().to(A,B).end()
.otherwise() // I get a (compile) error underlined here saying 'otherwise() is not on type ProcessorDefinition
.to( A )
it doesn't like it
I suspect this isn't the best way of phrasing this
basically I always want to route to (A) and if that condition is there I also want to route to (B)
what is the best way of expressing this in Camel?
use endChoice() at the end of your when() clause and it'll work...
see http://camel.apache.org/why-can-i-not-use-when-or-otherwise-in-a-java-camel-route.html
See this FAQ about the choice: https://camel.apache.org/why-can-i-not-use-when-or-otherwise-in-a-java-camel-route.html
You can also use dynamic recipient list and compute the endpoints to route to. Then you can return 1 or 2 depending on the conditions: http://camel.apache.org/recipient-list.html
If you always want your message to go to route A, then do not include it in the choice clause
from(?)
.to( A )
.choice()
.when( X )
to(B).end()
Something like above should suffice your case. Also read the articles that Claus has given in his answer.
Regarding your compilation error, remove the end() after the when clause. end() causes the choice() clause to be finished but you then use otherwise() clause while choice has already been closed.
I have found that expressing your routes using the XML notation is a lot more concise in meaning.
For instance with the Java DSL people often make the mistake of not calling, or even adding 'endChoice()' and 'end()' like you have in your example; Sometimes you will also face an issue with Camel's Route Builder which is currently a limitation due to Java's Generics.
Unfortunately using XML comes with the cost of using XML :)

Resources