Apache Camel Spring DSL,指环境变量HOSTNAME

时间:2016-10-10 14:26:31

标签: spring properties apache-camel filenotfoundexception

我正在使用Apache Camel 2.16.0和Spring DSL 我有一个Spring上下文XML,我在其中定义了一个Property PlaceHolder来读取各种文件中的属性,如下所示 -

    <bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="ignoreResourceNotFound" value="false"/>
    <property name="locations">
        <list>
            <value>classpath:/properties/versioning.properties</value>
            <value>classpath:/properties/#{inetAddress.hostName}.properties</value>
        </list>
    </property>
</bean>
<bean id="inetAddress" class="java.net.InetAddress" factory-method="getLocalHost"/>

属性值用于构造其他bean,例如 -

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
    <property name="url" value="${${LIVE_}DATASOURCE_URL}"/>
    <property name="username" value="${${LIVE_}DATASOURCE_USERNAME}"/>
    <property name="password" value="${${LIVE_}DATASOURCE_PASSWORD}"/>
</bean>

这很好用,我可以看到正在创建的bean。 我在同一个应用程序中有另一个Spring Context XML,它有一个camel上下文,我想使用在相同属性文件中定义的一些其他属性。我知道camel支持Spring Property Placeholder,请参阅下面的上下文摘录 -

<camelContext id="charge-process-context" xmlns="http://camel.apache.org/schema/spring">
    <propertyPlaceholder id="properties" 
        location="classpath:/properties/versioning.properties,
                             properties/${env:HOSTNAME}.properties"
        xmlns="http://camel.apache.org/schema/spring" />
    .....
    .....
</camelContext>

作为此上下文的一部分,我有一个使用https组件的路由,该组件使用属性文件中的值,如下所示 -

<to uri="https4:{{LIVE_AUTH_RESPONSE_HOST}}:{{LIVE_AUTH_RESPONSE_PORT}}/{{LIVE_AUTH_RESPONSE_CONTEXT_PATH}}"/>

此路由无法启动并在异常后抛出 -

Caused by: java.lang.IllegalArgumentException: Cannot find system environment with key: HOSTNAME
    at org.apache.camel.util.FilePathResolver.resolvePath(FilePathResolver.java:54)
    at org.apache.camel.component.properties.PropertiesComponent.parseLocations(PropertiesComponent.java:434)
    at org.apache.camel.component.properties.PropertiesComponent.parseUri(PropertiesComponent.java:163)
    at org.apache.camel.component.properties.PropertiesComponent.parseUri(PropertiesComponent.java:148)
    at org.apache.camel.impl.DefaultCamelContext.resolvePropertyPlaceholders(DefaultCamelContext.java:2261)
    at org.apache.camel.model.ProcessorDefinitionHelper.resolvePropertyPlaceholders(ProcessorDefinitionHelper.java:730)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessorImpl(ProcessorDefinition.java:427)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:413)
    at org.apache.camel.model.ProcessorDefinition.createOutputsProcessor(ProcessorDefinition.java:165)
    at org.apache.camel.model.ExpressionNode.createFilterProcessor(ExpressionNode.java:109)
    at org.apache.camel.model.WhenDefinition.createProcessor(WhenDefinition.java:74)
    at org.apache.camel.model.WhenDefinition.createProcessor(WhenDefinition.java:32)
    at org.apache.camel.model.ProcessorDefinition.createProcessor(ProcessorDefinition.java:483)
    at org.apache.camel.model.ChoiceDefinition.createProcessor(ChoiceDefinition.java:135)
    at org.apache.camel.model.ProcessorDefinition.makeProcessorImpl(ProcessorDefinition.java:534)
    at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:495)
    at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:219)
    at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1069)

请注意:我正在AWS实例上将我的应用程序部署为Tomcat 8上的war文件。 我在Windows 10上有一个Dev Environment,我发现它可以在Windows操作系统上运行。我还看到Apache Camel 2.16中的文件FilePathResolver.java使用System.getenv(key)来获取值,即System.getenv(&#34; HOSTNAME&#34;),它在AWS实例上返回null并且值正确在Windows 10.我也尝试使用env:hostname(unix的小写字母),但仍然没有运气......

1 个答案:

答案 0 :(得分:0)

我在http://camel.apache.org/using-propertyplaceholder.html找到了一个解决方案 在Bridging Spring和Camel Property Placeholders

它提到了以下 -

Spring Framework不允许第三方框架(如Apache Camel)无缝挂接到Spring属性占位符机制。但是,您可以通过声明类型为org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer的Spring bean来轻松地桥接Spring和Camel,这是一个Spring org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类型。