在vertx中编写调度程序以删除文件

时间:2016-09-26 01:25:41

标签: java vert.x

我想写一个方法,我可以返回Future而不是void start

此外,我想在删除文件时应用锁定。

public static void start (Vertx vertx)抛出异常{         vertx.setTimer(timeInterval,id - > {             文件文件=新文件(config.getStringProperty(" file.upload.directory",null).get());

        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File fileName : files) {
                long diff = new Date().getTime() - fileName.lastModified();
        if (diff > config.getLongProperty("file.upload.cleanup.timer.millisecond", 0).get()) {
                    deleteFileUploads(fileName.getAbsolutePath());
                }
            }
        }
    });
}

private static void deleteFileUploads(String fileName) {
    File file = new File(fileName);
    LOGGER.debug("Name of the file to be deleted"+fileName);
    file.delete();
}

}

而不是这个,我想要我的功能 public static 未来开始(Vertx vertx)抛出异常{ }

1 个答案:

答案 0 :(得分:0)

所以改变虚空是你想要返回的东西吗?有什么问题?

public static Future start(Vertx vertx) throws Exception { vertx.setTimer(timeInterval, id -> { File file = new File(config.getStringProperty("file.upload.directory", null).get());

        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File fileName : files) {
                long diff = new Date().getTime() - fileName.lastModified();
        if (diff > config.getLongProperty("file.upload.cleanup.timer.millisecond", 0).get()) {
                    deleteFileUploads(fileName.getAbsolutePath());
                }
            }
        }

    });
    //add new line to do your return here. If you want to get something out of the info above then use a public variable?
    return new Future()......;//define and return what you want here?
}

至于文件锁定尝试搜索其他问题。例如,请看这里: Does File.delete in java perform file locking?和此处:Locking Executing Files: Windows does, Linux doesn't. Why?