Apache Camel:使用peedicateBuilder并从wher()函数访问它们,复合条件

发布于 2025-01-29 03:06:03 字数 2096 浏览 1 评论 0原文

我正在尝试将条件与谓词构造器相复合,并将其保存到标题中,以从wher()函数访问谓词。一开始,我将身体设置为Hello World。身体也不是无效的 或者不等于身份验证失败,但是我正在登录Whes block和process_no_mail_or_failed正在记录。我不明白我在做什么错。如何在Apache骆驼中复合谓词并使用 他们在()函数中?如果我必须在某个步骤检查身体。因此,我做到了。

我寻求任何帮助!

Apache骆驼路线:

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

I am trying to compound conditions with the PredicateBuilder and saving it to the header to access the predicate from the when() function. At the beginning I set the body to Hello World. Also the body is not null
or not equal to AUTHENTICATE failed but I getting into the when block and process_no_mail_or_failed is being logged. I do not understand what I am doing wrong. How can I compound predicates in Apache Camel and use
them in the when() function? In case I have to check the body at some step. Therefore I did it in the way.

I apreciate any help!

Apache Camel route:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回梦里 2025-02-05 03:06:03

可以用来评估谓词

谓词具有匹配方法,如果要获得谓词的结果, ,您可以使用匹配方法评估谓词。

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

您也可以从谓词构建器中导入静态方法,并在选择中使用它们,而无需存储结果以交换属性。

从谓词构建器导入静态方法的这种窍门很容易错过,因为仅在否定文档的谓词

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

Predicates have matches method which you can use to evaluate the predicate with

If you want to get the result for Predicate you can use the matches-method to evaluate the 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");

You could also just import static methods from the Predicate builder and use them within choice without having to store the result to exchange property.

This trick of importing the static methods from Predicate builder can be easy to miss due to it being only mentioned in negate predicates section of the documentation.

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文