Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading multiple properties files with Camel
    primarykey
    data
    text
    <p>So I'll admit I have a lot going on in my applicationContext.xml, but I'm not sure why I can't add in another properties file called publish.properties, and use it in a similar way to a config.properties I have now. I also have a sensor.properties but its a group of keyvalue pairs (and in any case they load fine too).</p> <p>I've been reading the Camel properties doc i.e., <a href="http://camel.apache.org/properties.html" rel="nofollow">http://camel.apache.org/properties.html</a> but its still not clear to me how I specify multiple properties files so that Camel can resolve them.</p> <p>Here is my current applicationContext.xml that runs, and injects config.properties and sensor.properties just fine:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /&gt; &lt;context:component-scan base-package="com.data.world2" /&gt; &lt;context:annotation-config /&gt; &lt;camel:camelContext id="HelloWorldContext"&gt; &lt;!-- Add Jackson library to render Java Map into JSON --&gt; &lt;camel:dataFormats&gt; &lt;camel:json id="jack" library="Jackson"/&gt; &lt;/camel:dataFormats&gt; &lt;camel:route&gt; &lt;!-- sends {{config.numSamples}} request(s) to the hello world JMS queue every {{config.timeout}} seconds --&gt; &lt;camel:from uri="timer://hello.world.request.timer?fixedRate=true&amp;amp;period={{config.timeout}}&amp;amp;repeatCount={{config.numSamples}}"/&gt; &lt;camel:to uri="log:hello.world.request?level=INFO&amp;amp;showAll=true" /&gt; &lt;camel:bean ref="helloWorld" /&gt; &lt;!-- now print out the map in JSON format --&gt; &lt;camel:marshal ref ="jack"/&gt; &lt;camel:convertBodyTo type="java.lang.String" /&gt; &lt;camel:log message="${body}"/&gt; &lt;!-- print out message that we are returning sensor event in JSON --&gt; &lt;camel:log message="Returned Random Sensor Event in JSON"/&gt; &lt;!-- print out values read from config.properties file --&gt; &lt;camel:log message="printing values read from config.properties file"/&gt; &lt;camel:log message=" config.timeout= {{config.timeout}} milliseconds"/&gt; &lt;camel:log message=" config.numSamples= {{config.numSamples}} random Sensor Event(s) ## NOTE: 0 or -1 means generate forever ##"/&gt; &lt;camel:log message=" config.defaultViz= {{config.defaultViz}}"/&gt; &lt;!-- now log the message --&gt; &lt;camel:to uri="log:hello.world.response?level=INFO&amp;amp;showAll=true" /&gt; &lt;!-- now send the message to the JMS queue --&gt; &lt;camel:to uri="jms:queue:helloworld.response" /&gt; &lt;/camel:route&gt; &lt;/camel:camelContext&gt; &lt;!-- creates a java.util.Properties instance with values loaded from the supplied location --&gt; &lt;util:properties id="sensorProperties" location="classpath:/sensor.properties"/&gt; &lt;!-- pass in sensor.properties and defaultViz from config.properties --&gt; &lt;bean class="com.data.world2.SensorEventStore"&gt; &lt;property name="sourceProperties" ref="sensorProperties" /&gt; &lt;property name="defaultViz" value="${config.defaultViz}"/&gt; &lt;/bean&gt; &lt;!-- declare a Spring bean to use the Camel Properties component in Spring XML --&gt; &lt;bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent"&gt; &lt;property name="location" value="classpath:config.properties"/&gt; &lt;/bean&gt; &lt;!-- bridge spring property placeholder with Camel --&gt; &lt;!-- you must NOT use the &lt;context:property-placeholder at the same time, only this bridge bean --&gt; &lt;bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer"&gt; &lt;property name="location" value="classpath:config.properties"/&gt; &lt;/bean&gt; &lt;bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent"&gt; &lt;property name="configuration" ref="jmsConfig" /&gt; &lt;/bean&gt; &lt;bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"&gt; &lt;property name="connectionFactory" ref="jmsConnectionFactory" /&gt; &lt;property name="transacted" value="false" /&gt; &lt;property name="concurrentConsumers" value="1" /&gt; &lt;/bean&gt; &lt;bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"&gt; &lt;property name="brokerURL" value="vm://localhost" /&gt; &lt;property name="redeliveryPolicy" ref="redeliveryPolicy" /&gt; &lt;property name="prefetchPolicy" ref="prefetchPolicy" /&gt; &lt;/bean&gt; &lt;bean id="prefetchPolicy" class="org.apache.activemq.ActiveMQPrefetchPolicy"&gt; &lt;property name="queuePrefetch" value="5" /&gt; &lt;/bean&gt; &lt;bean id="redeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy"&gt; &lt;property name="maximumRedeliveries" value="1" /&gt; &lt;property name="backOffMultiplier" value="2" /&gt; &lt;property name="initialRedeliveryDelay" value="2000" /&gt; &lt;property name="useExponentialBackOff" value="true" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>I've been trying to add in the following bean in my applicationContext.xml to inject six fields from my publish.properties file into setters/getters in my RandomEventGenerator class:</p> <pre><code>&lt;!-- pass in publish.properties to RandomEventGenerator --&gt; &lt;bean class="com.data.world2.RandomEventGenerator"&gt; &lt;property name="makePub" value="${publish.makePub}"/&gt; &lt;property name="modelPub" value="${publish.modelPub}"/&gt; &lt;property name="serialNumberPub" value="${publish.serialNumberPub}"/&gt; &lt;property name="firmwareRevPub" value="${publish.firmwareRevPub}"/&gt; &lt;property name="sensorTypePub" value="${publish.sensorTypePub}"/&gt; &lt;property name="payloadPub" value="${publish.payloadPub}"/&gt; &lt;/bean&gt; </code></pre> <p>along with a variation of the "properties" bean id:</p> <pre><code> &lt;!-- declare a Spring bean to use the Camel Properties component in Spring XML --&gt; &lt;bean id="publishProperties" class="org.apache.camel.component.properties.PropertiesComponent"&gt; &lt;property name="location" value="classpath:publish.properties"/&gt; &lt;/bean&gt; </code></pre> <p>I also tried to combine both properties files inside my properties bean ID as follows:</p> <pre><code> &lt;bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent"&gt; &lt;property name="locations" value="classpath:config.properties,classpath:publish.properties"/&gt; &lt;/bean&gt; </code></pre> <p>but I still get the following error:</p> <pre><code>Could not resolve placeholder 'publish.makePub' in string value "${publish.makePub}" </code></pre> <p>But I do remember that I had to add in the bridgePropertyPlaceholder to get the config.properties to work from Spring. Is there a way I can share this bridge for both config.properties and publish.properties? Or is there an easier way to do this?</p> <h1>Updated per feedback from Frederic</h1> <p>Here's my updated applicationContext.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /&gt; &lt;context:component-scan base-package="com.data.world2" /&gt; &lt;context:annotation-config /&gt; &lt;camel:camelContext id="HelloWorldContext"&gt; &lt;!-- Add Jackson library to render Java Map into JSON --&gt; &lt;camel:dataFormats&gt; &lt;camel:json id="jack" library="Jackson"/&gt; &lt;/camel:dataFormats&gt; &lt;camel:route&gt; &lt;!-- sends {{config.numSamples}} request(s) to the hello world JMS queue every {{config.timeout}} seconds --&gt; &lt;camel:from uri="timer://hello.world.request.timer?fixedRate=true&amp;amp;period={{config.timeout}}&amp;amp;repeatCount={{config.numSamples}}"/&gt; &lt;camel:to uri="log:hello.world.request?level=INFO&amp;amp;showAll=true" /&gt; &lt;camel:bean ref="helloWorld" /&gt; &lt;!-- now print out the map in JSON format --&gt; &lt;camel:marshal ref ="jack"/&gt; &lt;camel:convertBodyTo type="java.lang.String" /&gt; &lt;camel:log message="${body}"/&gt; &lt;!-- print out message that we are returning sensor event in JSON --&gt; &lt;camel:log message="Returned Random Sensor Event in JSON"/&gt; &lt;!-- print out values read from config.properties file --&gt; &lt;camel:log message="printing values read from config.properties file"/&gt; &lt;camel:log message=" config.timeout= {{config.timeout}} milliseconds"/&gt; &lt;camel:log message=" config.numSamples= {{config.numSamples}} random Sensor Event(s) ## NOTE: 0 or -1 means generate forever ##"/&gt; &lt;camel:log message=" config.defaultViz= {{config.defaultViz}}"/&gt; &lt;!-- now log the message --&gt; &lt;camel:to uri="log:hello.world.response?level=INFO&amp;amp;showAll=true" /&gt; &lt;!-- now send the message to the JMS queue --&gt; &lt;camel:to uri="jms:queue:helloworld.response" /&gt; &lt;/camel:route&gt; &lt;/camel:camelContext&gt; &lt;!-- creates a java.util.Properties instance with values loaded from the supplied location --&gt; &lt;util:properties id="sensorProperties" location="classpath:/sensor.properties"/&gt; &lt;!-- pass in sensor.properties and defaultViz from config.properties --&gt; &lt;bean class="com.data.world2.sensor.SensorEventStore"&gt; &lt;property name="sourceProperties" ref="sensorProperties" /&gt; &lt;property name="defaultViz" value="${config.defaultViz}"/&gt; &lt;/bean&gt; &lt;!-- pass in publish.properties to RandomEventGenerator --&gt; &lt;bean class="com.data.world2.RandomEventGenerator"&gt; &lt;property name="makePub" value="${publish.makePub}"/&gt; &lt;property name="modelPub" value="${publish.modelPub}"/&gt; &lt;property name="serialNumberPub" value="${publish.serialNumberPub}"/&gt; &lt;property name="firmwareRevPub" value="${publish.firmwareRevPub}"/&gt; &lt;property name="sensorTypePub" value="${publish.sensorTypePub}"/&gt; &lt;property name="payloadPub" value="${publish.payloadPub}"/&gt; &lt;/bean&gt; &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt; &lt;property name="ignoreResourceNotFound" value="false"/&gt; &lt;property name="locations"&gt; &lt;list&gt; &lt;value&gt;classpath:config.properties&lt;/value&gt; &lt;value&gt;classpath:publish.properties&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;!-- bridge spring property placeholder with Camel --&gt; &lt;!-- you must NOT use the &lt;context:property-placeholder at the same time, only this bridge bean --&gt; &lt;bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer"&gt; &lt;property name="location" value="classpath:config.properties"/&gt; &lt;/bean&gt; &lt;bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent"&gt; &lt;property name="configuration" ref="jmsConfig" /&gt; &lt;/bean&gt; &lt;bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"&gt; &lt;property name="connectionFactory" ref="jmsConnectionFactory" /&gt; &lt;property name="transacted" value="false" /&gt; &lt;property name="concurrentConsumers" value="1" /&gt; &lt;/bean&gt; &lt;bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"&gt; &lt;property name="brokerURL" value="vm://localhost" /&gt; &lt;property name="redeliveryPolicy" ref="redeliveryPolicy" /&gt; &lt;property name="prefetchPolicy" ref="prefetchPolicy" /&gt; &lt;/bean&gt; &lt;bean id="prefetchPolicy" class="org.apache.activemq.ActiveMQPrefetchPolicy"&gt; &lt;property name="queuePrefetch" value="5" /&gt; &lt;/bean&gt; &lt;bean id="redeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy"&gt; &lt;property name="maximumRedeliveries" value="1" /&gt; &lt;property name="backOffMultiplier" value="2" /&gt; &lt;property name="initialRedeliveryDelay" value="2000" /&gt; &lt;property name="useExponentialBackOff" value="true" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>Here's the publish.properties file I'm using:</p> <pre><code>publish.makePub=true publish.modelPub=true publish.serialNumberPub=true publish.firmwareRevPub=true publish.sensorTypePub=false publish.payloadPub=true </code></pre> <p>And here are the String Getters and Setters in my RandomEventGenerator class:</p> <pre><code> // getters and setters public String getMakePub() { return makePub; } public void setMakePub(String makePub) { this.makePub = makePub; } public String getModelPub() { return modelPub; } public void setModelPub(String modelPub) { this.modelPub = modelPub; } public String getSerialNumberPub() { return serialNumberPub; } public void setSerialNumberPub(String serialNumberPub) { this.serialNumberPub = serialNumberPub; } public String getFirmwareRevPub() { return firmwareRevPub; } public void setFirmwareRevPub(String firmwareRevPub) { this.firmwareRevPub = firmwareRevPub; } public String getSensorTypePub() { return sensorTypePub; } public void setSensorTypePub(String sensorTypePub) { this.sensorTypePub = sensorTypePub; } public String getPayloadPub() { return payloadPub; } public void setPayloadPub(String payloadPub) { this.payloadPub = payloadPub; } </code></pre> <h1>Updated application.xml per feedback from Claus {and trial and error :-)}</h1> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /&gt; &lt;context:component-scan base-package="com.data.world2" /&gt; &lt;context:annotation-config /&gt; &lt;camel:camelContext id="HelloWorldContext"&gt; &lt;!-- Add Jackson library to render Java Map into JSON --&gt; &lt;camel:dataFormats&gt; &lt;camel:json id="jack" library="Jackson"/&gt; &lt;/camel:dataFormats&gt; &lt;camel:route&gt; &lt;!-- sends {{config.numSamples}} request(s) to the hello world JMS queue every {{config.timeout}} seconds --&gt; &lt;camel:from uri="timer://hello.world.request.timer?fixedRate=true&amp;amp;period={{config.timeout}}&amp;amp;repeatCount={{config.numSamples}}"/&gt; &lt;camel:to uri="log:hello.world.request?level=INFO&amp;amp;showAll=true" /&gt; &lt;camel:bean ref="helloWorld" /&gt; &lt;!-- now print out the ArrayList in JSON format --&gt; &lt;camel:marshal ref ="jack"/&gt; &lt;camel:convertBodyTo type="java.lang.String" /&gt; &lt;camel:log message="${body}"/&gt; &lt;!-- print out message that we are returning sensor event in JSON --&gt; &lt;camel:log message="Returned Random Sensor Event in JSON"/&gt; &lt;!-- print out values read from config.properties file --&gt; &lt;camel:log message="printing values read from config.properties file"/&gt; &lt;camel:log message=" config.timeout= {{config.timeout}} milliseconds"/&gt; &lt;camel:log message=" config.numSamples= {{config.numSamples}} random Sensor Event(s) ## NOTE: 0 or -1 means generate forever ##"/&gt; &lt;camel:log message=" config.defaultViz= {{config.defaultViz}}"/&gt; &lt;!-- now log the message --&gt; &lt;camel:to uri="log:hello.world.response?level=INFO&amp;amp;showAll=true" /&gt; &lt;!-- now send the message to the JMS queue --&gt; &lt;!-- &lt;camel:to uri="jms:queue:helloworld.response" /&gt; --&gt; &lt;/camel:route&gt; &lt;/camel:camelContext&gt; &lt;!-- creates a java.util.Properties instance with values loaded from the supplied location --&gt; &lt;util:properties id="sensorProperties" location="classpath:/sensor.properties"/&gt; &lt;!-- pass in sensor.properties and defaultViz from config.properties --&gt; &lt;bean class="com.data.world2.sensor.SensorEventStore"&gt; &lt;property name="sourceProperties" ref="sensorProperties" /&gt; &lt;property name="defaultViz" value="${config.defaultViz}"/&gt; &lt;/bean&gt; &lt;!-- creates a java.util.Properties instance with values loaded from the supplied location --&gt; &lt;util:properties id="admissionProperties" location="classpath:/admission.properties"/&gt; &lt;!-- pass in admission.properties and defaultViz from config.properties --&gt; &lt;bean class="com.data.world2.admission.AdmissionEventStore"&gt; &lt;property name="sourceAdmissionProperties" ref="admissionProperties" /&gt; &lt;property name="defaultViz" value="${config.defaultViz}"/&gt; &lt;/bean&gt; &lt;bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent"&gt; &lt;property name="locations"&gt; &lt;list&gt; &lt;value&gt;classpath:config.properties&lt;/value&gt; &lt;value&gt;classpath:publish.properties&lt;/value&gt; &lt;value&gt;classpath:admission.properties&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;!-- bridge spring property placeholder with Camel --&gt; &lt;!-- you must NOT use the &lt;context:property-placeholder at the same time, only this bridge bean --&gt; &lt;bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer"&gt; &lt;property name="locations"&gt; &lt;list&gt; &lt;value&gt;classpath:config.properties&lt;/value&gt; &lt;value&gt;classpath:publish.properties&lt;/value&gt; &lt;value&gt;classpath:admission.properties&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;!-- pass in publish.properties to RandomEventGenerator --&gt; &lt;bean class="com.data.world2.RandomEventGenerator"&gt; &lt;property name="makePub" value="${publish.makePub}"/&gt; &lt;property name="modelPub" value="${publish.modelPub}"/&gt; &lt;property name="serialNumberPub" value="${publish.serialNumberPub}"/&gt; &lt;property name="firmwareRevPub" value="${publish.firmwareRevPub}"/&gt; &lt;property name="sensorTypePub" value="${publish.sensorTypePub}"/&gt; &lt;property name="payloadPub" value="${publish.payloadPub}"/&gt; &lt;/bean&gt; &lt;!-- &lt;bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent"&gt; --&gt; &lt;!-- &lt;property name="configuration" ref="jmsConfig" /&gt; --&gt; &lt;!-- &lt;/bean&gt; --&gt; &lt;!-- &lt;bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration"&gt; --&gt; &lt;!-- &lt;property name="connectionFactory" ref="jmsConnectionFactory" /&gt; --&gt; &lt;!-- &lt;property name="transacted" value="false" /&gt; --&gt; &lt;!-- &lt;property name="concurrentConsumers" value="1" /&gt; --&gt; &lt;!-- &lt;/bean&gt; --&gt; &lt;!-- &lt;bean id="jmsConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"&gt; --&gt; &lt;!-- &lt;property name="brokerURL" value="vm://localhost" /&gt; --&gt; &lt;!-- &lt;property name="redeliveryPolicy" ref="redeliveryPolicy" /&gt; --&gt; &lt;!-- &lt;property name="prefetchPolicy" ref="prefetchPolicy" /&gt; --&gt; &lt;!-- &lt;/bean&gt; --&gt; &lt;!-- &lt;bean id="prefetchPolicy" class="org.apache.activemq.ActiveMQPrefetchPolicy"&gt; --&gt; &lt;!-- &lt;property name="queuePrefetch" value="5" /&gt; --&gt; &lt;!-- &lt;/bean&gt; --&gt; &lt;!-- &lt;bean id="redeliveryPolicy" class="org.apache.activemq.RedeliveryPolicy"&gt; --&gt; &lt;!-- &lt;property name="maximumRedeliveries" value="1" /&gt; --&gt; &lt;!-- &lt;property name="backOffMultiplier" value="2" /&gt; --&gt; &lt;!-- &lt;property name="initialRedeliveryDelay" value="2000" /&gt; --&gt; &lt;!-- &lt;property name="useExponentialBackOff" value="true" /&gt; --&gt; &lt;!-- &lt;/bean&gt; --&gt; &lt;/beans&gt; </code></pre> <h1>Final Fix</h1> <p>The final issue was that I didn't have a separate @Autowired to inject both classes. Once I changed the code from this:</p> <pre><code> @Autowired SensorEventStore sensorEventStore; AdmissionEventStore admissionEventStore; </code></pre> <p>to this:</p> <pre><code> @Autowired SensorEventStore sensorEventStore; @Autowired AdmissionEventStore admissionEventStore; </code></pre> <p>everything injected properly. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload