只有当表达式在apache camel选择中起作用时?

时间:2014-09-09 09:27:30

标签: regex apache-camel

我有这条路线检查身体是否以" 01"或" 02"并基于此调用不同的bean。问题是只有第一个有效。例如,如果我发送一条以" 01"开头的消息 它工作正常,但如果我的消息以" 02"否则部分被执行,我得到一个空体的错误消息。

<route id="genericService">
        <from uri="servlet:///genericService"/>
        <choice>
            <when>
                <simple>${body} regex "^01.*$"</simple>
                <bean ref="cardFacade" method="getBalance" />
            </when>
            <when>
                <simple>${body} regex "^02.*$"</simple>
                <bean ref="depositFacade" method="getBalance" />
            </when>
            <otherwise>
                <transform>
                    <simple>error:  ${body}</simple>
                </transform>
            </otherwise>
        </choice>
        <marshal>
            <json />
        </marshal>
        <transform>
            <simple>${body}</simple>
        </transform>
    </route>

2 个答案:

答案 0 :(得分:3)

问题是servlet组件将body提供为只能读取一次的流。因此,您需要启用流缓存,或将邮件正文转换为非流类型,如String或byte []。

您可以在此处找到更多详情

还可以看到本页的第一个方框

答案 1 :(得分:0)

我在Java DSL中遇到了同样的问题(不是编译问题)。我要做的是为每一个选择添加.endchoice(),如下所示:

from(endPointTopic)
.errorHandler(deadLetterChannel)
.log("Message from Topic is ${body} & header string is ${header.Action}" )          
.choice()
    .when(header("Action").isEqualTo("POST"))
        .setHeader(Exchange.HTTP_METHOD, constant("POST"))
        .setHeader("Content-Type", constant("application/json"))
        .convertBodyTo(String.class)
        .to("log:like-to-see-all?level=INFO&showAll=true&multiline=true")
        .to(privateApi)
        .log("POST request for " + topicName)
        .endChoice()
    .when(header("Action").isEqualTo("PUT"))
        .setHeader(Exchange.HTTP_METHOD, constant("PUT"))
        .setHeader("Content-Type", constant("application/json"))
        .convertBodyTo(String.class)
        .to("log:like-to-see-all?level=INFO&showAll=true&multiline=true")
        .to(privateApi)
        .log("PUT request for " + topicName)
        .endChoice()
    .when(header("Action").isEqualTo("DELETE"))
        .setHeader(Exchange.HTTP_METHOD, constant("DELETE"))
        .setHeader("Content-Type", constant("application/json"))
        .convertBodyTo(String.class)
        .to("log:like-to-see-all?level=INFO&showAll=true&multiline=true")
        .to(privateApi)
        .log("DELET request for " + topicName)
        .endChoice()
    .otherwise()
        .setHeader(Exchange.HTTP_METHOD, constant("GET"))
        .setHeader("Content-Type", constant("application/json"))
        .convertBodyTo(String.class)
        .to("log:like-to-see-all?level=INFO&showAll=true&multiline=true")
        .to(privateApi)
        .log("Un-known HTTP action so posting to GET queue")
        .endChoice();