【问题标题】:Apache Camel File End Point - Reading the file one by one sorted in the created orderApache Camel File End Point - 按创建顺序一一读取文件
【发布时间】:2017-12-14 05:20:21
【问题描述】:

请求 Apache Camel 2.15.3 的帮助。 Endpoint 配置为从文件夹中获取文件并对其进行处理。它的工作方式是使用 Java 应用程序将文件中的数据上传(插入/更新)到多个表。 配置的端点是

<endpoint id="batchFilesFolder" uri="file:///C:/IBM/WebSphere/AppServer/profiles/AppSrv01/APP/Batch/" />

文件会在一天中出现在这个文件夹中,有时是多个文件放在一起。现在,如果有多个文件,不同的线程会将其用于处理并将其发布到数据库中会产生争用。

现在我想有一个完美的文件端点选项,那将

  1. 一次读取和处理一个文件。处理后有适当的延迟。
  2. 读取顺序必须按照创建文件的顺序。

你能帮忙吗?我已经尝试过延迟,maxMessagesPerPoll 等,但它不起作用..如下所示 -

<endpoint id="batchFilesFolder" uri="file:///C:/IBM/WebSphere/AppServer/profiles/AppSrv01/APP/Batch/?delay=300000&amp;maxMessagesPerPoll=1" />

【问题讨论】:

  • 看起来你的一些问题在第三行的“是”之后丢失了。如果您输入了任何标签。它们需要在单独的行上缩进 4 个空格,并在文本之间增加一行。
  • 你可能想看看实现使用 AsyncProcessor API 的处理器。
  • 感谢@dskow 指出这一点。我已经更正了这个问题。

标签: java apache-camel websphere


【解决方案1】:

我使用修改日期差为 1 秒的文件做了一个小测试:

for i in {1..10}; do echo $i &gt;&gt; file$i; sleep 1 ; done;

端点:

<endpoint id="batchFilesFolder" uri="file:///C:/IBM/WebSphere/AppServer/profiles/AppSrv01/APP/Batch/?maxMessagesPerPoll=1&amp;eagerMaxMessagesPerPoll=false&amp;sortBy=file:modified&amp;delay=1000" />

这里的诀窍是eagerMaxMessagesPerPollsort your files 然后处理它:

允许控制来自 ma​​xMessagesPerPoll 的限制是否急切。如果急切,则限制是在文件扫描期间。 Where as false 将扫描所有文件,然后执行排序。将此选项设置为 false 允许首先对所有文件进行排序,然后限制轮询。请注意,这需要更高的内存使用率,因为所有文件详细信息都在内存中以执行排序。

请注意,在执行排序时您的记忆会增长。

测试结果是:

INFO 18103 --- [resources/file2] route1                                   : file1
INFO 18103 --- [resources/file2] route1                                   : file2
INFO 18103 --- [resources/file2] route1                                   : file3
INFO 18103 --- [resources/file2] route1                                   : file4
INFO 18103 --- [resources/file2] route1                                   : file5
INFO 18103 --- [resources/file2] route1                                   : file6
INFO 18103 --- [resources/file2] route1                                   : file7
INFO 18103 --- [resources/file2] route1                                   : file8
INFO 18103 --- [resources/file2] route1                                   : file9
INFO 18103 --- [resources/file2] route1                                   : file10

本次测试使用的路线:

@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("file:src/test/resources/file2?noop=true&maxMessagesPerPoll=1&eagerMaxMessagesPerPoll=false&sortBy=file:modified&delay=1000")
                .log("${in.header.CamelFileName}")
                .convertBodyTo(String.class)
                .to("mock:result");
        }
    };
}

您也可以考虑查看readLock 属性来控制您的路由应该如何读取文件。

【讨论】:

    【解决方案2】:

    您可以使用quartz2-scheduler 与maxMessagesPerPollsortBy 选项一起每10 秒轮询一次:

    <endpoint id="batchFilesFolder" uri="file:///C:/IBM/WebSphere/AppServer/profiles/AppSrv01/APP/Batch/?scheduler=quartz2&amp;scheduler.cron=0/10+*+*+*+*+?&amp;sortBy=reverse:file:modified&amp;maxMessagesPerPoll=1" />
    

    文档有点不清楚 file:modified 是查找最新文件还是最旧文件,因此如果我的示例为您提供最新文件,只需删除 reverse:

    【讨论】:

      猜你喜欢
      • 2014-04-21
      • 2018-05-09
      • 1970-01-01
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      相关资源
      最近更新 更多