如何在apache camel XML配置中的URI端点中使用动态值

时间:2017-01-19 03:13:08

标签: spring apache-camel

我正在使用apache camel在端点之间创建路由,通过在一个端口上运行在Tomcat上的一个URI(API网关),我映射到在不同域和端口上运行在Tomcat上的另一个URI。

<bean id="hostnameVerifier" class="org.apache.http.conn.ssl.AllowAllHostnameVerifier" />
...
<camel:sslContextParameters id="ssl">
    <camel:keyManagers keyPassword="password">
        <camel:keyStore ... />
    </camel:keyManagers>
    <camel:trustManagers>
        <camel:keyStore ... />
    </camel:trustManagers>
</camel:sslContextParameters>
    ....

<rest path="/MyService" consumes="application/json" produces="application/json">

    <post uri="/login">
        <description>Authenticate User</description>
        <route streamCache="true">
            <to
                uri="https4://domain-b:9000/Auth/user/login?bridgeEndpoint=true&amp;sslContextParametersRef=ssl&amp;x509HostnameVerifier=hostnameVerifier" />
        </route>
    </post>
    ...
</rest>

现在,就我在端点中硬编码域-b而言,事情进展顺利。当我必须从某个配置文件的输入中动态填充该值时会出现问题。

这就是我试图达到同样目的的方法 -

<bean id="properties" class="org.apache.camel.component.properties.PropertiesComponent">
    <property name="location" value="classpath:${LOCATION_PATH}propsfile.properties"/>
</bean>

属性键的名称是“domain”,现在在我的结束点定义我写的相同 -

<to
    uri="https4://${properties.domain}:9000/Auth/user/login?bridgeEndpoint=true&amp;sslContextParametersRef=ssl&amp;x509HostnameVerifier=hostnameVerifier" />

基本上在名为属性的bean中加载属性后,我尝试用$ {properties.domain}或#{properties.domain}替换domain-b,但似乎没有用。

如果有人可以建议,仅在基于XML的配置中,如何从属性文件中读取URL域,那将非常棒。

-AJ

2 个答案:

答案 0 :(得分:1)

你必须使用属性占位符来实现你想要的动态uri。

例如:

<camelContext ...>
   <propertyPlaceholder id="properties" location="YOUR_PROPERTY_FILE_LOCATION"/>
</camelContext>

然后尝试

<to uri="https4://{{properties.domain}}:9000/.......>

注意:使用spring bean “PropertiesComponent”配置属性文件时,必须在camel路由中使用camel Property 组件来实现动态值加载。

答案 1 :(得分:0)

自Camel 2.16起

我们可以使用

.from("file://path")
.toD("file://folder/${file:onlyname}")

我们可以使用文件