-1

I'd like to use custom properties defined in a custom config file under $KARAF_HOME/etc/ and use those properties in my Apache Camel Java DSL route. Could someone help me with detailed steps with an example?

At Karaf end, configuration details are as shown below (file resides in $KARAF_HOME/etc directory).

Configuration File : $KARAF_HOME/etc/client.cfg

Content in the file:

dev.userID=userName@client.com

dev.password=secretPassword

Below snippet is where I'm trying to access the above props in a Camel Route using Java DSL.

from("timer:someTimer?period=10000")
  .setHeader("userID",simple("${env.userID}")
  .setHeader("password",simple("${env.password}")
     .log("${header[userID]}")
     .log("${header[password]}")
       .end();

The above code is throwing an error (see below).

Caused by: org.apache.camel.language.simple.types.SimpleIllegalSyntaxException: Unknown function: env.userID at location 0

${env.userID}
*
at org.apache.camel.language.simple.SimpleExpressionParser.parseExpression(SimpleExpressionParser.java:67) ~[132:org.apache.camel.camel-core:2.24.0]
at org.apache.camel.language.simple.SimpleLanguage.createExpression(SimpleLanguage.java:196) ~[132:org.apache.camel.camel-core:2.24.0]
at org.apache.camel.language.simple.SimpleLanguage.createExpression(SimpleLanguage.java:230) ~[132:org.apache.camel.camel-core:2.24.0]
at org.apache.camel.builder.SimpleBuilder.createExpression(SimpleBuilder.java:115) ~[132:org.apache.camel.camel-core:2.24.0]
... 11 more

Please help me how to access these properties in a Camel Route (using Java DSL), the configuration (.cfg) file is present in $KARAF_HOME/etc directory. Hope my problem description is understandable.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

2 Answers2

0

You use the Camel simple language (https://camel.apache.org/simple.html) in order to read the properties, and this can be done with either

.setHeader("userID",simple("{{dev.userID}}")

OR

.setHeader("userID",simple("${properties:dev.userID}")

${env:DEMO} is used in order to read an environmental variable called DEMO. Which in your case you don't have to since you have a properties file.

But first you have to load your property file (client.cfg) in your Camel Context. In o order to do this you use initialize the PropertiesComponent like:

PropertiesComponent pc = new PropertiesComponent();
pc.setLocation("file:${karaf.home}/etc/client.cfg"); 
context.addComponent("properties", pc);

Where context is your Camel Context.

Themis Pyrgiotis
  • 876
  • 5
  • 15
  • Thanks, Themis Pyrgiotis, can we achieve without using PropertiesComponent in the Camel Route(s)? Apache Karaf 's documentation says that any update(s) to the configuration files under ${karaf.home}/etc are dynamic will the approach you suggested be able to adopt the changes at runtime? – SaiBhaskar Jun 05 '19 at 06:55
  • I just implemented it and am able to see the log with data present in the cfg file however it is not listening to the runtime updates made on the same property? Any thoughts on that? Below is the line printed in the log after I changed the value. INFO apache-karaf-4.2.5\etc] Updating configuration from sample.cfg – SaiBhaskar Jun 05 '19 at 07:05
  • You're welcome @SaiBhaskar. So at the end PropertiesComponent was needed or not? I know that connection with your property cfg file is done automatically with OSGI Blueprint, but i don't now if this is provided with Java DSL like you are using. If my answer solved your problem please aceept my answer. Auto reload is not included in your initial question, maybe you should open a new question for that. – Themis Pyrgiotis Jun 05 '19 at 11:45
  • Thank You @Themis Pyrgiotis for your answer but my requirement was to have the properties to be updated at runtime that's the whole purpose of any properties file I believe, which Apache Karaf 's claiming is capable of but I don't think so. I achieved this using a file component with "idempotentKey" and "noop" features. Thanks to Abhishek Chatterjee ( https://stackoverflow.com/a/23675859/3887557 ) – SaiBhaskar Jun 05 '19 at 13:25
  • Sorry but your initial question was a out using properties in your camel. Nothing mentioned about what you said in the last commend. – Themis Pyrgiotis Jun 05 '19 at 14:43
  • Apologies from my end too about that... Thanks much for your quick turnaround Themis Pyrgiotis. – SaiBhaskar Jun 05 '19 at 15:15
0

After searching for some more help on StackOverflow finally I was able to achieve what I needed. Below code snippet monitors the files and triggers whenever there's an update/edit on the file(s).

 from("file:testingFolder?recursive=true&idempotent=true&idempotentKey=${file:name}-${file:size}&readLock=changed&noop=true")        
    .log(LoggingLevel.INFO, "Logger","${body}")
        .end();

Thanks to post . Please note that Apache Karaf 's claim on the adaptation of runtime updates is not working. Please refer section # 4.8.1 on their official documentation here, also there are no proper examples (anywhere) guiding on the usage of custom properties under ${KARAF_HOME}/etc/ in Camel's Route(s) using Java DSL. Highly appreciate if someone can explain with detailed example covering what needs to be done at all the three levels i.e.

  1. Apache Karaf 's etc folder
  2. Route.xml in Camel project's Blueprint
  3. RouteBuilder.java file - where we need to access those props defined in point #1.

Thank You.