简体   繁体   English

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

[英]How to check if a property placeholder exists in Apache Camel?

How to check if a property placeholder exists in Apache Camel?如何检查 Apache Camel 中是否存在属性占位符?

I tried:我试过了:

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

java.lang.IllegalArgumentException: Property with key [app.custom.property] not found in properties from text: {{app.custom.property}} java.lang.IllegalArgumentException:在文本的属性中找不到键为 [app.custom.property] 的属性:{{app.custom.property}}

I also tried ( note the? (question mark) at the beginning of the key name ):我也试过( 注意键名开头的?(问号) ):

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

java.lang.NullPointerException: null java.lang.NullPointerException:null

Also ( with default value ):另外( 默认值):

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

It is never null because (if not exists) it initializes the variable with the string "null" .它永远不会是null因为(如果不存在)它用字符串"null"初始化变量。 I could compare against this string, but then I have no way of knowing if the property didn't really exist or if it had set that value.我可以与这个字符串进行比较,但是我无法知道该属性是否真的不存在或者它是否设置了该值。

You can use Simple Language property lookup:您可以使用简单语言属性查找:

Variable多变的 Type类型 Description描述
properties:key:default属性:键:默认 String细绳 Camel 2.14.1: Lookup a property with the given key. Camel 2.14.1:使用给定键查找属性。 If the key does not exists or has no value, then an optional default value can be specified.如果键不存在或没有值,则可以指定一个可选的默认值。

In your case, it would be: ${properties:app.custom.property:null}在您的情况下,它将是: ${properties:app.custom.property:null}

I think , its is easiest and clean solution .我认为,它是最简单和干净的解决方案。 if you want use other properties in other routes u can write basic method for controlling property exist如果您想在其他路线中使用其他属性,您可以编写控制属性存在的基本方法

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM