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

Apache Camel: compound conditions by using the predicateBuilder and access them from the when() function

我正在尝试使用 PredicateBuilder 组合条件并将其保存到 header 以从 when() 函数访问谓词。一开始我将 body 设置为 Hello World。 body 也不为空 或不等于 AUTHENTICATE failed 但我进入 when 块并且正在记录 process_no_mail_or_failed 。我不明白我做错了什么。我如何在 Apache Camel 中复合谓词并使用 它们在 when() 函数中?万一我必须在某个步骤检查 body 。所以顺路做了。

非常感谢您的帮助!

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}");
    
    }
}
    

Predicates 有 matches 方法,你可以用它来评估谓词

如果您想获得 Predicate 的结果,您可以使用 matches-method 来评估谓词。

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 构建器导入静态方法并在选择中使用它们,而不必存储结果以进行交换 属性。

这个从 Predicate 构建器导入静态方法的技巧很容易被忽略,因为它只在文档的 negate predicates section 中提到过。

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");