Search code examples
apache-camelspring-camel

How to use properties with the SimpleRegistry in Apache Camel (Spring XML)


I want to use a SimpleRegistry to store properties (as global variables). The property is changed with setProperty in a route with a jms endpoint. The camel documentation changed last week and has many dead links, also the Registry page. I did not found any samples that describe the use of the simpleRegistry.

I used the camel-example-servlet-tomcat as base. I do not use Fuse or the patched camel wildfly, because is to huge for our simple module.

<beans .... >
   .
   .
   .
  <bean id="simpleRegistry" class="org.apache.camel.support.SimpleRegistry" />

  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <propertyPlaceholder id="properties" location="ref:simpleRegistry" />
    <route id="storeConfig">
      <from id="myTopic" uri="jms:topic:myTopic?selector=Configuration %3D 'xyz'" />
      <log id="printHeader2" message="Received header: ${headers}" />
      <log id="logToken" message="Received token: ${headers[myToken]}" />
      <setProperty id="setMyToken" name="myProperty">
        <simple>${headers[myToken]}</simple>
      </setProperty>
    </route>
    <route id="externalIncomingDataRoute">
      <from uri="servlet:hello" />
      <transform>
            <simple>The Token is: {{myProperty}}</simple>
      </transform>
    </route>
  </camelContext>
</beans>

With the camel context deined like above, I got a java.io.FileNotFoundException Properties simpleRegistry not found in registry.

When I use <propertyPlaceholder id="properties" location="classpath:test.properties" /> and create a test.properties file, everything works fine but I cannot change the property. The operation in the setProperty tag is ignored.

The reason why I need a global variable is, I send a dynamic configuration (the myToken) via a jms topic to the camel context. A single route should store this configuration globaly. If an other route is called via an rest component, this route need the token to make a choice.


Solution

  • Alternatively you can achieve the same result following the below approach which uses the PropertiesComponent

    <bean id="applicationProperties" class="java.util.Properties"/>
    
    <bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
        <property name="location" value="classpath:application.properties"/>
        <property name="overrideProperties" ref="applicationProperties" />
    </bean> 
    

    Define the property place holder in the camel context:

    <propertyPlaceholder id="propertiesRef" location="ref:applicationProperties" />
    

    Set a property as shown below :

    <bean ref="applicationProperties" method="setProperty(token, 'Test'})" />
    

    And to fetch the property : ${properties:token}