0

I want to write a method where i can return Future instead of void start

Also i want to apply lock while deleting the file.

public static void 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());
                }
            }
        }
    });
}

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

}

Instead of this I want my function to be like public static Future start(Vertx vertx) throws Exception { }

user3352615
  • 119
  • 2
  • 5
  • 13
  • Just a note about the deleteFileUploads method: it executes blocking code and you're calling it from a timer callback. You should wrap invocation in vertx#executeBlocking. – tsegismont Sep 26 '16 at 07:35

1 Answers1

0

So just change void to be what you want to return instead? What is the problem?

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?
}

As for the file locking try searching other questions. For example see here: Does File.delete in java perform file locking? and here: Locking Executing Files: Windows does, Linux doesn't. Why?

Community
  • 1
  • 1
sorifiend
  • 5,927
  • 1
  • 28
  • 45