Java ApacheCamel条件路由

Java ApacheCamel条件路由,java,routing,cxf,apache-camel,apache-karaf,Java,Routing,Cxf,Apache Camel,Apache Karaf,我有一个服务,它有两个操作 RegisterUser UpdateUser 我有一个骆驼路线: <camel:route id="myRoute"> <camel:from uri="cxf:bean:myListenerEndpoint?dataFormat=POJO&amp;synchronous=true" /> <camel:bean ref="processor" method="processMessag

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

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>
我得到register用户对象。一切正常。 问题是我希望camel有条件地路由我的请求,例如:

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

我曾尝试使用camelxpath,但它似乎不起作用

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

//登记员
我正在寻找如何设置骆驼路线到不同的目标,但没有找到任何东西。也许有人知道问题出在哪里?

尝试使用表达式而不是xpath来解决此问题

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

所需操作的信息将出现在消息的标题中

您要查找的标题名为“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> 

${headers.operationName}=='RegisterUser'
${headers.operationName}=='UpdateUser'
(请注意,该示例使用的是ApacheAries blueprint,但它对于spring是相同的,而不是名称空间)

SpringXML路由 在我的情况下,我使用入境码头EP。 我按要求检查参数。 发票地址


${in.header.alg}=='1'
...
返回有关错误的消息

这对我很有用。正是我需要的。谢谢!:)在这个网站上,只有代码的答案通常是不受欢迎的。请编辑您的答案,包括一些注释或对代码的解释,好吗?解释应该回答这样的问题:它做什么?它是如何做到的?它去哪里了?它如何解决OP的问题?请参阅:。谢谢
<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> 
    <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>
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);
}