Apache Camel 条件路由 [英] Apache Camel conditional routing

查看:62
本文介绍了Apache Camel 条件路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有两个操作的服务.

I have a service which has two operations.

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 中,当我指定:

In my processor bean, when I specify:

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

我得到了注册用户对象.一切正常.问题是我希望骆驼有条件地路由我的请求,例如:

I get the register user object. Everything works fine. The problem is that I want camel to route my request conditionally, for e.g:

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

If the service operation is RegisterUser I want to route the message to my specific bean and if the service operation is UpdateUser I want to route the message to the other bean.

我曾尝试使用骆驼 xPath,但它似乎不起作用.

I have tried to use camel xPath, but it not seems to be working.

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

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

I was searching how to set up camel to route to the different targets but did not find anything. Maybe somebody knows where might be the problem?

推荐答案

需要操作的信息会在消息的头部.

The information of the operation required will be in the header of the message.

您要查找的标头名为 'operationName'

The header you are looking for is called '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 蓝图 - 但对于 spring,除了命名空间之外,它是相同的)

(Note the example is using apache aries blueprint - but it will be identical for spring, other than the namespace)

这篇关于Apache Camel 条件路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆