Apache camel 基于内容的路由

Apache camel 基于内容的路由,apache-camel,Apache Camel,我对camel还不熟悉,我想编写一个camel路由,如果上面的bean方法返回“hi”,那么我必须调用另一个路由。但这在下面的代码中并没有发生。请告诉我解决办法 这是我的密码: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta

我对camel还不熟悉,我想编写一个camel路由,如果上面的bean方法返回“hi”,那么我必须调用另一个路由。但这在下面的代码中并没有发生。请告诉我解决办法

这是我的密码:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    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
       http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
       http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route id="firstRoute">
        <from uri="activemq:queue:test.queue" />
        <doTry>
            <to uri="bean:myBean?method=appendCamel(1,hell)" />
              <log message="TEST LOG" />
            <when>
                <xpath>${in.body} = 'hi'</xpath>
                <to uri="stream:out" />
            </when>
            <doCatch>
                <exception>java.lang.Exception</exception>
            </doCatch>
        </doTry>
        <to uri="stream:out" />
        <to uri="bean:myBean?method=appendCamel2(34)" />
        <to uri="stream:out" />
        <to uri="direct:nextRoute" />
    </route>

    <route>
        <from uri="direct:nextRoute" />
        <to uri="stream:out" />
    </route>
</camelContext>
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL" value="vm://localhost?broker.persistent=false" />
</bean>
<bean id="activemq" class="org.apache.activemq.camel.component.ActiveMQComponent">
    <property name="connectionFactory" ref="connectionFactory" />
</bean>

<bean id="myBean" class="Camel.CamelHelloWorldSid.MyBean" />

${in.body}='hi'
java.lang.Exception


${in.body}=='hi'
java.lang.Exception

您应该使用过滤器EIP:


${in.body}=='hi'
如果您需要多个谓词,那么您需要使用基于内容的路由器:(就像Java中的If..elseif…elseif…else)

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route id="firstRoute">
        <from uri="activemq:queue:test.queue" />
        <doTry>
            <to uri="bean:myBean?method=appendCamel(1,hell)" />
              <log message="TEST LOG" />
            <when>
                <simple>${in.body} == 'hi'</simple>
                <to uri="direct:nextRoute" />
            </when>
            <doCatch>
                <exception>java.lang.Exception</exception>
            </doCatch>
        </doTry>
        <to uri="stream:out" />
        <to uri="bean:myBean?method=appendCamel2(34)" />
        <to uri="stream:out" />
        <to uri="direct:nextRoute" />
    </route>

    <route>
        <from uri="direct:nextRoute" />
        <to uri="stream:out" />
    </route>
</camelContext>
<filter>
    <simple>${in.body} == 'hi'</simple>
    <to uri="direct:nextRoute" />
</filter>