如何更改Apache Camel Route内部的属性值?

时间:2019-01-17 08:42:19

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

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

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网页更改路由的默认参数,并且只能在启动路由之后。

1 个答案:

答案 0 :(得分:0)

使用语法{{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>
相关问题