8

如何实现Spring DSL 中给出的以下谓词示例:

Predicate isWidget = header("type").isEqualTo("widget");

from("jms:queue:order")
   .choice()
      .when(isWidget).to("bean:widgetOrder")
      .when(isWombat).to("bean:wombatOrder")
   .otherwise()
      .to("bean:miscOrder")
   .end();
4

3 回答 3

6

像这样:

<route>
  <from uri="jms:queue:order"/>
  <choice>
    <when>
       <simple>${header.type} == 'widget'</simple>
       <to uri="bean:widgetOrder"/>
    </when>
    <when>
      <simple>${header.type} == 'wombat'</simple>
      <to uri="bean:wombatOrder"/>
    </when>
    <otherwise>
      <to uri="bean:miscOrder"/>
    </otherwise>
  </choice>
</route>
于 2012-05-10T13:24:23.870 回答
6

所需的简单元素(见接受的答案)是

<simple>${header.type} == 'widget'</simple>

请注意字段表达式是如何被 ${} 包围的,然后是用于比较的 OGNL 语法,这不是字段表达式本身的一部分。

于 2012-09-01T04:02:15.293 回答
0

如果目标是在 Spring XML DSL 中使用谓词,那么这将更合适 -

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:camel="http://camel.apache.org/schema/spring"
xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

<camel:camelContext trace="true">
    <camel:route>
        <camel:from uri="jms:queue:order" />
        <camel:filter>
            <camel:method ref="myPredicate" />
            <to uri="bean:widgetOrder"/>
        </camel:filter>
    </camel:route>
</camel:camelContext>

<bean id="myPredicate" class="MyPredicate"/>
</beans> 
于 2020-11-28T01:37:48.657 回答