com.datastax.data.exploration.biz.quartz.DeleteTimerJob Maven / Gradle / Ivy
The newest version!
package com.datastax.data.exploration.biz.quartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import java.io.File;
import java.util.Date;
import static com.datastax.data.exploration.config.ConstantConfig.LOCAL_DATA_URL;
public class DeleteTimerJob implements Job {
//任务
@Override
public void execute(JobExecutionContext context) {
// 执行响应的任务.
File parentDir = new File(LOCAL_DATA_URL);
if (parentDir.isDirectory()) {
long timeNow = new Date().getTime();
deleteFolder(parentDir,timeNow);
}
}
//删除文件
public static void deleteFolder(File folder,long timeNow) {
File[] files = folder.listFiles();
if(files!=null) {
for(File f: files) {
if(f.isDirectory()) {
deleteFolder(f,timeNow);
} else {
Long timeFile =f.lastModified();
long difference = timeNow-timeFile;
int hour = (int)(difference/(1000*60*60));
if (hour > 24) {
f.delete();
}
}
}
}
}
// //测试
// @Override
// public void execute(JobExecutionContext context) throws JobExecutionException {
// SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
// System.out.println("AAAA: The time is now " + dateFormat.format(new Date()));
// }
}