Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • lookup or creating endpoints
  • lookup of beans in the Registry
  • additional supported in Spring XML (see below in examples)
  • using Blueprint PropertyPlaceholder with Camel Properties component
  • using @PropertyInject to inject a property in a POJO
  • Camel 2.14.1 Using default value if a property does not exists
  • Camel 2.14.1 Using custom functions, which can be plugged into the property component.

Syntax

The syntax to use Camel's property placeholder is to use {{key}} for example {{file.uri}} where file.uri is the property key.
You can use property placeholders in parts of the endpoint URI's which for example you can use placeholders for parameters in the URIs.

...

Code Block
    @PropertyInject(value = "myTimeout", defaultValue = "5000")
    private int timeout;

Using custom functions

Available as of Camel 2.14.1

The Properties component allow to plugin 3rd party functions which can be used during parsing of the property placeholders. These functions are then able to do custom logic to resolve the placeholders, such as looking up in databases, do custom computations, or whatnot. The name of the function becomes the prefix used in the placeholder. This is best illustrated in the example code below

Code Block
  <bean id="beerFunction" class="MyBeerFunction"/>

  <camelContext xmlns="http://camel.apache.org/schema/blueprint">
    <propertyPlaceholder id="properties" location="none" ignoreMissingLocation="true">
      <propertiesFunction ref="beerFunction"/>
    </propertyPlaceholder>

    <route>
      <from uri="direct:start"/>
      <to uri="{{beer:FOO}}"/>
      <to uri="{{beer:BAR}}"/>
    </route>
  </camelContext>

Here we have a Camel XML route where we have defined the <propertyPlaceholder> to use a custom function, which we refer to be the bean id - eg the beerFunction. As the beer function uses "beer" as its name, then the placeholder syntax can trigger the beer function by starting with beer:value.

The implementation of the function is only two methods as shown below:

Code Block
    public static final class MyBeerFunction implements PropertiesFunction {

        @Override
        public String getName() {
            return "beer";
        }

        @Override
        public String apply(String remainder) {
            return "mock:" + remainder.toLowerCase();
        }
    }

The function must implement the org.apache.camel.component.properties.PropertiesFunction interface. The method getName is  the name of the function, eg beer. And the apply method is where we implement the custom logic to do. As the sample code is from an unit test, it just returns a value to refer to a mock endpoint.

To register a custom function from Java code is as shown below:

Code Block
        PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
        pc.addFunction(new MyBeerFunction());

 

See Also