拆分集合并写入文件

拆分集合并写入文件

本文介绍了Camel:拆分集合并写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试拆分一个 ArrayList 并使用 Apache Camel 将每个元素写入它自己的文件,就像在这个简化的例子中一样:

I´m trying to split an ArrayList and writing each element to it´s own file using Apache Camel like in this simplified example:

from("timer://poll?period=10000").process(new Processor(){
    public void process(Exchange exchange){
        ArrayList<String> list = new ArrayList<String>();
        list.add("one");
        list.add("two");
        list.add("three");
        exchange.getIn().setBody(list, ArrayList.class);
    }
}).split(body()).log(body().toString()).to("file:some/dir");

日志打印每个项目,但只有三个"保存到文件中.我做错了什么?

The log prints each item but only "three" is saved to a file. What am I doing wrong?

一月

推荐答案

如果文件已经存在,文件生成器将默认覆盖".

The file producer will by default "override" if a file already exists.

在其文档页面查看 fileExist 选项http://camel.apache.org/file2

See the fileExist option at its documentation pagehttp://camel.apache.org/file2

由于此路由的输入也是一个文件,因此生产者将从输入中继承"文件名.

Since the input to this route is also a file, then the producer will "inherit" the file name from the input.

因此,在您的情况下,如果要将每个拆分的消息保存在新文件中,则需要使用 fileName 选项设置目标文件名

So in your case if you want to save each splitted message in a new file, then you would need to set a target file name, using the fileName option

"file:some/dir?fileName=splitted-${id}"

fileName 选项支持简单和文件语言

The fileName option supports the simple and file language

http://camel.apache.org/simple.html

http://camel.apache.org/file-language.html

这意味着可以动态计算文件名,例如上面的 ${id} 是唯一的消息 ID.

That means the file name can be dynamic computed, such as above, where ${id} is an unique message id.

这篇关于Camel:拆分集合并写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:45