Q:

How to do housekeeping in jetty?

We let users upload some image files to our webserver. These files are of no use 10-15 mins after uploading. Is there any way to automatically delete these files e.g. all image files which have 'expire' in their name 15 mins after they were created?

A:

You just answered your own question - get all files with word "expire" in the name and modified 15 minutes prior current time and delete them.
Here is code. It's not efficient but plain:
File dir=new File(".");
String []expiredFiles=dir.list(new FilenameFilter() {
    @Override
    public boolean accept(File dir, String name) {
        return (name.contains("expired")&& new File(dir,name).lastModified()<System.currentTimeMillis()-15*60*1000);                
    }
});
for(String file:expiredFiles){
    new File(dir,file).delete();
}

You can run it every 15 minutes or so. Or, more simple approach, run it when each request is answered and closed but thread is not stopped yet. For example when just after your close your output stream on the response object. It doesn't take much resources especially when thread is up and still running.

A:

I basically setup quartz scheduler inside my webserver with a cron trigger
and the job looked more or like this
public static void deleteFilesOlderThanNdays(int daysBack, String dirWay, org.apache.commons.logging.Log log) {

    File directory = new File(dirWay);
    if(directory.exists()){

        File[] listFiles = directory.listFiles();            
        long purgeTime = System.currentTimeMillis() - (daysBack * 24 * 60 * 60 * 1000);
        for(File listFile : listFiles) {
            if(listFile.lastModified() < purgeTime) {
                if(!listFile.delete()) {
                    System.err.println("Unable to delete file: " + listFile);
                }
            }
        }
    } else {
        log.warn("Files were not deleted, directory " + dirWay + " does'nt exist!");
    }
}

ref:http://www.coderanch.com/t/384581/java/java/Delete-Files-Older-days

