简体   繁体   English

如何使Apache Camel在“直接”路由的末尾删除文件?

[英]How to make Apache Camel delete a file at the end of a “direct” route?

I have a situation where I'm consuming from a direct endpoint. 我有一种情况,我从direct端点消费。 The body of the Exchange contains a File . Exchange的主体包含一个File Is there a way via the Spring DSL to delete this File at the end of the route? 有没有办法通过Spring DSL在路径的末尾删除这个File

For example, I have a route that looks like 例如,我的路线看起来像

<route>
    <from uri="direct:start"/>
    <to uri="file:/data/files"/>
</route>

The file gets copied to /data/files , but the original is left alone. 该文件将被复制到/data/files ,但原始/data/files将保持不变。

I know I can add something like 我知道我可以添加类似的东西

<to uri="bean:deleter?method=delete"/>

to the end of the route to perform the deletion, but I was wondering if there were a camel specific way to handle it. 到执行删除的路径的末尾,但我想知道是否有一种骆驼特定的方式来处理它。

Well, that really depends on how you pick up the file. 那么,这实际上取决于你如何拿起文件。 But seeing how in comments you seem to imply that the file is not coming from a file endpoint then I'm afraid that you're left with having to do the cleanup yourself. 但是看到评论中你似乎暗示文件不是来自file端点,那么我担心你不得不自己做清理工作。

If you are using ProducerTemplate to send the file to direct then you can delete it in the sender method: 如果您使用ProducerTemplate将文件发送到direct则可以在sender方法中将其删除:

try {
    ProducerTemplate template = ...;
    template.sendBody("direct:start", file);
} finally {
    file.delete();
}

Otherwise, you can either have a bean method that will do the deletion for you or - my suggestion - you could try to move the file reading behavior to Camel, so that your route with file instead of direct . 否则,您可以使用bean方法为您执行删除操作,或者 - 我的建议 - 您可以尝试将文件读取行为移至Camel,以便使用file而不是direct路由。

EDIT: 编辑:

If you are using Camel 2.15+, you can also try using pollEnrich() with dynamic URIs , given that you previously set the file or filename name somewhere in the exchange (eg body, exchange property, header...): 如果您使用的是Camel 2.15+,您还可以尝试使用pollEnrich()和动态URIpollEnrich()是您之前在交换中的某处设置了文件或文件名(例如body,exchange属性,header ...):

<route>
    <from uri="direct:start"/>
    <pollEnrich>
        <!--Example for  java.io.File body-->
        <simple>file:${body.parent}?fileName=${body.name}&delete=true</simple>

        <!--Example for file dir and file name as exchange properties-->
        <!--<simple>file:${exchangeProperty.myFileDir}?fileName=${exchangeProperty.myFileName}&delete=true</simple>-->
    </pollEnrich>
    <to uri="file:/data/files"/>
</route>

You need to set the delete parameter to equal true in your original from endpoint when the file is picked up. 在拾取文件时,您需要在原始端点中将delete参数设置为true。 For example: 例如:

from("file://inobox?delete=true")

For more parameters: http://camel.apache.org/file2.html 有关更多参数: http//camel.apache.org/file2.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM