我有一些 Apache Camel 路线,有很多选项,比如这个:
<from uri="sftp://user@host:22/path?
password=vvvvv;delay=3000&streamDownload=true&
delete=true&idempotent=true&idempotentRepository=#jpaStore&
inProgressRepository=#jpaStore"/>
这还不错,但我还有其他六条路线,它们具有相同的选项但路径不同。我想将所有选项放在一个常量中以避免重复:
<from uri="sftp://user@host:22/path?OPTIONS"/>
我也许可以使用Camel EL来完成此任务,但没有一个示例显示它,并且我尝试猜测语法的尝试不起作用。
我创建了一个像这样的 Spring bean:
<bean id="myoptions" class="java.lang.String">
<constructor-arg value="allmyoptions"/>
</bean>
并尝试像这样引用它:
<from uri="sftp://user@host:22/path?${myoptions}"/>
但是我收到一个错误:
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}=}]
这个问题,Simple Expression in apache-camel uri ,正在尝试类似的操作,但他们使用 Java DSL,并且我的路由是在 XML 中配置的。
有人知道避免跨路线重复所有这些选项的好方法吗?
最佳答案
从此页面,How do I use Spring Property Placeholder with Camel XML ,我读到“我们还不支持任意 Camel XML 中的 ${something} 表示法。”也就是说,他们在此页面上建议了各种解决方法,Properties .
对我有用的是配置 BridgePropertyPlaceholderConfigurer
如下:
<bean id="bridgePropertyPlaceholder" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
<property name="location" value="classpath:myproperties.properties"/>
</bean>
在属性文件中我有:
OPTIONS=password=vvvvv;delay=3000&streamDownload=true&delete=true&idepotent=true&idempotentRepository=#jpaStore&inProgressRepository=#jpaStore
这允许我使用 Spring 属性占位符表示法 ${}
和 Camel 占位符表示法 {{ }}
:
<from uri="sftp://user@host:22/path?{{OPTIONS}}"/>
一个问题是我需要删除属性文件中编码的 & 符号,将 &
替换为 &
。
另请参阅:
关于java - Apache Camel 避免在类似的 XML 路由中重复选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41901618/