Apache骆驼2.23.1:to(file)创建一个空文件



[JDK 8/11,骆驼2.23.1,Spring Boot 2.1.3]

嗨,我目前正在学习骆驼,我无法克服一个奇怪的问题。

我想将URL下载到文件中。

URL有效,例如http://localhost/date显示" 11:59:00"。创建目标文件,但它是空的。为什么?

@Component
public class DownloadRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        String timer1 = "timer://foo?fixedRate=true&period=10000&delay=5000";
        String url = "http://localhost:8888/date";
        String file = "file:target/date";
        from(timer1).to(url).log("${id}->${body}").to(file);  // ID-Laptop-1551464093962-0-2->11:59:00, that's ok
    }
}

记录器输出正确的内容,例如

2019-03-01 19:15:00.421信息2984 --- [3-计时器://foo] Route1
:ID-LATTOP-1551464093962-0-2-> 11:59:00

但是生成的文件(例如目标/日期/ID-laptop-1551464093962-0-2)是空的。

有什么想法?

解决方案(感谢Tache):

    from(timer1).to(url).to(file);  // without logging

    from(timer1).to(url).convertBodyTo(String.class).log("${id}->${body}").to(file);    // with logging

http endpoint的输出(http://localhost:8888/date)可能是使用InputStream读取的流。这样的流只能读一次。可能的解决方案:

  • 删除您的日志语句
  • 在您的骆驼上下文或路由中启用流式缓存
  • 在记录并将其发送到文件之前,将消息正文转换为字符串(.convertBodyTo(String.class)

最新更新