2

I have some Apache Camel routes with many options, like this one:

<from uri="sftp://user@host:22/path?
  password=vvvvv;delay=3000&amp;streamDownload=true&amp;
  delete=true&amp;idempotent=true&amp;idempotentRepository=#jpaStore&amp;
  inProgressRepository=#jpaStore"/>

This isn't so bad, but I have six other routes with the same options but different paths. I'd like to put all the options in a constant to avoid duplication:

<from uri="sftp://user@host:22/path?OPTIONS"/>

I might be able to use Camel EL to accomplish this, but none of the examples show it, and my attempts to guess the syntax aren't working.

I create a Spring bean like this:

<bean id="myoptions" class="java.lang.String">
  <constructor-arg value="allmyoptions"/>
</bean>

And try to refer to it like this:

<from uri="sftp://user@host:22/path?${myoptions}"/>

But I get an error:

There are 1 parameters that couldn't be set on the endpoint. Check the uri if the parameters are spelt correctly and that they are properties of the endpoint. Unknown parameters=[{${myoptions}=}]

This question, Simple Expression in apache-camel uri, is attempting something similar, but they use Java DSL and my routes are configured in XML.

Does anyone know a good way to avoid duplicating all this options across routes?

Community
  • 1
  • 1
DavidS
  • 5,022
  • 2
  • 28
  • 55

1 Answers1

3

From this page, How do I use Spring Property Placeholder with Camel XML, I read that "We do NOT yet support the ${something} notation inside arbitrary Camel XML." This said, they suggest various workarounds on this page, Properties.

What worked for me was to configure a BridgePropertyPlaceholderConfigurer as follows:

<bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
  <property name="location" value="classpath:myproperties.properties"/>
</bean>

In the properties file I have:

OPTIONS=password=vvvvv;delay=3000&streamDownload=true&delete=true&idepotent=true&idempotentRepository=#jpaStore&inProgressRepository=#jpaStore

This allows me to use both the Spring property placeholder notation ${} and the Camel placeholder notation with {{ }}:

<from uri="sftp://user@host:22/path?{{OPTIONS}}"/>

One gotcha is that I needed to get rid of my encoded ampersands in the properties file, replacing &amp; with just &.

See also:

Community
  • 1
  • 1
DavidS
  • 5,022
  • 2
  • 28
  • 55