简体   繁体   中英

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

How to check if a property placeholder exists in 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}}

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

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" . 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. 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}

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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