Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exchange.getIn().getBody() returns empty string in camel on second call

I have 2 identical calls:

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

In msg1 I get the correct expected value , but msg2 is an empty string. I'm not setting the Out message , so the exchange In message should be still intact. Please explain why this is happening.

Camel routes:

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

The processor contains only the 2 statements from above. The processing in route1 is correct , but in route2 I get the described behaviour : first call - valid string , second call - empty string. So I think maybe it has something to do with HttpMessage conversion.

like image 750
Natasha Avatar asked Jun 25 '15 10:06

Natasha


1 Answers

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>
like image 157
Sergey Avatar answered Sep 18 '22 10:09

Sergey