【问题标题】:How to change a property value inside a Apache Camel Route?如何更改 Apache Camel Route 中的属性值?
【发布时间】:2019-01-19 07:07:30
【问题描述】:

在属性文件中定义了一个变量测试:

test=OLD_VALUE

在下面的 Spring-DSL 定义中定义了骆驼路线。属性通过 PropertiesComponent 加载。

  <bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
    <property name="cache" value="false"/>
    <property name="location" value="classpath:res.properties"/>
  </bean>


  <camelContext id="ctx" xmlns="http://camel.apache.org/schema/spring">
    <route id="toParamRoute">    
      <from uri="servlet:myParam"/>
            HERE I WOULD LIKE TO SET THE 
            VARIABLE TEST WITH A NEW VALUE, 
            SUCH THAT THE FOLLOWING LOG MESSAGE 
            WILL PRINT THE NEW VALUE, 
            E.G: test=NEW_VALUE
      <log message="{{test}}"/>                   
    </route>    
 </camelContext>

我尝试了使用 groovy、语言脚本表达式、外部 spring bean 的不同方法,但没有成功。有没有办法设置和更改启动时加载的变量的值? 最好的方法是什么?

有人可以帮助我吗?我在stackoverflow上没有找到任何类似的问题!我面临的问题和我正在寻找的解决方案是构建 WEB UI 管理控制台以动态更改某些路由行为的基本构建块。为了简化流程,我可以说在 propertyPlaceholder 加载了一个属性文件之后,然后通过 UI 网页可以更改路由的默认参数,并且只有在路由可以启动之后。

【问题讨论】:

    标签: groovy apache-camel spring-camel spring-dsl


    【解决方案1】:

    使用语法{{property}} 评估的属性仅在上下文初始化期间解析一次。如果您需要反映运行时更改,请使用Simple language

    示例:

    <bean id="myProperties" class="java.util.Properties"/>
    
    <bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
        <property name="cache" value="false"/>
        <property name="location" value="classpath:res.properties"/>
        <property name="overrideProperties" ref="myProperties" />
    </bean>
    
    
    <camelContext id="ctx" xmlns="http://camel.apache.org/schema/spring">
        <route id="toParamRoute">
            <from uri="timer://foo"/>
            <log message="About to change property test from value ${properties:test} to value ${exchangeProperty.CamelTimerCounter}. Initial value was {{test}}"/>
            <bean ref="myProperties" method="setProperty(test, ${exchangeProperty.CamelTimerCounter})" />
            <log message="New value is ${properties:test}"/>
        </route>
    </camelContext>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 1970-01-01
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多