首页 文章

Apache Camel避免在类似的XML路由中复制选项

提问于
浏览
1

我有一些Apache Camel路由有很多选项,比如这个:

<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"/>

这不是那么糟糕,但我有六个其他路线具有相同的选项但路径不同 . 我想将所有选项放在一个常量中以避免重复:

<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}"/>

但是我收到一个错误:

endpoints 上无法设置1个参数 . 如果参数拼写正确并且它们是 endpoints 的属性,请检查uri . 未知参数= [{$ =}]

这个问题Simple Expression in apache-camel uri正在尝试类似的东西,但他们使用Java DSL,我的路由是用XML配置的 .

有没有人知道避免在路线上复制所有这些选项的好方法?

1 回答

  • 2

    从这个页面How do I use Spring Property Placeholder with Camel XML,我读到"We do NOT yet support the $ notation inside arbitrary Camel XML."这就是说,他们建议在这个页面上有各种解决方法,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}}"/>
    

    一个问题是我需要摆脱属性文件中的编码&符号,用 & 替换 &amp; .

    也可以看看:

相关问题