为什么我的Apache Camel条件路由总是执行第一个选项?

brtdzjyr  于 2023-01-05  发布在  Apache
关注(0)|答案(1)|浏览(92)

我有一个Camel路由,它应该运行验证器,但前提是设置了某个属性,即com.acme.ValidatorOn
路径的相关部分如下所示:

<choice>
    <when>
        <simple>{{com.acme.ValidatorOn}} == true</simple>
        <to uri="validator:MyWebService.xsd" />
    </when>
</choice>

当我注意到验证器总是被执行,即使属性被设置为false,我开始试验这个条件。

<choice>
    <when>
        <simple>{{com.blahblahthiswillnotwork.ValidatorOn}} == true</simple>
        <to uri="validator:MyWebService.xsd" />
    </when>
</choice>

即使使用了一个假的属性名,验证器仍然在执行,所以我做得更好,并且添加了一个<otherwise>

<choice>
    <when>
        <simple>5 == 6</simple>
        <log message="first choice" />
    </when>
    <otherwise>
        <log message="second choice"/>
    </otherwise>
</choice>

这将打印“第一选择”。无论我在<simple>中输入什么表达式,第一选择都会被执行。我尝试过一些明显不正确的表达式,如'foo' == 'bar',甚至只是胡言乱语。
我已经看过Camel关于基于内容路由的文档,在我的设置方式上似乎没有错误。我使用的是Camel 2.8.1和Java 8 update 45。
下面是完整的路线,以防万一在路线中可能有其他东西会把这件事搞砸(为了保护无辜的人,名字已经改了):

<route autoStartup="false" id="com.acme.doStuffRoute">
    <from uri="spring-ws:rootqname:{namepace}doStuff?endpointMapping=#com.acme.EndpointMapping"/>
    <setHeader headerName="Exchange.HTTP_QUERY">
        <simple>certainProperty=${ref:certainProperty}</simple>
    </setHeader>
    <setHeader headerName="CamelHttpMethod">
        <constant>POST</constant>
    </setHeader>
    <policy ref="com.acme.Administrators">
        <choice>
            <when>
                <simple>{{com.acme.ValidatorOn}} == true</simple>
                <to uri="validator:MyWebService.xsd"/>
            </when>
        </choice>
        <to pattern="InOut" uri="xslt:/xslt/do-stuff-request.xsl?transformerFactory=transformerFactory&amp;uriResolver=uriResolver"/>
        <to ref="com.acme.ToLogging"/>
        <transform>
            <method bean="msgTransform" method="encrypt"/>
        </transform>
        <to uri="ref:doMoreStuffEndpoint"/>
        <transform>
            <method bean="msgTransform" method="decrypt"/>
        </transform>
        <to ref="com.acme.FromLogging"/>
        <to uri="xslt:/xslt/do-stuff-request.xsl?transformerFactory=transformerFactory&amp;uriResolver=uriResolver"/>
        <process ref="com.acme.MetricsProcessor"/>
    </policy>
</route>
qacovj5a

qacovj5a1#

回答较晚,但将来可能会有所帮助。来自documentation
解析器被限制为仅支持单个运算符。
要启用它,左边的值必须包含在${ }中。语法为:

${leftValue} OP rightValue

您必须使用camel-simple表达式。例如,使用属性值设置header,然后在<when>中使用${headers.yourHeader}

**编辑:**我找到了更好的解决方案:使用 Camel -简单的方法来获得属性:${properties:com.acme.ValidatorOn}

相关问题