You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "jason.parr" <ja...@usa.net> on 2011/08/16 16:15:09 UTC

Configuring property locations dynamically at runtime

I want to configure the camel context's list of property file locations
dynamically. Basically each of my environments (dev,stage,prod) have a
particular hierachy of property file overrides and the list has to be
determined at runtime.

Rather than as below statically listing them in my spring config file:

	  <bean id="properties"
class="org.apache.camel.component.properties.PropertiesComponent">
	    <property name="locations"
value="file:config/config.properties,file:config/config-dev.properties" />
	  </bean>

I want to calculate a dynamic list, I've worked out how todo this when using
CamelTestSupport by overriding createCamelContext.

	  @Override
	  protected CamelContext createCamelContext() throws Exception {
	    CamelContext context = super.createCamelContext();

	    // Load test specific properties
	    PropertiesComponent properties =
context.getComponent("properties",PropertiesComponent.class);
	    properties.setLocations(myDynamicLocations);
      

But how can I do this from my actual route that extends RouteBuilder?, I
tried overriding createContainer but this doesnt seemed to get called.

	public class RadialRouteBuilder extends RouteBuilder {


	  /**
	   * A main() so we can easily run these routing rules in our IDE
	   *
	   * @param args command line arguments
	   * @throws Exception exception that terminated running of main thread
	   */
	  public static void main(String... args) throws Exception {
	    Main.main(args);
	  }

	  @Override
	  protected CamelContext createContainer() {
	    CamelContext context = super.createContainer();

	    PropertiesComponent properties = new PropertiesComponent();
	    properties.setLocations(myDynamicLocations);
	    context.addComponent("properties", properties);

	    return context;
	  }

This just ends with me getting exception:

Caused by: java.lang.IllegalArgumentException: PropertiesComponent with name
properties must be defined in CamelContext to support property placeholders.
	at
org.apache.camel.impl.DefaultCamelContext.resolvePropertyPlaceholders(DefaultCamelContext.java:969)
	at
org.apache.camel.model.ProcessorDefinition.resolvePropertyPlaceholders(ProcessorDefinition.java:500)
	at
org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:426)
	at
org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:183)
	at
org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:791)
	... 26 more

I may be trying to use a sledge hammer to crack a nut, so there may be an
easier way of doing this? The main point is that the exact list of
properties files are not known until runtime, this may be unusual but is
just the way it is!

--
View this message in context: http://camel.465427.n5.nabble.com/Configuring-property-locations-dynamically-at-runtime-tp4704549p4704549.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Configuring property locations dynamically at runtime

Posted by Willem Jiang <wi...@gmail.com>.
Hi,

I suggest you to create the CamelContext yourself instead of using the 
Main from camel.
It could be more handy for you to inject the customer properties component.


On Tue Aug 16 22:15:09 2011, jason.parr wrote:
> I want to configure the camel context's list of property file locations
> dynamically. Basically each of my environments (dev,stage,prod) have a
> particular hierachy of property file overrides and the list has to be
> determined at runtime.
>
> Rather than as below statically listing them in my spring config file:
>
> 	<bean id="properties"
> class="org.apache.camel.component.properties.PropertiesComponent">
> 	<property name="locations"
> value="file:config/config.properties,file:config/config-dev.properties" />
> 	</bean>
>
> I want to calculate a dynamic list, I've worked out how todo this when using
> CamelTestSupport by overriding createCamelContext.
>
> 	  @Override
> 	  protected CamelContext createCamelContext() throws Exception {
> 	    CamelContext context = super.createCamelContext();
>
> 	    // Load test specific properties
> 	    PropertiesComponent properties =
> context.getComponent("properties",PropertiesComponent.class);
> 	    properties.setLocations(myDynamicLocations);
>
>
> But how can I do this from my actual route that extends RouteBuilder?, I
> tried overriding createContainer but this doesnt seemed to get called.
>
> 	public class RadialRouteBuilder extends RouteBuilder {
>
>
> 	  /**
> 	   * A main() so we can easily run these routing rules in our IDE
> 	   *
> 	   * @param args command line arguments
> 	   * @throws Exception exception that terminated running of main thread
> 	   */
> 	  public static void main(String... args) throws Exception {
> 	    Main.main(args);
> 	  }
>
> 	  @Override
> 	  protected CamelContext createContainer() {
> 	    CamelContext context = super.createContainer();
>
> 	    PropertiesComponent properties = new PropertiesComponent();
> 	    properties.setLocations(myDynamicLocations);
> 	    context.addComponent("properties", properties);
>
> 	    return context;
> 	  }
>
> This just ends with me getting exception:
>
> Caused by: java.lang.IllegalArgumentException: PropertiesComponent with name
> properties must be defined in CamelContext to support property placeholders.
> 	at
> org.apache.camel.impl.DefaultCamelContext.resolvePropertyPlaceholders(DefaultCamelContext.java:969)
> 	at
> org.apache.camel.model.ProcessorDefinition.resolvePropertyPlaceholders(ProcessorDefinition.java:500)
> 	at
> org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:426)
> 	at
> org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:183)
> 	at
> org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:791)
> 	... 26 more
>
> I may be trying to use a sledge hammer to crack a nut, so there may be an
> easier way of doing this? The main point is that the exact list of
> properties files are not known until runtime, this may be unusual but is
> just the way it is!
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Configuring-property-locations-dynamically-at-runtime-tp4704549p4704549.html
> Sent from the Camel - Users mailing list archive at Nabble.com.


-- 
Willem
----------------------------------
FuseSource
Web: http://www.fusesource.com
Blog:    http://willemjiang.blogspot.com (English)
          http://jnn.javaeye.com (Chinese)
Twitter: willemjiang
Weibo: willemjiang