[英]Not able delete remote file located on a server using apache camel FTP
I am working on a piece of code where I need to work on the files which are located on a server using Apache camel. 我正在编写一段代码,需要使用Apache骆驼处理服务器上的文件。 I need to achieve the following steps:
我需要实现以下步骤:
1 --> The control should iterate through all the files which are located in a specific folder. 1->控件应循环访问特定文件夹中的所有文件。
2 --> Then it should check which file is older than 15 days. 2->然后它应该检查哪个文件早于15天。
3 --> Then delete the files which are older than 15 days. 3->然后删除超过15天的文件。
Now, I am able to implement the first and second step. 现在,我可以执行第一步和第二步了。 But I am not able to delete the file.
但是我无法删除该文件。 I have created a camel route like this:
我创建了这样的骆驼路线:
<camel:route id="lastModifiedFMFileCheckRoute">
<camel:from uri="sftp://someUser@someServer/usableFiles?password=secret"/>
<camel:setProperty propertyName="availableFile">
<camel:simple>${body}</camel:simple>
</camel:setProperty>
<camel:process ref="fileModificationDateProcessor" />
</camel:route>
and the process method of my processor looks like this, where I am checking whether file is older than 15 days. 处理器的处理方法如下所示,其中我正在检查文件是否早于15天。
@Override
public void process(Exchange exchange) throws Exception {
boolean isFileDeleted = false;
@SuppressWarnings("rawtypes")
GenericFile currentFile = (GenericFile)exchange.getProperty("availableFile", RemoteFile.class);
Date currentDate = new Date();
int numberOfDays = (int)( (currentDate.getTime() - availableFile.getLastModified()) / MILLISECONDS_TO_DAY_CONVERTER_VALUE);
if(numberOfDays > 15){
String absoluteFilePath = availableFile.getAbsoluteFilePath();
//TODO The file (currentFile) needs to be deleted. As it is older than 15 days.
}
exchange.getOut().setBody(fileDeleted);
}
How can I achieve the deletion of the required file here. 如何在此处删除所需文件。
On your (s)ftp endpoint you can set delete=true
and filter=#ageFilter
where ageFilter is a reference to a custom file filter that only accepts files older than 15 days. 在(s)ftp端点上,可以设置
delete=true
和filter=#ageFilter
,其中ageFilter是对自定义文件过滤器的引用,该过滤器仅接受15天以上的文件。
public class AgeFilter<T> implements GenericFileFilter<T> {
@Override
public boolean accept(final GenericFile<T> file) {
long now = System.currentTimeMillis();
long lastModified = file.getLastModified();
return now-lastModified > CUT_OFF_AGE;
}
}
At the end of the route the (s)ftp enpoint will then delete all files selected by this filter. 在该路由的末尾,(s)ftp指定点将删除此过滤器选择的所有文件。
As an alternative to the file filter you could throw a RuntimeException
in your processor if the file is NOT older than 15 days. 如果文件不超过15天,则可以使用替代文件筛选器的方法在处理器中引发
RuntimeException
。 This will make your route fail and and will leave the file on the remote server. 这将使您的路由失败,并将文件保留在远程服务器上。 However, using exceptions for control flow is really not good practice.
但是,对控制流使用异常确实不是一个好习惯。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.