Java Apache Camel 文件拆分器示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20661661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache Camel File Splitter by Example
提问by IAmYourFaja
I'm being given a file containing numeric IDs that are delimited by newlines ("\n
"):
我得到了一个包含由换行符 (" \n
")分隔的数字 ID 的文件:
123948
939904
129384
234049
etc. I want to use Camel to transform the file into an instance of the following POJO:
等我想使用骆驼将文件转换为以下 POJO 的实例:
public class IDFile {
private String fileName; // The name of the file
private List<Long> ids; // All the IDs in the file (123948, 939904, etc.)
// Constructor, getters/setters, etc.
}
I'm trying to see if I can use Camel's Splittercomponent to do this form me, but it feels like I'm trying to force a round peg into a square hole:
我想看看我是否可以使用 Camel 的Splitter组件来做这个,但感觉就像我试图将一个圆钉插入一个方孔:
<route>
<from uri="file://input/idfile"/>
<split streaming="true">
<tokenize token="\n" />
<to uri="bean://idfileProcessor?method=process"/>
</split>
</route>
The above looks like it would split my file into a List<Long>
, but I need the file name associated with the list as well. Any ideas?
上面看起来像是将我的文件拆分为一个List<Long>
,但我还需要与列表关联的文件名。有任何想法吗?
采纳答案by Ralf
There is a header "CamelFileName" on the exchange. Your processor/bean is passed a reference to the exchange and you can get the header from there and associate it with the token you have been invoked with.
交换中有一个标题“CamelFileName”。您的处理器/bean 被传递了一个对交换的引用,您可以从那里获取标头并将其与您被调用的令牌相关联。
Your route could look like this:
您的路线可能如下所示:
<camel:route id="splitter_test">
<camel:from uri="file:///home/steppra1/camel_test?delete=true&idempotent=true" />
<camel:to uri="bean:splitBean?method=init" />
<camel:split streaming="true">
<camel:tokenize token="\n" />
<camel:to uri="bean:splitBean?method=addToken" />
</camel:split>
<camel:to uri="bean:splitBean?method=done" />
<camel:log message="${in.body}" loggingLevel="INFO" logName="split_test" />
</camel:route>
The bean you are using to maintain the state on the exchange object:
您用来维护交换对象状态的 bean:
public class SplitBean {
public Object init(Exchange exchange) {
exchange.setProperty("splitTokens", new ArrayList<Integer>());
return exchange.getIn().getBody();
}
public Object addToken(Exchange exchange) {
((List<Integer>)exchange.getProperty("splitTokens")).add(Integer.parseInt((String)exchange.getIn().getBody()));
return null;
}
public Tuple done(Exchange exchange) {
return new Tuple<String, List<Integer>>((String)exchange.getIn().getHeader("CamelFileName"), (List<Integer>)exchange.getProperty("splitTokens"));
}
}
A file containing the rows
包含行的文件
1
2
3
5
fed to the route under the names splitter.text and splitter_2.txt yields the following log output:
馈送到名称为 splitter.text 和 splitter_2.txt 的路由会产生以下日志输出:
2013-12-18 18:20:02,081 INFO split_test - Tuple [first=splitter.txt, second=[1, 2, 3, 5]]
2013-12-18 18:20:46,610 INFO split_test - Tuple [first=splitter_2.txt, second=[1, 2, 3, 5]]
HTH
HTH