Apache camel 在Camel上追加数据时锁定文件

Apache camel 在Camel上追加数据时锁定文件,apache-camel,Apache Camel,我写了2条路径来处理目录中的文件,这些文件可以有任何名称,但我需要2条路径,因为我需要一些复杂的处理 第一条路线: <route id="Init"> <from uri="file:{{file.path}}?move=.done&amp;moveFailed=.error&amp;readLock=changed&amp;readLockCheckInterval=1500&amp;charset=UTF-8"/>

我写了2条路径来处理目录中的文件,这些文件可以有任何名称,但我需要2条路径,因为我需要一些复杂的处理

第一条路线:

<route id="Init">
        <from uri="file:{{file.path}}?move=.done&amp;moveFailed=.error&amp;readLock=changed&amp;readLockCheckInterval=1500&amp;charset=UTF-8"/>
        <transacted/>

        <split streaming="true" stopOnException="true" shareUnitOfWork="true" parallelProcessing="false">
            <tokenize token="\r\n"/>

            <choice>
                <when>
                    <simple>${body.substring(0,4)} == 4000</simple>

                    [...]

                    <to uri="file:{{file.path}}/tmp?fileName=${date:now:yyyyMMddss}.txt&amp;fileExist=append&amp;charset=UTF-8"/>
                </when>

                <when>
                    <simple>${body.substring(0,4)} == 4002</simple>

                    [...]

                    <to uri="file:{{file.path}}/tmp?fileName=${date:now:yyyyMMddss}.txt&amp;fileExist=append&amp;charset=UTF-8"/>
                </when>
            </choice>

        </split>

    </route>
<route id="End">
        <from uri="file:{{file.path}}/tmp?delete=true&amp;moveFailed=.error&amp;readLock=changed&amp;readLockCheckInterval=1500&amp;charset=UTF-8"/>
        <transacted/>

        <split streaming="true" stopOnException="true" shareUnitOfWork="true" parallelProcessing="false">
            <tokenize token="\r\n4000"/>

            [...]

            <to uri="[...]"/>

        </split>

    </route>

${body.substring(0,4)}==4000
[...]
${body.substring(0,4)}==4002
[...]
第二个路由,它使用第一个路由生成的文件:

<route id="Init">
        <from uri="file:{{file.path}}?move=.done&amp;moveFailed=.error&amp;readLock=changed&amp;readLockCheckInterval=1500&amp;charset=UTF-8"/>
        <transacted/>

        <split streaming="true" stopOnException="true" shareUnitOfWork="true" parallelProcessing="false">
            <tokenize token="\r\n"/>

            <choice>
                <when>
                    <simple>${body.substring(0,4)} == 4000</simple>

                    [...]

                    <to uri="file:{{file.path}}/tmp?fileName=${date:now:yyyyMMddss}.txt&amp;fileExist=append&amp;charset=UTF-8"/>
                </when>

                <when>
                    <simple>${body.substring(0,4)} == 4002</simple>

                    [...]

                    <to uri="file:{{file.path}}/tmp?fileName=${date:now:yyyyMMddss}.txt&amp;fileExist=append&amp;charset=UTF-8"/>
                </when>
            </choice>

        </split>

    </route>
<route id="End">
        <from uri="file:{{file.path}}/tmp?delete=true&amp;moveFailed=.error&amp;readLock=changed&amp;readLockCheckInterval=1500&amp;charset=UTF-8"/>
        <transacted/>

        <split streaming="true" stopOnException="true" shareUnitOfWork="true" parallelProcessing="false">
            <tokenize token="\r\n4000"/>

            [...]

            <to uri="[...]"/>

        </split>

    </route>

[...]
我试图确保路由Init生成的文件在Init处理完第一个文件之前不会被路由End使用

我猜是使用临时文件扩展名,然后在第二个路由上使用exlude,但它不适用于fileExists

有什么想法吗

谢谢

使用完成文件

您需要一种机制来确保第二个路由只使用第一个路由已完全处理的文件

一个简单的方法是让第一个路由发送一个已完成的文件作为信号,告诉第二个路由该文件已处理完毕并准备好拾取

要使用done文件,可以在进程完成时在第一个路由中添加
doneFileName
参数,也可以使用相同的文件名模式在第二个路由中添加参数


有关更多详细信息,请阅读您不能将
readLock=changed
文件
组件一起使用,因为它仅适用于从Camel 2.8版起的FTP/SFTP

changed使用文件长度/修改时间戳来检测文件当前是否正在复制。将至少使用1秒。要确定这一点,此选项不能像其他选项那样快速地使用文件,但可以更可靠,因为JDK IO API无法始终确定文件当前是否正在被其他进程使用。readLockCheckInterval选项可用于设置检查频率此选项仅适用于Camel 2.8以后的FTP组件。注意:从Camel 2.10.1以后,如果FTP服务器支持完整文件名的列表操作(某些服务器可能不支持),则可以启用FTP选项fastExistsCheck以加速此读锁策略


请尝试其他机制,如
markerFile
fileLock
rename

谢谢。我测试了doneFileName=done,但它向文件夹中添加了一个“done”文件,如果有多个文件正在写入该文件夹,会发生什么情况?每个文件都将替换“完成”文件。。。对吗?哦,明白了!仅在上次拆分时或拆分完成后使用doneFileName。我没想到。成功了!令人惊叹的!谢谢@费迪。你可以为每个完成的文件创建动态完成文件,就像“编写‘完成’文件”一节中的“完成文件”一样支持简单的文件表达式。谢谢据我所知,这些标记都不起作用,因为文件在每个拆分周期后“完全”写入,因此第二个路由可能开始使用文件,而第一个路由尚未完成对第一个文件的处理;正确的?