Versions Compared

Key

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

...

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.

Code Block
languagejava
// 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:

Code Block
languagejava
// properties
cool.foo=result

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

...

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

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

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

...

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

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

...

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

Code Block
languagejava
// properties
cool.foo=result

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

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

Code Block
languagejava
// 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:

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

...

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

Code Block
languagejava
// properties
cheese.quote=Camel rocks

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

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

Code Block
languagejava
// bar.properties
bar.quote=Beer tastes good

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

...

Code Block
languagexml
<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"
              useHostIPAddress="true"/>

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

...

Available as of Camel 2.5
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.

Example:

Code Block
languagejava
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();

...

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

...

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

Code Block
languagexmljava
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");

...

Take notice when using Spring bridging placeholder then the spring ${} syntax clashes with the Simple in Camel, and therefore take care.

Example:

Code Block
languagexml
<setHeader headerName="Exchange.FILE_NAME">
  <simple>{{file.rootdir}}/${in.header.CamelFileName}</simple>
</setHeader>

clashes with Spring property placeholders, and you should use $simple{} to indicate using the Simple language in Camel.

Code Block
languagexml
<setHeader headerName="Exchange.FILE_NAME">
  <simple>{{file.rootdir}}/$simple{in.header.CamelFileName}</simple>
</setHeader>

...

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.

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.

...

Code Block
java
java
public class MyRouteBuilder extends RouteBuilder {

    @PropertyInject("hello")
    private String greeting;

    @Override
    public void configure() throws Exception {
        from("direct:start")
            .transform().constant(greeting)
            .to("{{result}}");
    }

}

Notice we have annotated the greeting field with @PropertyInject and define it to use the key hello. Camel will then lookup the property with this key and inject its value, converted to a String type.

You can also use multiple placeholders and text in the key, for example we can do:

Code Block
languagejava
    @PropertyInject("Hello {{name}} how are you?")
    private String greeting;

This will lookup the placeholder with they key name.

You can also add a default value if the key does not exists, such as:

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

Using Out of the Box Functions

...

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:

Code Block
languagexml
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
  <route>
    <from uri="direct:start"/>
    <to uri="{{env:SOMENAME}}"/>
    <to uri="{{sys:MyJvmPropertyName}}"/>
  </route>
</camelContext>

You can use default values as well, so if the property does not exists, you can define a default value as shown below, where the default value is a log:foo and log:bar value.

Code Block
languagexml
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
  <route>
    <from uri="direct:start"/>
    <to uri="{{env:SOMENAME:log:foo}}"/>
    <to uri="{{sys:MyJvmPropertyName:log:bar}}"/>
  </route>
</camelContext>

...

For example if the FOO service a remote HTTP service, then we can refer to the service in the Camel endpoint URI, and use the HTTP component to make the HTTP call:

Code Block
languagexml
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
  <route>
    <from uri="direct:start"/>
    <to uri="http://{{service:FOO}}/myapp"/>
  </route>
</camelContext>

...

And we can use default values if the service has not been defined, for example to call a service on localhost, maybe for unit testing etc:

Code Block
languagexml
<camelContext xmlns="http://camel.apache.org/schema/blueprint">
  <route>
    <from uri="direct:start"/>
    <to uri="http://{{service:FOO:localhost:8080}}/myapp"/>
  </route>
</camelContext>

...

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

...

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

Code Block
languagejava
public static final class MyBeerFunction implements PropertiesFunction {
  @Override
  public String getName() {
    return "beer";
  }
  @Override
  public String apply(String remainder) {
    return "mock:" + remainder.toLowerCase();
  }
}

...

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

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

...