Apache Camel:在 when() 中使用复合条件

Apache Camel: using compound conditions in when()

我正在使用 Apache Camel DSL 路由并想检查正文是否不是 null 并且正文是否不包含 authenticate failed 之类的子字符串。在 java 中是这样的:

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

Apache 骆驼 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`. 

这样的条件怎么写?在过程方法中谓词对象并写我的案例?

您可以使用 PredicateBuilder 创建漂亮的复合谓词。下面是如何使用 PredicateBuilder 进行简单的 Body is not null or empty 检查的示例。

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 和 Or 可以将谓词列表作为参数,甚至可以将其他复合谓词作为参数,这允许人们制作一些相当复杂的复合谓词。然而,当使用 PredicateBuilder 开始变得过于冗长时,最好只使用处理器或 bean 来抽象掉一些复杂性。