骆驼 - 流缓存不缓存/不能转换? [英] Camel - Stream cache not caching / can't convert?

查看:161
本文介绍了骆驼 - 流缓存不缓存/不能转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎一旦看完后要失去我的'中'身上。请注意,我用骆驼的流缓存,并输入是从HTTP组件JSON文件。我有以下code的处理器。

I seem to be losing my 'in' body after reading it once. Note that I am using Camel's stream caching, and that the input is a json file from the http component. I have a processor with the following code.

    log.debug("Body Type: " + exchange.getIn().getBody().getClass().getCanonicalName());
    log.debug("In msg1:"  + exchange.getIn().getBody(String.class));
    log.debug("In msg2:"  + exchange.getIn().getBody(String.class));

我希望看到这里是MSG1和MSG2是相同的输出,但是MSG 2返回一个空字符串(NOT NULL)。以下是在跟踪级别的日志。

What I'd expect to see here is that msg1 and msg2 are the same output, However msg2 returns a blank string (not null). Here are the logs at TRACE level.

1- DEBUG com.mycompany.MyProcessor : Body Type: org.apache.camel.converter.stream.InputStreamCache
2- TRACE org.apache.camel.impl.converter.DefaultTypeConverter : Converting org.apache.camel.converter.stream.InputStreamCache -> java.lang.String with value: org.apache.camel.converter.stream.InputStreamCache@780a5cef
3- TRACE org.apache.camel.impl.converter.DefaultTypeConverter : Using converter: StaticMethodTypeConverter: public static java.lang.String org.apache.camel.converter.IOConverter.toString(java.io.InputStream,org.apache.camel.Exchange) throws java.io.IOException to convert [class org.apache.camel.converter.stream.InputStreamCache=>class java.lang.String]
4- DEBUG com.mycompany.MyProcessor : In msg1:{myJson}
5- TRACE org.apache.camel.impl.converter.DefaultTypeConverter : Converting org.apache.camel.converter.stream.InputStreamCache -> java.lang.String with value: org.apache.camel.converter.stream.InputStreamCache@780a5cef
6- TRACE org.apache.camel.impl.converter.DefaultTypeConverter : Using converter: StaticMethodTypeConverter: public static java.lang.String org.apache.camel.converter.IOConverter.toString(java.io.InputStream,org.apache.camel.Exchange) throws java.io.IOException to convert [class org.apache.camel.converter.stream.InputStreamCache=>class java.lang.String]
7- DEBUG com.mycompany.MyProcessor : In msg2:

事情从日志注意:

Things to note from the logs:


  • 1号线的身型是否正确显示缓存输入流

  • 4-转换为字符串行的确实工作的生产MSG1,尽管3号线,转换code,似乎失败,并抛出IOException

  • 6号线也未能转换不过要注意的是,身体还是缓存流是非常重要的。

  • 7号线我的邮件丢失。

  • Line 1- The Body Type is correctly showing a cached input stream
  • Line 4- Converting to String does work to produce msg1, even though line 3, the conversion code, seems to fail with an IOException
  • Line 6- Also failing the conversion but it's important to note that the body is still a cached stream.
  • Line 7- My message is lost.

所以没有MSG2哪儿去了?

So where did msg2 go?

修改

有些事情除了提以下彼得的回答是:

Some things to mention in addition to Peter's answer below:

骆驼的MessageHelper静态类有两个有用的功能:

Camel's MessageHelper static class has two useful functions:


  • resetStreamCache

  • extractBodyAsString

  • resetStreamCache
  • extractBodyAsString

这两者将有助于这种情况

Both of which will help for this situation

推荐答案

使用流缓存可以读取数据流的不止一次在不同的的处理器,但仍的只能在同一个处理器一次的。

Using stream cache allows you to read streams more than once in different processors but still only once in the same processor.

我测试过:

ModelCamelContext context = new DefaultCamelContext();
context.setStreamCaching(true); //!!
// ...

from("direct:start")
    .to("http://ip.jsontest.com/?callback=showMyIP")
    .process(new MyProcessor())
    .process(new MyProcessor());

public class MyProcessor implements Processor {
    private static final Logger LOG = LoggerFactory.getLogger(HttpStreamCache.MyProcessor.class);
    @Override
    public void process(final Exchange exchange) throws Exception {
        LOG.info("***** Body Type: " + exchange.getIn().getBody().getClass().getCanonicalName());
        LOG.info("***** In msg1  : " + exchange.getIn().getBody(String.class));
        LOG.info("***** In msg2  : " + exchange.getIn().getBody(String.class));
    }
};

这将打印:

INFO  ***** Body Type: org.apache.camel.converter.stream.InputStreamCache
INFO  ***** In msg1  : showMyIP({"ip": "00.000.000.00"});

INFO  ***** In msg2  : 
INFO  ***** Body Type: org.apache.camel.converter.stream.InputStreamCache
INFO  ***** In msg1  : showMyIP({"ip": "00.000.000.00"});

INFO  ***** In msg2  :  

这篇关于骆驼 - 流缓存不缓存/不能转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆