I am using an environment variable in Apache Camel which is optional and in certain cases the value won't be there, but still I want my config to work, assuming the not found environment variable as blank.
camelContext id="inboud" xmlns="http://camel.apache.org/schema/spring">
<!-- and then let Camel use those #Component scanned route builders -->
<propertyPlaceholder id="properties"
location="properties/app${env}.properties" ignoreMissingLocation="true" />
<!-- Messages placed here will be raw data from force.com-->
<template id="frceProducerTemplate" />
Here the ${env} I want to make as optional and if it's not present then the location must be taken as properties/app.properties.
Any idea anyone?
There is no support for default values if an ENV variable is not there. You can read more about using property placeholders here: http://camel.apache.org/using-propertyplaceholder.html
However you can likely add 2 locations, one with the ENV and another without the ENV. And then turn on ignoreMissingLocation="true" then if the ENV is not there Camel will ignore it.
Related
I am attempting use the PropertiesComponent and reading a property file that is in my classpath. I have built a standalone executable jar and I am using the camel Main class - no spring boot.
However, I would like to override one of those properties using environment variables, but it is not working. I am able to override it using the -D, but the documentation indicated that it is possible to override it using an environment variable.
Here is the sample code snippets
Main main = new Main();
main.addRouteBuilder(new HelloRoute());
main.bind("doWork", new DoWork());
PropertiesComponent pc = new PropertiesComponent();
pc.setLocation("classpath:app.properties");
main.bind("properties", pc);
main.run();
Here is the property file
account.route.id=account_route_get
account.route.get=account_route_get_description
startup_route=false
And here is my route where I am attempting to use it. I am attempting to override the startup_route and it does not work correctly.
rest("/account")
.get("/{name}")
.consumes("application/json")
.outType(Account.class)
.route()
.id("{{account.route.id}}")
.description("{{account.route.get}}")
.autoStartup("{{startup_route}}")
.to("log:{{account.route.get}}?level=INFO")
.to("bean:doWork?method=info(${header.name})")
.endRest()
I found this CAMEL-13502 but it is in a different Camel version, and I am wondering if this is also relevant for Camel 2.24.2
Thanks
I'm guessing you don't want to explicitly call out that you want the variable from the OS environment variables.
If you did want to do that, you could state
.autoStartup("${env:STARTUP_ROUTE}")
but if you are wanting a more dynamic solution, another option could be to create a
choice()
that first checks if the environment variable of that name exists, and if not, to default to the one in the properties file.
.when("${env:STARTUP_ROUTE} != null")
.autoStartup("${env:STARTUP_ROUTE}")
.otherwise()
.autoStartup("{{startup_route}}")
Finally, the Camel documentation of Property Component also states:
You can control these modes using the systemPropertiesMode and environmentVariableMode options on the properties component.
When I ran a sample in my project, I did have the ability to configure the systemPropertiesMode, but I didn't have an option for environmentVariableMode so unsure if that is a feature of a higher level Camel version than I'm using.
I just installed both PHPMD and PHPCS with my Project.
Now, I would like to customize them a bit, but can't seem to achieve it.
I get 2 warnings that I would like to remove for all my project:
phpcs: public method name MyTests::my_test_that_should_pass is not in camel caps format
phpmd: the method my_test_that_should_pass is not in camel case
With PHPMD, I tried to change : .composer/vendor/phpmd/phpmd/src/main/resources/rulesets/controversial.xml and set allow-underscore-test to true like mentioned here
With PHPCS, I don't really know how to do it.
Any idea???
https://phpmd.org/rules/controversial.html
PHPCS uses a file called ruleset.xml to allow you to create your own custom standard. The documentation for it is here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset
If you want a specific standard for your project, you can include a phpcs.xml file at the root of your project. It's exactly the same format as a ruleset.xml file and can even specify which files and folders need checking by default. Documentation for that is here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file
I have no idea what coding standard you are using with PHPCS right now, but I'll assume you are using PSR2.
If you run phpcs with the -s option, you'll see an error message with an error code, like this: Method name "MyTests::my_test_that_should_pass" is not in camel caps format (PSR1.Methods.CamelCapsMethodName.NotCamelCaps). The code is the bit you need here.
For your custom standard, you want PSR2, but you don't want the PSR1.Methods.CamelCapsMethodName sniff because you obviously don't want PHPCS checking for camel case. So create a ruleset with this content:
<?xml version="1.0"?>
<ruleset name="MyStandard">
<description>My custom coding standard.</description>
<rule ref="PSR2">
<exclude name="PSR1.Methods.CamelCapsMethodName"/>
</rule>
</ruleset>
Save that file and call it ruleset.xml or phpcs.xml and then run PHPCS using it: phpcs /path/to/code --standard=/path/to/ruleset.xml
Take a look at the annotated ruleset docs I linked at the top of the comment because there is a lot more you can do with those rulesets.
Is it possible to use SpEL when specifying a URI in a route? I've tried this a few ways but doesn't seem to be working.
Would like to do something like:
<from uri="jms:queue:#{ ${mq.dynamic.switch} ? '$mq.dynamic.queue' : '$mq.static.queue'}?connectionFactory=#connectionFactory" />
I'm essentially trying to evaluate a property to determine what queues to leverage when configuring a JMS route.
No that is not possible in <from>. Howerver you can use Camel's property placeholders:
http://camel.apache.org/using-propertyplaceholder.html
And in Camel routes such as <to> you can use "dynamic-to" which allows to use any of the scripting languages such as SpEL also:
http://camel.apache.org/how-to-use-a-dynamic-uri-in-to.html
http://camel.apache.org/languages.html
In apache-camel, is there a way to auto generate routeId's overriding the existing ones with route numbers(generated in RouteDefinitionHelper)?
There is to the best of my knowledge no autoGeneration policy on routeNaming you can use, but you could do something similar to this:
private String myURI;
from("jms:queue:" + myURI).routeId("JmsComponent:" + myURI)
.to("....");
By using something like blueprint or spring to inject your variable to the java class you can change your URI and it will adjust the route name accordingly. You could also use the full URI in your private variable then parse the endpointURI yourself and format it for the routeId.
You can specify them directly for routes as well as processors in your routes.
from("direct:start").routeId("MyMainRoute")
.to("direct:out").id("MyOutputProcessor");
These ids will be visible in your jConsole so you can see statistics on your routes and processors as well.
The XSD allows to specify customId="true" id="foo" in the route elements. But when reading the route using Spring you get a parse error in Spring. I can't able to understand this bug. Can you please clarify me?
You should not use customId but only id.
The customId is for internal usage when updating routes from XML that the id is auto assigned or assigned by the end user.