今天在工作中遇到了定时删除某个文件夹的所有内容的任务。
完成这个任务主要有两个点:
1.定时器的实现
2.什么时候执行定时器的任务
定时器的实现主要有4种方式:普通的thread、 TimerTask和Timer搭配、ScheduledExecutorService、quartz四中方式。这里参考了http://blog.csdn.net/haorengoodman/article/details/23281343这篇文章,由于以前用过了quartz这种方式,在这里我选择了ScheduledExecutorService这种方式。
定时任务的触发方式我知道的有三种方式:
1.servlet(通过在web.xml中配置load-on-startup加载)
2.listener
3.spring提供的ScheduledTimerTask和TimerFactoryBean
本文主要采用listener的实现方式,实现了ServletContextListener接口,在应用上下文初始化的时候,触发定时任务。
以下贴出listener类和web.xml中listener的配置:
package com.shinow.hmsw.hminterface.file.timertask;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.lang3.StringUtils;
import com.shinow.hmsw.common.util.DateUtils;
import com.shinow.hmsw.common.util.ProUtil;
/**
* 类说明:聊天文件处理监听器
*
* @author guocheng
* @version Version 1.0
* @since JDK 1.7
*/
public class FileHandlerListener implements ServletContextListener {
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
*/
@Override
public void contextInitialized(ServletContextEvent event) {
Runnable runnable = new Runnable() {
public void run() {
cleanFiles();
}
};
ScheduledExecutorService service = Executors
.newSingleThreadScheduledExecutor();
// 第二个参数为首次执行的延时时间,第三个参数为定时执行的间隔时间
service.scheduleAtFixedRate(runnable, genInterTime(), 60*24, TimeUnit.MINUTES);
}
/* (non-Javadoc)
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
*/
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
/**
* 删除聊天文件
*/
private void cleanFiles() {
String fileRootPath = ProUtil.getConfigValue("talk_files");
Calendar c = Calendar.getInstance();
String del_inter_time = ProUtil.getConfigValue("del_inter_time");
Integer delInterTime = 7;
if(StringUtils.isNotEmpty(del_inter_time)) {
delInterTime = Integer.parseInt("del_inter_time");
}
c.add(Calendar.DATE, -delInterTime);
String relativePath = File.separator + DateUtils.date2Str(new Date(), new SimpleDateFormat("yyyy-MM-dd"));
File fireDir = new File(fileRootPath + relativePath);
deleteDir(fireDir);
}
/**
* 递归删除某个文件夹
* @param dir
* @return
*/
private boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
/**
* 生成执行时间为每日的22点
* @return
*/
private int genInterTime() {
int target = 22 * 60;
int result = 0;
GregorianCalendar gc = new GregorianCalendar();
int h = gc.get(GregorianCalendar.HOUR_OF_DAY);
int m = gc.get(GregorianCalendar.MINUTE);
int nowMinutes = h * 60 + m;
if(nowMinutes <= target) {
result = target - nowMinutes;
} else {
result = 24 * 60 - nowMinutes + target;
}
return result;
}
}
com.shinow.hmsw.hminterface.file.timertask.FileHandlerListener