交易所上有一个标题“CamelFileName”。您的处理器/bean 传递了对交换的引用,您可以从那里获取标头并将其与您被调用的令牌相关联。
您的路线可能如下所示:
<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>
您用来维护交换对象状态的 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"));
}
}
包含行的文件
1
2
3
5
馈送到名为 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]]
高温高压