Using PropertyPlaceholder - 6.3

Talend ESB Mediation Developer Guide

EnrichVersion
6.3
EnrichProdName
Talend Data Fabric
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for ESB
Talend Real-Time Big Data Platform
task
Design and Development
EnrichPlatform
Talend ESB

Camel now provides a new PropertiesComponent 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 have a limitation which prevents 3rd party frameworks to leverage Spring property placeholders to the fullest. See more at How do I use Spring Property Placeholder with Camel XML.

The property placeholder is generally in use when doing:

  • lookup or creating endpoints

  • lookup of beans in the Registry

  • additional supported in Spring XML (see below in examples)

  • using Blueprint PropertyPlaceholder with Camel Camel Component: Properties 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.

PropertyResolver

As usual Camel provides a pluggable mechanism which allows 3rd part to provide their own resolver to lookup 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:

  • ref: to lookup in the Registry

  • file: to load the from file system

  • classpath: to load from classpath (this is also the default if no prefix is provided)

  • blueprint: to use a specific OSGi blueprint placeholder service

Defining location

The PropertiesResolver need to know a location(s) where to resolve the properties. You can define one to many locations. If you define the location in a single String property you can separate multiple locations with comma such as:

pc.setLocation(
   "com/mycompany/myprop.properties,com/mycompany/other.properties");
Using system and environment variables in locations

The location now supports using placeholders for JVM system properties and OS environments variables.

For example:

location=file:${karaf.home}/etc/foo.properties

In the location above we defined a location using the file scheme using the JVM system property with key karaf.home.

To use an OS environment variable instead you would have to prefix with env:

location=file:${env:APP_HOME}/etc/foo.properties

where APP_HOME is an OS environment.

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

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

Configuring in Java DSL

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

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.

<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:

<camelContext ...>
   <propertyPlaceholder id="properties" 
      location="com/mycompany/myprop.properties"/>
</camelContext>

Using a Properties from the Registry

For example in OSGi you may want to expose a service which returns the properties as a java.util.Properties object.

Then you could setup the Camel Component: Properties component as follows:

<propertyPlaceholder id="properties" location="ref:myProperties"/>

where myProperties is the id to use for lookup in the OSGi registry. Notice we use the ref: prefix to tell Camel that it should lookup the properties for the Registry.

Examples using properties component

When using property placeholders in the endpoint URIs you can either use the properties: component or define the placeholders directly in the URI. We will show example of both cases, starting with the former.

// properties
cool.end=mock:result

// route
from("direct:start").to("properties:{{cool.end}}");

You can also use placeholders as a part of the endpoint uri:

// properties
cool.foo=result

// route
from("direct:start").to("properties:mock:{{cool.foo}}");

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:

// properties
cool.foo=result
cool.concat=mock:{{cool.foo}}

// route
from("direct:start").to("properties:mock:{{cool.concat}}");

Notice how cool.concat refer to another property.

The properties: component also offers you to override and provide a location in the given uri using the locations option:

from("direct:start")
   .to("properties:bar.end?locations=com/mycompany/bar.properties");

Examples

You can also use property placeholders directly in the endpoint uris without having to use properties:.

// properties
cool.foo=result

// route
from("direct:start").to("mock:{{cool.foo}}");

And you can use them in multiple wherever you want them:

// properties
cool.start=direct:start
cool.showid=true
cool.result=result

// route
from("{{cool.start}}")
   .to("log:{{cool.start}}?showBodyType=false"
   + "&showExchangeId={{cool.showid}}")
   .to("mock:{{cool.result}}");

You can also your property placeholders when using ProducerTemplate for example:

template.sendBody("{{cool.start}}", "Hello World");

Example with Simple language

The Simple language now also support using property placeholders, for example in the route below:

// properties
cheesy.quote=Camel rocks

// route
from("direct:start")
   .transform().simple(
   "Hi ${body} do you think ${properties:cheesy.quote}?");

You can also specify the location in the Simple language for example:

// bar.properties
bar.quote=Beer tastes good

// route
from("direct:start")
   .transform()
   .simple(
      "Hi ${body}. ${properties:com/mycompany/bar.properties:bar.quote}.");

Additional property placeholder supported in Spring XML

The property placeholders is also supported in many of the Camel Spring XML tags such as <package>, <packageScan>, <contextScan>, <jmxAgent>, <endpoint>, <routeBuilder>, <proxy> and the others.

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

<camelContext xmlns="http://camel.apache.org/schema/spring">
   <propertyPlaceholder id="properties" 
      location="org/apache/camel/spring/jmx.properties"/>

   <!-- we can use propery placeholders when we define the JMX agent -->
   <jmxAgent id="agent" 
      registryPort="{{myjmx.port}}" disabled="{{myjmx.disabled}}"
      usePlatformMBeanServer="{{myjmx.usePlatform}}"
      createConnector="true"
      statisticsLevel="RoutesOnly"/>

   <route id="foo" autoStartup="false">
      <from uri="seda:start"/>
      <to uri="mock:result"/>
   </route>
</camelContext>

You can also define property placeholders in the various attributes on the <camelContext> tag such as trace as shown here:

<camelContext trace="{{foo.trace}}" 
   xmlns="http://camel.apache.org/schema/spring">
   <propertyPlaceholder 
      id="properties" 
      location="org/apache/camel/spring/processor/myprop.properties"/>

   <template id="camelTemplate" defaultEndpoint="{{foo.cool}}"/>

   <route>
      <from uri="direct:start"/>
      <setHeader headerName="{{foo.header}}">
         <simple>${in.body} World!</simple>
      </setHeader>
      <to uri="mock:result"/>
   </route>
</camelContext>

Overriding a property setting using a JVM System Property

It is possible to override a property value at runtime using a JVM System property without the need to restart the application to pick up the change. This may also be accomplished from the command line by creating a JVM System property of the same name as the property it replaces with a new value. An example of this is given below

PropertiesComponent pc = 
   context.getComponent("properties", PropertiesComponent.class);
   pc.setCache(false);
        
   System.setProperty("cool.end", "mock:override");
   System.setProperty("cool.result", "override");

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

   getMockEndpoint("mock:override").expectedMessageCount(2);

   template.sendBody("direct:start", "Hello World");
   template.sendBody("direct:foo", "Hello Foo");

   System.clearProperty("cool.end");
   System.clearProperty("cool.result");
        
   assertMockEndpointsSatisfied();

Using property placeholders for any kind of attribute in the XML DSL

Previously it was only the xs:string type attributes in the XML DSL that support placeholders. For example often a timeout attribute would be a xs:int type and thus you cannot set a string value as the placeholder key. This is possible using a special placeholder namespace.

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 Multicast to indicate that the option stopOnException should be the value of the placeholder with the key "stop".

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:prop="http://camel.apache.org/schema/placeholder"
   xsi:schemaLocation="
      http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd
      http://camel.apache.org/schema/spring 
         http://camel.apache.org/schema/spring/camel-spring.xsd">

   <!-- Notice in the declaration above, we have defined the prop -->
   <!-- prefix as the Camel placeholder namespace -->

   <bean id="damn" class="java.lang.IllegalArgumentException">
      <constructor-arg index="0" value="Damn"/>
   </bean>

   <camelContext xmlns="http://camel.apache.org/schema/spring">

   <propertyPlaceholder id="properties" location=
      "classpath:org/apache/camel/component/properties/myprop.properties"
      xmlns="http://camel.apache.org/schema/spring"/>

      <route>
         <from uri="direct:start"/>
         <!-- use prop namespace, to define a property placeholder, 
              which maps to option stopOnException={{stop}} -->
         <multicast prop:stopOnException="stop">
            <to uri="mock:a"/>
            <throwException ref="damn"/>
            <to uri="mock:b"/>
         </multicast>
      </route>

   </camelContext>

</beans>

In our properties file we have the value defined as

stop=true

Using property placeholder in the Java DSL

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

from("direct:start")
   // use a property placeholder for the option stopOnException on the 
   // Multicast EIP which should have the value of {{stop}}  
   // key being looked up in the properties file
   .multicast()
      .placeholder("stopOnException", "stop")
      .to("mock:a")
      .throwException(new IllegalAccessException("Damn"))
      .to("mock:b");

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:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
   xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 
      http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

   <!-- OSGI blueprint property placeholder -->
   <cm:property-placeholder id="myblueprint.placeholder" 
      persistent-id="camel.blueprint">
   <!-- list some properties for this test -->
   <cm:default-properties>
      <cm:property name="result" value="mock:result"/>
   </cm:default-properties>
   </cm:property-placeholder>

   <camelContext xmlns="http://camel.apache.org/schema/blueprint">

   <!-- in the route we can use {{ }} placeholders which we'll -->
   <!-- lookup in blueprint as Camel will auto detect the OSGi -->
   <!-- blueprint property placeholder and use it -->
      <route>
         <from uri="direct:start"/>
         <to uri="mock:foo"/>
         <to uri="{{result}}"/>
      </route>
   </camelContext>
</blueprint>

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.

Important

Notice how we can use the Camel syntax for placeholders {{ }} in the Camel route, which will lookup the value from OSGi blueprint. The blueprint syntax for placeholders is ${ }. So outside the <camelContext> you must use the ${ } syntax. Whereas inside <camelContext> you must use {{ }} syntax. OSGi blueprint allows you to configure the syntax, so you can align those if you want.

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:

<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
   xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/
blueprint/v1.0.0/blueprint.xsd">

   <!-- OSGI blueprint property placeholder -->
   <cm:property-placeholder id="myblueprint.placeholder" 
      persistent-id="camel.blueprint">
      <!-- list some properties for this test -->
      <cm:default-properties>
         <cm:property name="result" value="mock:result"/>
      </cm:default-properties>
   </cm:property-placeholder>

   <camelContext xmlns="http://camel.apache.org/schema/blueprint">

      <!-- using Camel properties component and refer to the  -->
      <!-- blueprint property placeholder by its id -->
      <propertyPlaceholder id="properties" 
         location="blueprint:myblueprint.placeholder"/>

      <!-- in the route we can use {{ }} placeholders which we'll -->
      <!-- look up in blueprint -->
      <route>
         <from uri="direct:start"/>
         <to uri="mock:foo"/>
         <to uri="{{result}}"/>
      </route>

   </camelContext>

</blueprint>

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:

location="blueprint:myblueprint.placeholder,
   classpath:myproperties.properties"

Each location is separated by comma.