【问题标题】:Camel Predicate Example in xml DSLxml DSL 中的骆驼谓词示例
【发布时间】:2012-05-19 01:32:23
【问题描述】:

如何在 Spring DSL 中实现以下谓词示例 given

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();

【问题讨论】:

    标签: java spring apache-camel


    【解决方案1】:

    像这样:

    <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>
    

    【讨论】:

    • Spring 应用上下文在 header 中没有 name 属性并且 根本不存在。
    • 你的骆驼和弹簧版本是什么?
    • 无论如何你可以在 中尝试这个而不是谓词:${header.type == 'wombat'}
    • 这个语法错误。看看 Dhiraj 的回答。
    【解决方案2】:

    所需的简单元素(见accepted answer)是

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

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

    【讨论】:

    • ${header.type == 'widget'} 不起作用。使用 Dhiraj 提到的 ${header.type} == 'widget'
    【解决方案3】:

    如果目标是在 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> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-29
      • 2018-02-17
      • 2014-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-26
      相关资源
      最近更新 更多