I have a situation where I'm consuming from a direct
endpoint. The body of the Exchange
contains a File
. Is there a way via the Spring DSL to delete this File
at the end of the route?
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.
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.
If you are using ProducerTemplate
to send the file to direct
then you can delete it in the sender method:
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
.
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 (e.g. body, exchange property, 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>