user1743310
user1743310

Reputation:

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?

Upvotes: 3

Views: 4784

Answers (4)

AlanFoster
AlanFoster

Reputation: 8306

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 :)

Upvotes: 0

Sikorski
Sikorski

Reputation: 2691

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.

Upvotes: 0

Claus Ibsen
Claus Ibsen

Reputation: 55750

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

Upvotes: 1

Ben ODay
Ben ODay

Reputation: 21005

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

Upvotes: 6

Related Questions