6

I have been reading the following page on Camel properties: http://camel.apache.org/using-propertyplaceholder.html and also reading the book "Camel In Action".

I found Chapter 6 of "Camel In Action" very helpful in defining Camel properties, and I can load the following three properties from my config.properties:

config.timeout=10000
config.numSamples=1000
config.defaultViz=a

When I run my Java code I'm able to see the following three values inside my camel route in my applicationContext.xml, as shown in the thread#0 messages below:

14669 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO  route1  - printing values read from config.properties file
14669 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO  route1  - config.timeout= 10000
14669 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO  route1  - config.numSamples= 1000
14670 [Camel (HelloWorldContext) thread #0 - timer://hello.world.request.timer] INFO  route1  - config.defaultViz= a

However, when I try to pass the variable {{config.defaultViz}} to a String called defaultViz in my SensorGenerator Java class, and print that string I get "{{config.defaultViz}}" on the console instead of the value contained within {{config.defaultViz}}.

In other words, here's what I see on the screen:

Returning List
defaultViz= {{config.defaultViz}}

But I really want to see this on the screen:

Returning List
defaultViz=a

So what am I doing wrong in my applicationContext.xml?

UPDATED: The issue was that I needed to add a Bridge between Spring and Camel as outlined in the link I referenced above.

Here's my UPDATED applicationContext.xml with the bridge:

<?xml version="1.0" encoding="UTF-8"?>

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

    <bean
            class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
    <context:component-scan base-package="com.data.world2" />
    <context:annotation-config />

    <camel:camelContext id="HelloWorldContext">

<!--        Add Jackson library to render Java Map into JSON -->
        <camel:dataFormats>
          <camel:json id="jack" library="Jackson"/>
        </camel:dataFormats>

        <camel:route>
            <!-- sends a request to the hello world JMS queue every 10 seconds -->
            <camel:from
                uri="timer://hello.world.request.timer?fixedRate=true&amp;period={{config.timeout}}" />
            <camel:to uri="log:hello.world.request?level=INFO&amp;showAll=true" />
            <camel:bean ref="helloWorld" />

            <!-- now print out the map in JSON format -->
            <camel:marshal ref ="jack"/>
            <camel:convertBodyTo type="java.lang.String" />
            <camel:log message="${body}"/> 

            <!-- print out values read from config.properties file -->
            <camel:log message="printing values read from config.properties file"/>
            <camel:log message="config.timeout= {{config.timeout}}"/> 
            <camel:log message="config.numSamples= {{config.numSamples}}"/>
            <camel:log message="config.defaultViz= {{config.defaultViz}}"/>

            <!-- now log the message -->
            <camel:to uri="log:hello.world.response?level=INFO&amp;showAll=true" />

        </camel:route>

    </camel:camelContext>

<!-- creates a java.util.Properties instance with values loaded from the supplied location -->
<util:properties id="sensorProperties" location="classpath:/sensor.properties"/>

    <!--  pass in sensor.properties and defaultViz from config.properties -->
    <bean class="com.data.world2.SensorGenerator">
        <property name="sourceProperties" ref="sensorProperties" />
        <property name="defaultViz" value="${config.defaultViz}"/>
    </bean>

<!-- declare a Spring bean to use the Camel Properties component in Spring XML -->
    <bean id="properties"
          class="org.apache.camel.component.properties.PropertiesComponent">
        <property name="location" value="classpath:config.properties"/>
    </bean>
<!-- bridge spring property placeholder with Camel -->
<!-- you must NOT use the <context:property-placeholder at the same time, only this bridge bean -->
    <bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
      <property name="location" value="classpath:config.properties"/>
    </bean>

</beans>

I found this question that is similar but not quite the same: Injecting property into bean

Community
  • 1
  • 1
erj2code
  • 301
  • 5
  • 9
  • 19

1 Answers1

13

The {{}} notation just works inside the routes (ie inside the XML camel contexts). To use it in the bean I think you need to define the property placeholder bridge that camel provides but in your bean use the ${} notation. The explanation of how to use that bridge is in the link you have provided.

hveiga
  • 6,725
  • 7
  • 54
  • 78
  • 2
    Thanks! That worked. I added the Bridge and now I get my defaultViz. I'll update the applicationContext.xml to what I now have . Maybe it will be useful to someone else in the future. :-) – erj2code Oct 19 '13 at 00:57
  • Its pretty strange that all the properties in etc/ folder is not visible to spring context. It seems that Spring context and blueprint context are loaded independently. Even the bean declared in the Blueprint is not visible in spring context. looks like bridge is only way for Spring context i.e. – R-JANA Jan 27 '16 at 11:25