簡體   English   中英

Spring入站通道適配器-如何自動刪除10天以上的文件夾和文件

[英]Spring inbound channel adapter - how to auto delete folders and files older than 10 days

Integration.xml-這將獲取目錄中的所有文件

<int-file:inbound-channel-adapter id="delFiles" channel="delFiles" 
        directory="C:/abc/abc" use-watch-service="true" prevent-duplicates="false" auto-startup="true"
            watch-events="CREATE,MODIFY">
        <int:poller fixed-delay="1000"/>
        <int-file:nio-locker/>
    </int-file:inbound-channel-adapter>

我需要刪除該文件夾和子文件夾中所有超過10天的文件。 可以幫個忙嗎?

傾聽者

@Transformer(inputChannel="delFiles")
    public JobLaunchRequest deleteJob(Message<File> message) throws IOException {
        Long timeStamp = message.getHeaders().getTimestamp();
        return JobHandler.deleteJob(message.getPayload(), jobRepository, fileProcessor, timeStamp);
    }

處理器

public static JobLaunchRequest deleteJob(File file, JobRepository jobRepository, Job fileProcessor, Long timeStamp) throws IOException {

//Is there a way in spring integration whether I can check this easily?
//How to check for folder and subfolders?
// This will check for files once it's dropped.
// How to run this job daily to check the file's age and delete?

    }

這不是<int-file:inbound-channel-adapter>責任。 這真的是關於根據您提供的過濾設置從目錄中輪詢文件。

如果您對舊文件不感興趣,則可以實現自定義FileListFilter來跳過確實很舊的文件。

如果您仍想刪除這些舊文件作為某些應用程序功能,則需要查看其他解決方案,例如@Scheduled方法,它會遍歷該目錄中的文件並刪除它們,例如,每天一次,例如在午夜。

您也可以只在和邏輯中刪除已處理的文件。 由於使用了prevent-duplicates="false" ,因此您將一次又一次地輪詢相同的文件。

要執行目錄清理,您不需要Spring Integration:

public void recursiveDelete(File file) {
    if (file != null && file.exists()) {
        File[] files = file.listFiles();
        if (files != null) {
            for (File fyle : files) {
                if (fyle.isDirectory()) {
                    recursiveDelete(fyle);
                }
                else {
                    if (fyle.lastModified() > 10 * 24 * 60 * 60 * 1000) {
                        fyle.delete();
                    }
                }
            }
        }
    }
}

(您可能會對此功能進行一些改進:尚未測試...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM