Exchange.getIn()。getBody()在第二次调用

时间:2015-06-25 10:40:02

标签: java apache-camel

我有两个相同的电话:

String msg1 = exchange.getIn().getBody(String.class);
String msg2 = exchange.getIn().getBody(String.class);

在msg1中,我得到了正确的期望值,但msg2是一个空字符串。我没有设置 Out 消息,因此交换输入消息应保持不变。请解释为什么会这样。

骆驼路线:

<camelContext xmlns="http://camel.apache.org/schema/spring">
    <route id="route1">
        <from uri="timer://myTimer?period=2000" />
        <setBody>
            <simple>Hello World ${header.firedTime}</simple>
        </setBody>
        <process ref="messageProcessor" />
        <to uri="http://localhost:8090"/>
    </route>
    <route id="route2">
        <from uri="jetty://http://localhost:8090" />
        <process ref="messageProcessor" />
    </route>
</camelContext>

处理器仅包含上面的2个语句。 route1中的处理是正确的,但是在route2中我得到了描述的行为:第一次调用 - 有效字符串,第二次调用 - 空字符串。所以我想也许它与HttpMessage转换有关。

1 个答案:

答案 0 :(得分:10)

From http://camel.apache.org/jetty.html

Jetty is stream based, which means the input it receives is submitted to Camel as a stream. That means you will only be able to read the content of the stream once.

Just convert the input in a String before use it twice or more times

<route id="route2">
    <from uri="jetty://http://localhost:8090" />
    <convertBodyTo type="String" />
    <process ref="messageProcessor" />
</route>