Camel路由多个if条件

ycggw6v2  于 2022-11-07  发布在  Apache
关注(0)|答案(3)|浏览(204)

我必须写多个如果条件在 Camel 和我需要帮助去周围。

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

我想做的是

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

这不起作用..请帮助

eit6fx6z

eit6fx6z1#

两个条件:

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

合并这些条件:

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

然后在 Camel :

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

k7fdbhmy2#

最好的方法是使用Predicate s。
你可以定义Predicate为私有字段,如果你正在使用Java DSL,通过使用表达式构建器,来构建多个条件,然后在你的when()中使用 predicate ,你的路由会看起来更清晰和更容易阅读。

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

...

.when(invalidHeaders)
nfeuvbwi

nfeuvbwi3#

您需要在单个when中执行此操作,并使用和&&

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

有多种方法可以做到这一点。
如果使用Java代码,则可以将多个 predicate 附加在一起:http://www.davsclaus.com/2009/02/apache-camel-and-using-compound.html

相关问题