Apache骆驼:使用()中使用复合条件

发布于 2025-01-28 06:48:53 字数 654 浏览 1 评论 0原文

我正在使用Apache骆驼DSL路由,并且想检查身体是否不是null,并且主体不包含像身份验证失败的子字符串。在Java中类似:

              if(body != null && !body.contains("authenticate failed")) {
                  //Do something.
                  
              }

Apache Camel DSL:

    .choice()
        .when(body().contains("authenticate failed"))
             .log("after choice body().contains('authenticate failed') body: ${body}")
        .when(body().isNotNull()) //Here I want to add the addiontional condition to this when `body not contains authenticate failed`. 

如何编写这样的条件?在过程方法中鉴定对象并写下我的案例?

I am using Apache Camel DSL route and want to check if the body is not null and body does not contains substring like authenticate failed. In java something like:

              if(body != null && !body.contains("authenticate failed")) {
                  //Do something.
                  
              }

Apache Camel DSL:

    .choice()
        .when(body().contains("authenticate failed"))
             .log("after choice body().contains('authenticate failed') body: ${body}")
        .when(body().isNotNull()) //Here I want to add the addiontional condition to this when `body not contains authenticate failed`. 

How can I write a condition like this? predicates objects in the process method and write tin my case?

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

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

发布评论

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

评论(1

江挽川 2025-02-04 06:48:53

您可以使用peedicateBuilder创建漂亮的复合谓词。这是如何做简单的身体的示例,不是用谓词构建器进行空白或空检查。

package com.example;

import org.apache.camel.Predicate;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.PredicateBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class ExampleTests extends CamelTestSupport {
    

    @Test
    public void testValidBody() throws Exception {

        MockEndpoint resultMockEndpoint = getMockEndpoint("mock:notNull");
        resultMockEndpoint.expectedMessageCount(1);

        template.sendBody("direct:predicateExample", "Hello world!");

        resultMockEndpoint.assertIsSatisfied();
    }

    @Test
    public void testEmptyBody() throws Exception {


        MockEndpoint resultMockEndpoint = getMockEndpoint("mock:nullOrEmpty");
        resultMockEndpoint.expectedMessageCount(1);

        template.sendBody("direct:predicateExample", null);

        resultMockEndpoint.assertIsSatisfied();
    }

    @Test
    public void testNullBody() throws Exception {

        MockEndpoint resultMockEndpoint = getMockEndpoint("mock:nullOrEmpty");
        resultMockEndpoint.expectedMessageCount(1);

        template.sendBody("direct:predicateExample", null);

        resultMockEndpoint.assertIsSatisfied();
    }

    @Override
    protected RoutesBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {

            @Override
            public void configure() throws Exception {
             
                Predicate bodyNotNullOrEmpty = PredicateBuilder.and(
                    body().isNotNull(), 
                    body().isNotEqualTo("")
                );

                from("direct:predicateExample")
                    .routeId("predicateExample")
                    .choice().when(bodyNotNullOrEmpty)
                        .log("Received body: ${body}")
                        .to("mock:notNull")
                    .otherwise()
                        .log("Body was null or empty!")
                        .to("mock:nullOrEmpty")
                    .end()
                    .log("done");
            }
        };
    }
}

没有,或者可以将谓词作为参数甚至其他复合谓词列表,这使人们可以做出一些相当复杂的复合谓词。但是,当使用peedicateBuilder开始变得过于冗长时,只需使用处理器或Bean就可以抽象一些复杂性。

You can use PredicateBuilder to create pretty composite predicates. Here's example how to do simple Body is not null or empty check with PredicateBuilder.

package com.example;

import org.apache.camel.Predicate;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.PredicateBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;

public class ExampleTests extends CamelTestSupport {
    

    @Test
    public void testValidBody() throws Exception {

        MockEndpoint resultMockEndpoint = getMockEndpoint("mock:notNull");
        resultMockEndpoint.expectedMessageCount(1);

        template.sendBody("direct:predicateExample", "Hello world!");

        resultMockEndpoint.assertIsSatisfied();
    }

    @Test
    public void testEmptyBody() throws Exception {


        MockEndpoint resultMockEndpoint = getMockEndpoint("mock:nullOrEmpty");
        resultMockEndpoint.expectedMessageCount(1);

        template.sendBody("direct:predicateExample", null);

        resultMockEndpoint.assertIsSatisfied();
    }

    @Test
    public void testNullBody() throws Exception {

        MockEndpoint resultMockEndpoint = getMockEndpoint("mock:nullOrEmpty");
        resultMockEndpoint.expectedMessageCount(1);

        template.sendBody("direct:predicateExample", null);

        resultMockEndpoint.assertIsSatisfied();
    }

    @Override
    protected RoutesBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {

            @Override
            public void configure() throws Exception {
             
                Predicate bodyNotNullOrEmpty = PredicateBuilder.and(
                    body().isNotNull(), 
                    body().isNotEqualTo("")
                );

                from("direct:predicateExample")
                    .routeId("predicateExample")
                    .choice().when(bodyNotNullOrEmpty)
                        .log("Received body: ${body}")
                        .to("mock:notNull")
                    .otherwise()
                        .log("Body was null or empty!")
                        .to("mock:nullOrEmpty")
                    .end()
                    .log("done");
            }
        };
    }
}

Not and Or can take lists of predicates as arguments or even other composite predicates which allows one to make some fairly complex composite predicates. However when using PredicateBuilder starts to get overly verbose it's good to just use processor or bean instead to abstract away some of the complexity.

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