我想使用SimpleRegistry来存储属性(作为全局变量)。属性在带有jms端点的路由中使用setProperty
进行更改。骆驼文档上周发生了变化,有许多死链接,还有注册表页面。我没有发现任何描述simpleRegistry使用的示例。
我用骆驼-示例-servlet-tomcat作为基础。我不使用熔炉或补丁骆驼野蝇,因为是庞大的我们的简单模块。
<beans .... >
.
.
.
<bean id="simpleRegistry" class="org.apache.camel.support.SimpleRegistry" />
<camelContext xmlns="http://camel.apache.org/schema/spring">
<propertyPlaceholder id="properties" location="ref:simpleRegistry" />
<route id="storeConfig">
<from id="myTopic" uri="jms:topic:myTopic?selector=Configuration %3D 'xyz'" />
<log id="printHeader2" message="Received header: ${headers}" />
<log id="logToken" message="Received token: ${headers[myToken]}" />
<setProperty id="setMyToken" name="myProperty">
<simple>${headers[myToken]}</simple>
</setProperty>
</route>
<route id="externalIncomingDataRoute">
<from uri="servlet:hello" />
<transform>
<simple>The Token is: {{myProperty}}</simple>
</transform>
</route>
</camelContext>
</beans>
使用像上面这样的camel上下文,我得到了注册表中找不到的java.io.FileNotFoundException
Properties simpleRegistry。
当我使用<propertyPlaceholder id="properties" location="classpath:test.properties" />
并创建一个test.properties文件时,一切都正常,但我不能更改属性。忽略setProperty
标记中的操作。
我需要全局变量的原因是,我通过jms主题向camel上下文发送动态配置( myToken)。单个路由应该全局地存储此配置。如果通过rest组件调用另一条路由,则该路由需要令牌来做出选择。
发布于 2019-09-03 08:34:40
或者,您可以按照以下使用PropertiesComponent的方法获得相同的结果
<bean id="applicationProperties" class="java.util.Properties"/>
<bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
<property name="location" value="classpath:application.properties"/>
<property name="overrideProperties" ref="applicationProperties" />
</bean>
在camel上下文中定义属性位置所有者:
<propertyPlaceholder id="propertiesRef" location="ref:applicationProperties" />
设置一个属性,如下所示:
<bean ref="applicationProperties" method="setProperty(token, 'Test'})" />
并获取属性:${properties:token}
发布于 2019-09-02 08:04:28
好的,在你的问题中有多个主题。
如果您有Spring可用,Camel注册表将自动使用Spring注册表。Camel注册表只是一个薄包装或提供程序接口,它在可能的情况下使用作为另一个框架的可用注册表。
只有在没有其他可用的时才使用骆驼SimpleRegistry。这基本上是一个基于Map的内存注册表。
<setProperty>
设置应用程序属性。<setProperty>
设置Exchange属性,而不是应用程序属性。这样,您就可以在信息的交换中保存值。
您也许可以使用Spring单例bean,即Map。然后,您可以在需要它的地方自动使用它,它就像一个应用程序范围广泛的可用地图。
但是,再三考虑了为什么需要这种变量。这也可能是设计问题的一个症状。
https://stackoverflow.com/questions/57707998
复制相似问题