Exchange.getIn().getBody() 在第二次调用时返回骆驼中的空字符串

编程入门 行业动态 更新时间:2024-10-24 20:15:44
本文介绍了Exchange.getIn().getBody() 在第二次调用时返回骆驼中的空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有 2 个相同的电话:

I have 2 identical calls:

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

在 msg1 中我得到了正确的期望值,但 msg2 是一个空字符串.我没有设置 Out 消息,所以交换 In 消息应该仍然完整.请解释为什么会发生这种情况.

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.

骆驼路线:

<camelContext xmlns="http://camel.apache/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的转换有关系.

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.

推荐答案

来自 http://camel.apache/jetty.html

Jetty 是基于流的,这意味着它接收到的输入被提交以骆驼为流.这意味着您将只能阅读流的内容一次.

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>

这篇关于Exchange.getIn().getBody() 在第二次调用时返回骆驼中的空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-19 22:55:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/971466.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:骆驼   空字符串   Exchange   getIn   getBody

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!