Versions Compared

Key

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

...

The example below has property placeholder in the <jmxAgent> tag:

Wiki Markup
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/jmxConfigUsingProperties.xml}
You can also define property placeholders in the various attributes on the <camelContext> tag such as trace as shown here:
Wiki Markup
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringTraceUsingPropertyTest-context.xml}

Overriding a property setting using a JVM System Property

...

In the example below we use the prop prefix for the namespace http://camel.apache.org/schema/placeholder by which we can use the prop prefix in the attributes in the XML DSLs. Notice how we use that in the Multicast to indicate that the option stopOnException should be the value of the placeholder with the key "stop".

Wiki Markup
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringOptionalPropertiesDslTest.xml}
In our properties file we have the value defined as

...

Likewise we have added support for defining placeholders in the Java DSL using the new placeholder DSL as shown in the following equivalent example:

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/OptionalPropertiesDslTest.java}

Using Blueprint property placeholder with Camel routes

...

Camel supports Blueprint which also offers a property placeholder service. Camel supports convention over configuration, so all you have to do is to define the OSGi Blueprint property placeholder in the XML file as shown below:

Wiki Markup
{snippet:id=e1|lang=xml|title=Using OSGi blueprint property placeholders in Camel routes|url=camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/blueprint/blueprint-17.xml}
By default Camel detects and uses OSGi blueprint property placeholder service. You can disable this by setting the attribute useBlueprintPropertyResolver to false on the <camelContext> definition.

...

You can also explicit refer to a specific OSGi blueprint property placeholder by its id. For that you need to use the Camel's <propertyPlaceholder> as shown in the example below:

Wiki Markup
{snippet:id=e1|lang=xml|title=Explicit referring to a OSGi blueprint placeholder in Camel|url=camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/blueprint/blueprint-16.xml}
Notice how we use the blueprint scheme to refer to the OSGi blueprint placeholder by its id. This allows you to mix and match, for example you can also have additional schemes in the location. For example to load a file from the classpath you can do:

...

When using Blueprint property placeholder in the Blueprint XML file, you can declare the properties directly in the XML file as shown below:

Wiki Markup
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/configadmin-outside.xml}
Notice that we have a <bean> which refers to one of the properties. And in the Camel route we refer to the other using the {{ }} notation.

Now if you want to override these Blueprint properties from an unit test, you can do this as shown below:

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminOverridePropertiesOutsideCamelContextTest.java}
To do this we override and implement the useOverridePropertiesWithConfigAdmin method. We can then put the properties we want to override on the given props parameter. And the return value must be the persistence-id of the <cm:property-placeholder> tag, which you define in the blueprint XML file.

...

For example in the blueprint XML file we have the persistence-id="stuff", which mean it will load the configuration file as etc/stuff.cfg.

Wiki Markup
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/configadmin-loadfile.xml}
Now if you want to unit test this blueprint XML file, then you can override the loadConfigAdminConfigurationFile and tell Camel which file to load as shown below:
Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminLoadConfigurationFileTest.java}
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 value is the persistence-id of the <cm:property-placeholder> tag.

...

You can do both as well. Here is a complete example. First we have the Blueprint XML file:

Wiki Markup
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/configadmin-loadfileoverride.xml}
And in the unit test class we do as follows:
Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminLoadConfigurationFileAndOverrideTest.java}
And the etc/stuff.cfg configuration file contains

...

To bridge Spring and Camel you must define a single bean as shown below:

Wiki Markup
{snippet:id=e1|lang=xml|title=Bridging Spring and Camel property placeholders|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurerTest.xml}
You must not use the spring <context:property-placeholder> namespace at the same time; this is not possible.

After declaring this bean, you can define property placeholders using both the Spring style, and the Camel style within the <camelContext> tag as shown below:

Wiki Markup
{snippet:id=e2|lang=xml|title=Using bridge property placeholders|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/component/properties/CamelSpringPropertyPlaceholderConfigurerTest.xml}
Notice how the hello bean is using pure Spring property placeholders using the ${ } notation. And in the Camel routes we use the Camel placeholder notation with {{ }}.

...

So for example in your unit test classes, you can override the useOverridePropertiesWithPropertiesComponent method and return a java.util.Properties that contains the properties which should be preferred to be used.

Wiki Markup
{snippet:id=e1|lang=java|title=Providing properties from within unit test source|url=camel/trunk/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/ConfigAdminOverridePropertiesTest.java}
This can be done from any of the Camel Test kits, such as camel-test, camel-test-spring, and camel-test-blueprint.

...

  • 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.name - 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

As you can see these functions is intended to make it easy to lookup values from the environment. As they are provided out of the box, they can easily be used as shown below:

...