Monday, September 8, 2008

ServiceMix and flexible endpoint configurations


On my daily work with ServiceMix I was always facing the problem to have the xbean.xml not being flexible enough to let others customize it dynamically without rebuilding. Imagine you build a ready to use product for polling files and processing them in some way. Now you ship this product to the customer not knowing which path he will use for polling. This will probably end up in the demand for a change or some dirty hacking inside the data folder.

So how can I provide some more flexibility?

Spring does provide a nice mechanism to read dynamic properties from a file via an object called PropertyPlaceholderConfigurer. With this it is possible to load values from a property file.

So how can I do this?

Lets have a look at the xbean.xml:

<?xml version="1.0"?>
<beans xmlns:f="http://servicemix.apache.org/file/1.0"
   xmlns:tut="http://www.servicemix.org/ns/jbi"
   xmlns:sm="http://servicemix.apache.org/config/1.0">
   <f:poller
      service="tut:filePoller"
      endpoint="pollEndpoint"
      file="${file.poll.folder}"
      targetService="#targetService"
      period="${file.poll.period}"
      recursive="${file.poll.recursive}" />

   <bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location">
         <value>file:${user.home}/esb/esb.properties</value>
      </property>
   </bean>

   <bean id="targetService" class="javax.xml.namespace.QName">
      <constructor-arg index="0" value="${file.poll.target.service.ns}"/>
      <constructor-arg index="1" value="${file.poll.target.service.name}"/>
   </bean>

</beans>

In the example code above we defined an object called PropertyPlaceholderConfigurer. This object loads the key-value pairs stored in the defined property file and makes the values available using the ${var-name} notation. (for example: ${file.poll.folder})

Conclusion

The really nice thing is that you can even use system variables like ${user.home} for getting the users home directory.

So you can have the property file where you want. The user home is just an example. Now if the customer wants to change the polled folder it's just a matter of changing a value inside the properties file and restarting the servicemix afterwards so the changes can take effect.

Just try it out and it will make your life a bit more easy.

3 comments:

caman said...

Excellent. This is exactly what I was looking for.

thanks

dom rocabo said...

this is nice..
But is there a way where u don't have to restart servicemix when u change some value in he property file during servicemix runtime?

Thanks!

lhein said...

There is no way I know of. Even when using spring deployment with smx4 you will have to redeploy.