WebDev
WebDev

Reputation: 250

Camel route multiple if condition

I have to write to multiple if condition in camel and I need help to go around.

if(token is NULL)
if(condition is NULL) 
if(Dates does not match)
Then execute this...

What I am trying is

.choice
.when(token is NULL)
.when(condition is NULL)
.when(Dates does not match)
.log(update DB)
.endchoice()

which dont work.. Please help

Upvotes: 1

Views: 29154

Answers (3)

Benipal
Benipal

Reputation: 211

Two conditions:

Predicate p1 = header("token").isEqualTo("001"):
Predicate p2 = header("condition").isEqualTo("002");

Combine these conditions:

Predicate cond = PredicateBuilder.and(p1, p2);

Then In Camel:

.choice
.when(cond)
.log(update DB)
.endchoice()

Upvotes: 10

Sean Xue
Sean Xue

Reputation: 66

The best way to do this is to use Predicates.

You can define Predicates as private field if you are using Java DSL, by using expression builder, to build multiple conditions, then use the predicate in your when(), your route would looks much cleaner and easier to read.

private static final Predicate invalidHeaders = or(header(XXX).isNull(), header(YYY).isNull());

...

.when(invalidHeaders)

Upvotes: 5

Claus Ibsen
Claus Ibsen

Reputation: 55760

You need to do this in a single when and use and &&

.when(token is NULL && condition is NULL && XXX)

There are various ways to do this.

If you use Java code then you can append multiple predicates together: http://www.davsclaus.com/2009/02/apache-camel-and-using-compound.html

Upvotes: 0

Related Questions