...
The example below has property placeholder in the <jmxAgent> tag: {snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/ Wiki Markup
Code Block | ||
---|---|---|
| ||
<camelContext xmlns="http://camel.apache.org/schema/spring"> <propertyPlaceholder id="properties" location="org/apache/camel/spring/ |
...
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
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. An example of this is given below
Code Block |
---|
PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class); pc.setCache(false); 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" System.setProperty("cool.end", "mock:override"); System.setProperty("cool.result", "override"); context.addRoutes(new RouteBuilder() { statisticsLevel="RoutesOnly" @Override public void configure() throws Exception { useHostIPAddress="true"/> <route from("direct:start").to("properties:cool.end");id="foo" autoStartup="false"> <from uri="seda:start"/> from("direct:foo").to("properties:mock:{{cool.result}}");<to uri="mock: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");</route> </camelContext> |
You can also define property placeholders in the various attributes on the <camelContext> tag such as trace
as shown here:
Code Block | ||
---|---|---|
| ||
<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> assertMockEndpointsSatisfied(); |
Using property placeholders for any kind of attribute in the XML DSL
Available as of Camel 2.7
Info |
---|
If you use OSGi Blueprint then this only works from 2.11.1 or 2.10.5 onwards. |
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 now possible from Camel 2.7 onwards 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 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} |
Code Block |
---|
stop=true
|
Using property placeholder in the Java DSL
Available as of Camel 2.7
...
<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
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. An example of this is given below
Code Block |
---|
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
Available as of Camel 2.7
Info |
---|
If you use OSGi Blueprint then this only works from 2.11.1 or 2.10.5 onwards. |
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 now possible from Camel 2.7 onwards 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 the Multicast to indicate that the option stopOnException
should be the value of the placeholder with the key "stop".
Code Block | ||
---|---|---|
| ||
<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
Code Block |
---|
stop=true
|
Using property placeholder in the Java DSL
Available as of Camel 2.7
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 | ||
---|---|---|
| ||
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
...