Apache Camel :复合条件,并从when()函数访问它们

vyu0f0g1  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(264)

我尝试使用PredicateBuilder复合条件,并将其保存到标头中,以便从when访问 predicate ()函数。一开始我将body设置为Hello World。而且body不是null或不等于AUTHENTICATE failed,但我进入了when块,并且process_no_mail_or_failed正在被记录。我不明白我做错了什么。我如何在Apache Camel中复合 predicate 并在when()函数中使用它们呢?以防我在某个步骤中必须检查主体。因此,我这样做了。
谢谢你的帮助!

Apache Camel 路线:

import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Predicate;
import org.apache.camel.Processor;
import org.apache.camel.builder.PredicateBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class TestRoute  extends RouteBuilder {

    static final Logger LOGGER = LoggerFactory.getLogger(EmailPollingRoute.class);
    static final String AUTHENTICATE_FAILED = "AUTHENTICATE failed.";

    @Override
    public void configure() throws Exception {

        from("quartz://emailTimer?cron=0/30+*+*+*+*+?+*")
        .log("TestRoute - Before calling transform")
        .process(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                Message message = exchange.getIn();
                message.setBody("Hello World");
                String body = message.getBody(String.class);
                LOGGER.info("body: \n" + body);

                Predicate p1 = body().isNull();
                Predicate p2 = body().isEqualTo(AUTHENTICATE_FAILED);
                Predicate predicate_result12 = PredicateBuilder.or(p1, p2);
                message.setHeader("process_no_mail_or_failed", predicate_result12);

            }   
        })
        .choice()
        .when(header("process_no_mail_or_failed").convertTo(Predicate.class)) 
        //.when(simple("${body} == null || ${body} == 'AUTHENTICATE failed.'")) //This works for me. I am getting into the otherwise block
           .log("when: ${body}")

        .otherwise()
           .log("otherwise: ${body}");

    }
}
zsohkypk

zsohkypk1#

predicate 具有matches方法,您可以使用该方法来计算 predicate
如果要获取 predicate 的结果,可以使用matches-method来计算 predicate 。

from("direct:example")
    .routeId("example")
    .process(exchange -> {

        Predicate bodyNotNullOrEmpty = PredicateBuilder.and(
            body().isNotEqualTo(null), 
            body().isNotEqualTo("")
        );
        Boolean result = bodyNotNullOrEmpty.matches(exchange);
        exchange.getMessage().setHeader("bodyNotNullOrEmpty", result);
    })
    .choice().when(header("bodyNotNullOrEmpty"))
        .log("body: ${body}")
    .otherwise()
        .log("Body is null or empty")
    .end()
    .to("mock:result");

您也可以只从Predicate构建器导入静态方法,并在选择范围内使用它们,而不必将结果存储到exchange属性中。
从 predicate 构建器导入静态方法的技巧很容易被忽略,因为它只在文档的否定 predicate 部分提到。

import static org.apache.camel.builder.PredicateBuilder.not;
import static org.apache.camel.builder.PredicateBuilder.and;

...

from("direct:example")
    .routeId("example")
    .choice()
        .when(and(body().isNotNull(), body().isNotEqualTo("")))
            .log("body: ${body}")
        .otherwise()
            .log("Body is null or empty")
    .end()
    .to("mock:result");

相关问题