Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Reformatting and fix typos

Using PropertyPlaceholder

Available as of Camel 2.3

Camel now provides a new PropertiesComponent in in camel-core which allows you to use property placeholders when defining Camel Endpoint URIs. This works much like you would do if using Spring's <property-placeholder> tag. However Spring has a limitation that prevents 3rd third-party frameworks from fully leveraging Spring property placeholders.

...

The property placeholder is generally in use when doing typically used when trying to do any of the following:

  • lookup Lookup or creating endpoints.
  • lookup Lookup of beans in the Registry.
  • additional Additional supported in Spring XML (see below in examples).
  • using Using Blueprint PropertyPlaceholder with Camel Properties component.
  • using 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 Include out of the box functions, to lookup property values from OS environment variables, JVM system properties, or the service idiom.
  • Camel 2.14.1 Using custom functions, which can be plugged into the property component.

...

Format

The syntax to use Camel's property placeholder is to use value of a Camel property can be obtained by specifying its key name within a property placeholder, using the following format: {{key}} for example .

For example:

Code Block
{{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

Property placeholders can be used to specify parts, or all, of an endpoint's URI by embedding one or more placeholders in the URI's string definition.

From Camel 2.14.1: you can specify a default value to use if a property with the key does not exists, e.g., file.url:/some/path where the default value is the text after the colon, e.g., /some/path.

...

Camel provides a pluggable mechanism which allows 3rd part to provide that allows third-parties to specify their own resolver to use for the lookup of properties.

Camel provides a default implementation org.apache.camel.component.properties.DefaultPropertiesResolver which is capable of loading properties from the file system, classpath or Registry. You can prefix the locations with either:

...

To indicate which source to use the location must contain the appropriate prefix.

The list of prefixes is:

Prefix

Description

ref:

Lookup in the Registry.

file:

...

Load the from file system.

classpath:

...

Load from the classpath (this is also the default if no prefix is provided).

blueprint:

...

Use a specific OSGi blueprint placeholder service.

Defining Location

The PropertiesResolver need to know a must be configured with the location(s) where to resolve the use when resolving properties. You can define 1 to many locations. If you define the location in a single String property you can separate multiple locations with comma such as:One or more locations can be given. Specifying multiple locations can be done a couple of ways: using either a single comma separated string, or an array of strings.

Code Block
language
Code Block
java
java
pc.setLocation("com/mycompany/myprop.properties,com/mycompany/other.properties");
pc.setLocation(new String[] {"com/mycompany/myprop.properties", "com/mycompany/other.properties"}); 

 

FromAvailable as of Camel 2.19.0You : you can set which location can be discarded if missing by by setting the optional attribute, which is setting  optional=true, (false by default, i).e:

Configuring in Spring XML

Example:

 

Code Block
languagejava
pc.setLocations(
    "com/mycompany/override.properties;optional=true"
    ",com/mycompany/defaults.properties");

 

...

Where APP_HOME is an OS environment variable.

You can have multiple placeholders in the same location, such as:

Code Block
location=file:${env:APP_HOME}/etc/${prop.name}.properties

Using System

...

or Environment Variables to Configure Property Prefixes and Suffixes

Available as of From Camel 2.12.5, 2.13.3, 2.14.0: propertyPrefix, propertySuffix configuration properties support using the use of placeholders for de-referencing JVM system properties and OS environments variables.

Example:

Assume the For example, if PropertiesComponent is configured with the following properties file:

noformat
Code Block
language
text
dev.endpoint = result1
test.endpoint = result2

Then with the following The same properties file is then referenced from a route definition:

Code Block
java
languagejava
PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
pc.setPropertyPrefix("${stage}.");

// ...
context.addRoutes(new RouteBuilder() {
    @Override
    public void configure() throws Exception {
        from("direct:start")
          .to("properties:mock:{{endpoint}}");
    }
});

it is By using the configuration options propertyPrefix it's possible to change the target endpoint simply by changing the value of the system property stage either to dev (the message will be routed to mock:result1) or test (the message will be routed to mock:result2).

...

You have to create and register the PropertiesComponent under the name properties such as:

Code Block
java
languagejava
PropertiesComponent pc = new PropertiesComponent();
pc.setLocation("classpath:com/mycompany/myprop.properties");
context.addComponent("properties", pc);

Configuring in Spring XML

Spring XML offers two variations to configure. You can define a spring bean as a PropertiesComponent which resembles the way done in Java DSL. Or you can use the <propertyPlaceholder> tag.

Code Block
xml
languagexml
<bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
    <property name="location" value="classpath:com/mycompany/myprop.properties"/>
</bean>

Using the <propertyPlaceholder> tag makes the configuration a bit more fresh such as:

Code Block
xml
languagexml
<camelContext ...>
   <propertyPlaceholder id="properties" location="com/mycompany/myprop.properties"/>
</camelContext>

Setting the properties location through the location tag works just fine but sometime you have a number of resources to take into account and starting from Camel 2.19.0 you can set the properties location with a dedicated propertiesLocation:

Code Block
languagexml
<camelContext ...>
  <propertyPlaceholder id="myPropertyPlaceholder">
    <propertiesLocation
      resolver = "classpath"
      path     = "com/my/company/something/my-properties-1.properties"
      optional = "false"/>
    <propertiesLocation
      resolver = "classpath"
      path     = "com/my/company/something/my-properties-2.properties"
      optional = "false"/>
    <propertiesLocation
      resolver = "file"
      path     = "${karaf.home}/etc/my-override.properties"
      optional = "true"/>
   </propertyPlaceholder>
</camelContext>

...

In the example above the to endpoint will be resolved to mock:result.

You can also have properties with refer to each other such as:

...

When using Blueprint property placeholder in the Blueprint XML file, you can declare the properties in a .properties or .cfg file. If you use Apache ServieMixServiceMix/Karaf then this container has a convention that it loads the properties from a file in the etc directory with the naming etc/pid.cfg, where pid is the persistence-id.

...

Notice that this method requires to return a String[] with 2 values. The 1st value is the path for the configuration file to load. The 2nd second value is the persistence-id of the <cm:property-placeholder> tag.

...

The Spring Framework does not allow 3rd third-party frameworks such as Apache Camel to seamless hook into the Spring property placeholder mechanism. However you can easily bridge Spring and Camel by declaring a Spring bean with the type org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer, which is a Spring org.springframework.beans.factory.config.PropertyPlaceholderConfigurer type.

...

This can be done from any of the Camel Test kits, such as camel-test, camel-test-spring and camel-test-blueprint.

The ignoreMissingLocationWithPropertiesComponent can be used to instruct Camel to ignore any locations which was not discoverable. For example if you run the unit test, in an environment that does not have access to the location of the properties.

...

The Properties component includes the following functions out of the box

  • env - A function to lookup the property from OS environment variables.
  • sys - A function to lookup the property from Java JVM system properties.
  • service - A function to lookup the property from OS environment variables using the service naming idiom.
  • service.host - Camel 2.16.1: A function to lookup the property from OS environment variables using the service naming idiom returning the hostname part only.
  • service.port - Camel 2.16.1: A function to lookup the property from OS environment variables using the service naming idiom returning the port part only.

...