【发布时间】:2014-03-11 02:58:51
【问题描述】:
我正在尝试重新排序在 Apache Camel 之上编写的应用程序摄取文件的顺序。文件需要按设定的顺序处理,如果应用程序没有按顺序获取文件,文件中数据的各种聚合和其他处理将失败。为了保护应用程序,我尝试使用重新排序器 EIP 来确保文件按顺序处理。但是我发现在下游组件通过交换之前,重新排序器似乎导致文件被移动到 .camel 目录中。
我写了一个简单的例子来说明这个问题:
import java.io.File;
import java.io.IOException;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
public class CamelFileResequencerTest extends CamelTestSupport {
@Test
public void test() throws IOException, InterruptedException {
File file1 = new File("target/input/1.txt");
FileUtils.write(file1, "Hello Camel");
FileUtils.write(new File("target/input/2.txt"), "Hello Camel");
FileUtils.write(new File("target/input/3.txt"), "Hello Camel");
Thread.sleep(100l);
assertTrue(file1.exists());
FileUtils.write(new File("target/input/4.txt"), "Hello Camel");
FileUtils.write(new File("target/input/5.txt"), "Hello Camel");
Thread.sleep(5000l);
assertTrue(new File("target/output/1.txt").exists());
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("file://target/input")
.resequence(header(Exchange.FILE_NAME_ONLY)).batch().size(3)
.to("file://target/output/");
}
};
}
}
测试在尝试写入输出文件时抛出异常,因为输入文件不再位于输入目录中,而是已移至 /target/input/.camel 目录。
现在我可以通过在重排序器之后添加处理 bean 以将文件路径更改为 .camel 目录中的路径来解决这个问题,但这对我来说似乎有点混乱。另一种选择是重新排序文件名而不是文件(即消息的主体是文件名作为字符串而不是通用文件)。
有没有人在文件上使用过重测序器并可以提供建议?
【问题讨论】:
标签: java apache-camel