Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...