版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lixin2151408/article/details/72356693
框架:
首先,需要在web.xml 中 配置监听:*为文件名(因带公司标识隐去)
<listener>
<listener-class>com.*****.datagis.autoclean.PushDataTaskListener</listener-class>
</listener>
autoclean 目录如下:
首先来看PushDataTaskListener类:
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class PushDataTaskListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
new TimerManager();
}
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
}
public class TimerManager {
//时间间隔
private static long PERIOD_DAY =20 * 1000;
public TimerManager() {
String ifbackup= GetProperties.getConstValueByKey("backuplbl");
String ifclean = GetProperties.getConstValueByKey("cleanlbl");
//判断如果开启了备份,则进行定时备份
if(ifbackup!=null&&!"".equals(ifbackup)&&!"0".equals(ifbackup)){
backup();
}
if(ifclean!=null&&!"".equals(ifclean)&&!"0".equals(ifclean)){
cleandata();
}
}
// 增加或减少天数
public Date addDay(Date date, int num) {
Calendar startDT = Calendar.getInstance();
startDT.setTime(date);
startDT.add(Calendar.DAY_OF_MONTH, num);
return startDT.getTime();
}
//备份
public void backup(){
int backuptimenum = Integer.parseInt(GetProperties.getConstValueByKey("backuptimenum"));
String backupcycle = GetProperties.getConstValueByKey("backupcycle");
if(backupcycle.equals("day")){
PERIOD_DAY = backuptimenum * 24 * 60 * 60 *1000;
}
else if(backupcycle.equals("week")){
PERIOD_DAY = backuptimenum * 7 * 24 * 60 * 60 *1000;
}
else if(backupcycle.equals("month")){
PERIOD_DAY = backuptimenum * 30 * 24 * 60 * 60 *1000;
}
Calendar calendar = Calendar.getInstance();
/*** 定制定时执行的时间 ***/
String gettime = GetProperties.getConstValueByKey("backuptime");
int year = Integer.parseInt(gettime.split("/")[0]);
int month = Integer.parseInt(gettime.split("/")[1])-1;
int day = Integer.parseInt(gettime.split("/")[2].substring(0,2));
int hour = Integer.parseInt(gettime.split(":")[0].substring(gettime.split(":")[0].length()-2));
int min =Integer.parseInt(gettime.split(":")[1]);
int second = Integer.parseInt(gettime.split(":")[2]);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);//16
calendar.set(Calendar.MINUTE, min);//10n
calendar.set(Calendar.SECOND, second);
Date date=calendar.getTime(); //第一次执行定时任务的时间
//如果第一次执行定时任务的时间 小于 当前的时间
//此时要在 第一次执行定时任务的时间 加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。循环执行的周期则以当前时间为准
if (date.before(new Date())) {
date = this.addDay(date, 1);
}
Timer timer = new Timer();
BackUpTimerTask task = new BackUpTimerTask();
//安排指定的任务在指定的时间开始进行重复的固定延迟执行。
timer.schedule(task,date,PERIOD_DAY);
}
public void cleandata(){
int cleantimenum = Integer.parseInt(GetProperties.getConstValueByKey("cleantimenum"));
String cleancycle = GetProperties.getConstValueByKey("cleancycle");
if(cleancycle.equals("day")){
PERIOD_DAY = cleantimenum * 24 * 60 * 60 *1000;
}
else if(cleancycle.equals("week")){
PERIOD_DAY = cleantimenum * 7 * 24 * 60 * 60 *1000;
}
else if(cleancycle.equals("month")){
PERIOD_DAY = cleantimenum * 30 * 24 * 60 * 60 *1000;
}
Calendar calendar = Calendar.getInstance();
/*** 定制定时执行的时间 ***/
String gettime = GetProperties.getConstValueByKey("cleantime");
int year = Integer.parseInt(gettime.split("/")[0]);
int month = Integer.parseInt(gettime.split("/")[1])-1;
int day = Integer.parseInt(gettime.split("/")[2].substring(0,2));
int hour = Integer.parseInt(gettime.split(":")[0].substring(gettime.split(":")[0].length()-2));
int min =Integer.parseInt(gettime.split(":")[1]);
int second = Integer.parseInt(gettime.split(":")[2]);
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);//16
calendar.set(Calendar.MINUTE, min);//10n
calendar.set(Calendar.SECOND, second);
Date date=calendar.getTime(); //第一次执行定时任务的时间
System.out.println(date);
//如果第一次执行定时任务的时间 小于 当前的时间
//此时要在 第一次执行定时任务的时间 加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。循环执行的周期则以当前时间为准
if (date.before(new Date())) {
date = this.addDay(date, 1);
}
Timer timer = new Timer();
CleanDataTimerTask task = new CleanDataTimerTask();
//安排指定的任务在指定的时间开始进行重复的固定延迟执行。
timer.schedule(task,date,PERIOD_DAY);
}
}
在此类中,
①可在calendar中设置自己的定时执行的时间;
②PERIOD_DAY为定时执行的时间间隔;
③可忽略我对GetProperties类读取properties配置文件的读取;
④定时执行的任务的主要实现 体现在代码:
Timer timer = new Timer();
BackUpTimerTask task = new BackUpTimerTask();
//安排指定的任务在指定的时间开始进行重复的固定延迟执行。
timer.schedule(task,date,PERIOD_DAY);
最后略微看一下实现类:(好像没啥好看的)
public class BackUpTimerTask extends TimerTask {
@Override
public void run() {
//DO SOMETHING
}
}