java定时执行功能

框架:

首先,需要在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
         
    }
 
}


再来看TimerManager类:

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


OK啦~可以拿去试试┻┳|・ω・)




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值