Thursday 2 June 2016

Camel - Properties Loading using Java And Dynamic Loading of Routes XML



How to read properties from file in Camel Context and use it in anywhere ?

Solution:

I faced difficulty to get this working with camel context. That's why i wanna share with you.

Java version:

1.Load the Properties file and convert as Properties object in java.

2.
Properties props = new Properties();

try (InputStream propBody = IOUtils.toInputStream(body)){

props.loadFromXML(propBody);

} catch (Exception e) {

Logg the error here

}

3.
PropertiesComponent pcomp = new PropertiesComponent();
pc.setOverrideProperties(props);
exchange.getContext().addComponent("properties", pcomp);



Usage in Camel -Spring XML :

<log message="  TEST PROPERTY READ -  ${properties:serverURL}" loggingLevel="DEBUG"

logName="com.mycompnay.log" />





<enrich strategyRef="OrderPurifyStrategy">

<simple>${properties:serverURL}</simple>

</enrich>


Properties File:

serverURL= www.google.com


I used file component from camel to read the file for every change of content in file under "Test" Folder.

<route>
<from uri="file://Test?recursive=true&amp;noop=true&amp;delete=false&amp;runLoggingLevel=TRACE&amp;idempotent=true&amp;idempotentKey=${file:name}-${file:modified}" />
<bean ref="Someclass" method="updatePropertiesFile" />
</route>


Dynamic Loading of Routes Xml in Camel:

String body = body of the file.

try(InputStream is  =IOUtils.toInputStream(body)) {
                RoutesDefinition routes = exchange.getContext().loadRoutesDefinition(is);
                exchange.getContext().addRouteDefinitions(routes.getRoutes());
            } catch (Exception e) {
                logger.error("Error while routes ["+fileName+"]loading .....",e);
            }

you cannot use bean defintion in the same routes.xml, for this bean definition purpose we have to define new xml file with spring beans xml namespace.




2 comments:

  1. is there any example in which we refer beans in routes which are defined in different xml.

    ReplyDelete
  2. see link : http://stackoverflow.com/questions/43930922/beans-not-found-in-registry-when-camel-load-routes-from-xml-file/43983491#43983491

    ReplyDelete

Please comment here