Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java xml-DSL中的Camel谓词示例_Java_Spring_Apache Camel - Fatal编程技术网

Java xml-DSL中的Camel谓词示例

Java xml-DSL中的Camel谓词示例,java,spring,apache-camel,Java,Spring,Apache Camel,如何在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

如何在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();
像这样:

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

${header.type}=='widget'
${header.type}=='wombat'

所需的简单元素(请参阅)为

${header.type}=='widget'

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

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

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


Spring应用程序上下文在标头中没有name属性,根本不存在。您的camel和Spring版本是什么?无论如何,您可以在中尝试此操作,而不是在谓词中:${header.type=='wombat'}此操作的语法错误。看看Dhiraj的答案。${header.type=='widget'}不起作用。使用Dhiraj提到的${header.type}=='widget'。
<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>