Apache Camel File End Point - 按创建顺序逐一读取文件

Apache Camel File End Point - Reading the file one by one sorted in the created order

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

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

一整天,文件都会来到这个文件夹,有时多个文件在一起。现在,如果有多个文件,不同的线程会将其用于处理并将其发布 db 会造成争用。

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

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

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

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

您可以使用 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:.

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

for i in {1..10}; do echo $i >> 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然后处理它:

Allows for controlling whether the limit from maxMessagesPerPoll is eager or not. If eager then the limit is during the scanning of files. Where as false would scan all files, and then perform sorting. Setting this option to false allows for sorting all files first, and then limit the poll. Mind that this requires a higher memory usage as all file details are in memory to perform the sorting.

请注意,执行排序时您的内存会增加。

测试结果为:

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 属性 来控制路由读取文件的方式。