【问题标题】:How to check if a property placeholder exists in Apache Camel?如何检查 Apache Camel 中是否存在属性占位符?
【发布时间】:2022-02-05 21:44:29
【问题描述】:

如何检查 Apache Camel 中是否存在属性占位符?

我试过了:

.choice()
    .when(simple("{{app.custom.property}}").isNotNull())
       ...

java.lang.IllegalArgumentException:在来自文本的属性中找不到键 [app.custom.property] 的属性:{{app.custom.property}}

我也试过了(note the ? (question mark) at the beginning of the key name):

.choice()
    .when(simple("{{?app.custom.property}}").isNotNull())
        ...

java.lang.NullPointerException: null

还有(with default value):

.choice()
    .when(simple("{{app.custom.property:null}}").isNotNull())
        ...

它永远不会是null,因为(如果不存在)它用字符串"null" 初始化变量。我可以与这个字符串进行比较,但是我无法知道该属性是否真的存在,或者它是否设置了该值。

【问题讨论】:

    标签: java spring-boot apache-camel


    【解决方案1】:

    我认为,它是最简单和干净的解决方案。如果您想在其他路线中使用其他属性,您可以编写控制属性存在的基本方法

    public class Greeting  extends RouteBuilder{
    
        @Override
        public void configure() throws Exception {
           
            boolean isExist = getCamelContext().getPropertiesComponent().resolveProperty("app.custom.property").isPresent();
    
         from("timer:hello")
            .choice()
            .when(constant(isExist)).log("exist")
            .otherwise().log("not exist");
            
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用简单语言属性查找:

      Variable Type Description
      properties:key:default String Camel 2.14.1: Lookup a property with the given key. If the key does not exists or has no value, then an optional default value can be specified.

      在你的情况下,它是:${properties:app.custom.property:null}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-15
        • 2013-03-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多