[英]How to make Apache Camel delete a file at the end of a “direct” route?
我有一種情況,我從direct
端點消費。 Exchange
的主體包含一個File
。 有沒有辦法通過Spring DSL在路徑的末尾刪除這個File
?
例如,我的路線看起來像
<route>
<from uri="direct:start"/>
<to uri="file:/data/files"/>
</route>
該文件將被復制到/data/files
,但原始/data/files
將保持不變。
我知道我可以添加類似的東西
<to uri="bean:deleter?method=delete"/>
到執行刪除的路徑的末尾,但我想知道是否有一種駱駝特定的方式來處理它。
那么,這實際上取決於你如何拿起文件。 但是看到評論中你似乎暗示文件不是來自file
端點,那么我擔心你不得不自己做清理工作。
如果您使用ProducerTemplate
將文件發送到direct
則可以在sender方法中將其刪除:
try {
ProducerTemplate template = ...;
template.sendBody("direct:start", file);
} finally {
file.delete();
}
否則,您可以使用bean方法為您執行刪除操作,或者 - 我的建議 - 您可以嘗試將文件讀取行為移至Camel,以便使用file
而不是direct
路由。
編輯:
如果您使用的是Camel 2.15+,您還可以嘗試使用pollEnrich()
和動態URI , pollEnrich()
是您之前在交換中的某處設置了文件或文件名(例如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>
在拾取文件時,您需要在原始端點中將delete參數設置為true。 例如:
from("file://inobox?delete=true")
有關更多參數: http : //camel.apache.org/file2.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.