在camel配置中读取属性文件值

时间:2016-12-16 06:52:47

标签: apache-camel

我在camel.config

中定义了以下路径
<choice>
    <when>
        <simple>${BROADBAND_DN_RBSID_SWITCH} == 'DN'</simple>
            <to uri="bean:bbGetServiceProfileProcessorByDN"/>
    </when>
</choice>

我正在从其他文件中读取$ {BROADBAND_DN_RBSID_SWITCH}值,即spring.properties #temp switch BROADBAND_DN_RBSID_SWITCH=DN

但是我遇到了以下错误

    Message History
---------------------------------------------------------------------------------------------------------------------------------------
RouteId              ProcessorId          Processor                                                                        Elapsed (ms)
[BROADBAND-ROUTES  ] [BROADBAND-ROUTES  ] [direct://BROADBAND                                                            ] [       380]
[BROADBAND-ROUTES  ] [multicast4        ] [multicast                                                                     ] [       380]
[BROADBAND-ROUTES  ] [choice17          ] [when[simple{${BROADBAND_DN_RBSID_SWITCH} == 'DN'}]choice[]                    ] [        14]

    Stacktrace
---------------------------------------------------------------------------------------------------------------------------------------
org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[ID-INPUSCPC08541-53313-1481817337243-0-29]
    at org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1696) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.builder.SimpleBuilder.createPredicate(SimpleBuilder.java:104) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.builder.SimpleBuilder.matches(SimpleBuilder.java:83) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.processor.FilterProcessor.matches(FilterProcessor.java:65) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:93) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:468) [camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) [camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109) [camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.processor.MulticastProcessor.doProcessParallel(MulticastProcessor.java:814) [camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.processor.MulticastProcessor.access$200(MulticastProcessor.java:84) [camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.processor.MulticastProcessor$1.call(MulticastProcessor.java:314) [camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.processor.MulticastProcessor$1.call(MulticastProcessor.java:299) [camel-core-2.17.0.jar:2.17.0]
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_45]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [?:1.7.0_45]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [?:1.7.0_45]
    at java.lang.Thread.run(Thread.java:744) [?:1.7.0_45]
Caused by: org.apache.camel.language.simple.types.SimpleIllegalSyntaxException: Unknown function: BROADBAND_DN_RBSID_SWITCH at location 0
${BROADBAND_DN_RBSID_SWITCH} == 'DN'
*

    at org.apache.camel.language.simple.SimplePredicateParser.parsePredicate(SimplePredicateParser.java:69) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.language.simple.SimpleLanguage.createPredicate(SimpleLanguage.java:115) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.builder.SimpleBuilder.createPredicate(SimpleBuilder.java:102) ~[camel-core-2.17.0.jar:2.17.0]
    ... 16 more
Caused by: org.apache.camel.language.simple.types.SimpleParserException: Unknown function: BROADBAND_DN_RBSID_SWITCH
    at org.apache.camel.language.simple.ast.SimpleFunctionExpression.createSimpleExpression(SimpleFunctionExpression.java:216) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.language.simple.ast.SimpleFunctionExpression.createExpression(SimpleFunctionExpression.java:40) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.language.simple.ast.SimpleFunctionStart.doCreateLiteralExpression(SimpleFunctionStart.java:58) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.language.simple.ast.SimpleFunctionStart.createExpression(SimpleFunctionStart.java:48) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.language.simple.ast.BinaryExpression.createExpression(BinaryExpression.java:78) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.language.simple.SimplePredicateParser.createPredicates(SimplePredicateParser.java:438) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.language.simple.SimplePredicateParser.doParsePredicate(SimplePredicateParser.java:119) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.language.simple.SimplePredicateParser.parsePredicate(SimplePredicateParser.java:66) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.language.simple.SimpleLanguage.createPredicate(SimpleLanguage.java:115) ~[camel-core-2.17.0.jar:2.17.0]
    at org.apache.camel.builder.SimpleBuilder.createPredicate(SimpleBuilder.java:102) ~[camel-core-2.17.0.jar:2.17.0]
    ... 16 more

2 个答案:

答案 0 :(得分:1)

您不能使用简单语言中的${xxx}来引用属性占位符。而是使用其properties函数:

 <simple>${properties:BROADBAND_DN_RBSID_SWITCH} == 'DN'</simple>

在文档中查看更多内容:http://camel.apache.org/simple

答案 1 :(得分:0)

其他可能的解决方案: 您可以将Camel上下文的属性持有者定义为:

<camelContext id="my-camel-context">
    <propertyPlaceholder
        location="file:${config.path}/my.properties" id="properties" />

请注意,在此级别,您可以使用常规Spring或System中的其他属性(即$ {config.path})

然后在路线中你可以使用{{notation as

 <simple>{{BROADBAND_DN_RBSID_SWITCH}} == 'DN'</simple>

与普通的Spring PropertyPlaceholder相比,Camel propertyPlaceholder有一些限制,但仅适用于文件,它可以正常工作。