【问题标题】:Apache Camel, resequencer moving files to .camelApache Camel,重测序器将文件移动到 .camel
【发布时间】: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


    【解决方案1】:

    要停止将文件移动到 .camel,您可以在路由构建器的 configure 方法中将 noop 参数添加到文件使用者。你可以阅读这个here if using Camel 1.xhere if using Camel 2.0 onwards

    from("file://target/input?noop=true")
    

    文件被移动到 .camel 目录的原因与重新排序器无关,而是与 Camel 中的实际文件生成器有关,如文档中所述:

    默认情况下,Camel 会将使用的文件移动到相对于文件使用位置的子文件夹 .camel。

    这些文件在此之后会被跳过,因为 Camel 会忽略任何以点开头的文件。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      相关资源
      最近更新 更多