'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.



Solution 1:[1]

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

}

Solution 2:[2]

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}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eray_p1
Solution 2 Jacek Szyma?ski