Montag, 19. September 2016

HowTo: Change Camel route configuration during runtime

Introduction

In the following we are going to set up a dynamic configurable Camel route whose configuration can be changed during runtime without restarting the route. You may think that this sounds pretty easy but it turned out that it is not. So let me introduce you to the problem. 

Usually property placeholder are used to read properties from a configuration file. These properties can be used smoothly within route configuration but when the property is changed the update mechanism has no effect on the running route. The route has to be restarted to tighten the change. An automatic route restart can be achieved by configuring the appropriate update strategy within the property placeholder configuration. An example of this approach can be found in the Apache Camel documentation. I will quote the relevant snippet in the following.
 <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"  
       xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">  
   <!-- OSGI blueprint property placeholder -->  
   <cm:property-placeholder id="myblueprint.placeholder" persistent-id="camel.blueprint">  
     <!-- list some properties as needed -->  
     <cm:default-properties>  
       <cm:property name="result" value="mock:result"/>  
     </cm:default-properties>  
   </cm:property-placeholder>  
   <camelContext xmlns="http://camel.apache.org/schema/blueprint">  
     <!-- in the route we can use {{ }} placeholders which will lookup in blueprint  
        as Camel will auto detect the OSGi blueprint property placeholder and use it -->  
     <route>  
       <from uri="direct:start"/>  
       <to uri="mock:foo"/>  
       <to uri="{{result}}"/>  
     </route>  
   </camelContext>  
 </blueprint>  
The problem is that a route restart on a property change is often not acceptable as unintentional updates are triggered. So if you are okay with the restart, use the snippet from the Camel documentation if you want to get around restarting the route keep reading.