在Apache Camel中,当使用路由的when()语句时,有时需要组合多个条件来过滤消息。下面是如何使用复合条件的示例代码:
from("direct:start") .choice() .when().body().contains("foo").and().header("myHeader").isEqualTo("bar") .to("direct:fooBar") .when().body().contains("foo") .to("direct:foo") .when().header("myHeader").isEqualTo("bar") .to("direct:bar") .otherwise() .to("direct:other") .end();
在上面的示例中,when()语句使用contains()和isEqualTo()方法来组合多个条件。这么做能够使路由更加准确,过滤消息更加精细。同时,使用otherwise()方法来定义默认的路由路径。
注意,在组合多个条件时,每个条件都需要使用()括起来,否则可能会出现语法错误。