Apache Camel条件路由

10

我有一个服务,它有两个操作。

RegisterUser
UpdateUser

我有一个骆驼路由:

<camel:route id="myRoute">
    <camel:from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&amp;synchronous=true" />            
    <camel:bean ref="processor" method="processMessage"/>
    <camel:to uri="xslt:file:resources/service/2.0.0/UserRegistration.xsl"/>
    <camel:to uri="cxf:bean:myTargetEndpoint"/>
</camel:route>

在我的处理器bean中,当我指定:

RegisterUser registerUser = exchange.getIn().getBody(RegisterUser.class);

我已获得注册用户对象,一切都运行良好。问题是我想让Camel有条件地路由我的请求,例如:

如果服务操作为RegisterUser,我想将消息路由到我的特定bean;如果服务操作为UpdateUser,我想将消息路由到另一个bean。

我尝试使用Camel xPath,但它似乎没有起作用。

<camel:route id="myRoute">
    <camel:from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&amp;synchronous=true" />  
    <camel:choice>
        <camel:when>
            <camel:xpath>
                //RegisterUser
            </camel:xpath>
            <camel:bean ref="processor" method="processMessage"/>
            <camel:to uri="xslt:file:resources/service/2.0.0/UserRegistration.xsl"/>
        </camel:when>
    </camel:choice>                        
    <camel:to uri="cxf:bean:myTargetEndpoint"/>
</camel:route>

我在搜索如何设置骆驼路由到不同的目标,但没有找到任何东西。也许有人知道问题出在哪里?

4个回答

19

所需操作的信息将在消息头中。

您要查找的标题为“operationName”。

因此,这里是一个示例:

<camelContext xmlns="http://camel.apache.org/schema/blueprint">
    <route id="example">
        <from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&amp;synchronous=true" />
        <log message="The expected operation is :: ${headers.operationName}" />
        <choice>
            <when>
                <simple>${headers.operationName} == 'RegisterUser'</simple>
                    <bean ref="processor" method="processMessage"/>
                <to uri="xslt:file:resources/service/2.0.0/UserRegistration.xsl"/>
            </when>
            <when>
                <simple>${headers.operationName} == 'UpdateUser'</simple>
                <!-- Do the update user logic here -->
                <bean ref="processor" method="updateUser" />
            </when>
        </choice>
    <to uri="cxf:bean:myTargetEndpoint"/>
    </route>
</camelContext> 

(请注意,该示例使用Apache Aries Blueprint - 但对于Spring来说,除了命名空间之外,它将是相同的)


6

尝试使用camel-simple表达式替代xpath来解决这个问题...

<when><simple>${body} is 'com.RegisterUser'</simple><to uri="..."/></when>

0

Spring XML路由 在我的情况下,我使用入站Jetty EP。 我检查请求中的参数。 调用URL http://localhost:8080/srv?alg=1

    <choice id="_choice1">
    <when id="_when1">
        <simple>${in.header.alg} == '1'</simple>
        <log id="_log10" message="LOG ALG 1"/>
    </when>
    ...
    <otherwise id="_otherwise1">
        <setFaultBody id="_setFaultBody1">
            <constant>Return message about ERROR</constant>
            </setFaultBody>
    </otherwise>
</choice>

-1
final CamelContext context = exchange.getContext();
if (isAlive) {
    context.startRoute("table-reader-route");
    log.info("starting dailycase route= " + response);
} else {
    context.stopRoute("table-reader-route");
    log.info("stoping dailycase route= " + response);
}

1
在这个网站上,仅提供代码的答案通常不被看好。您能否编辑您的答案,包括一些注释或解释您的代码?解释应该回答以下问题:它是做什么的?它是如何做到的?它去哪里?它是如何解决 OP 的问题的?请参阅:如何回答。谢谢! - Eduardo Baitello

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接