1.spring定时器是大家在做项目的时候经常用到的,有了定时器就可以有一定规律的调用某些接口或者执行某些程序。
首先是spring文件配置:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <!-- 12小时执行一次 --> <bean id="recordJobTimer" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask"> <ref bean="recordDeleteJob" /> </property> <property name="delay"> <value>43200000</value> </property> <property name="period"> <value>30000</value> </property> </bean> <bean id="job2" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref bean="recordJobTimer" /> </list> </property> </bean> <!-- 录音删除 --> <bean id="recordDeleteJob" class="com.net263.ccs.job.RecordDeleteJob"> <property name="localPath" value="${ftp.local}"/> </bean> </beans>
然后是实现类的代码:
package com.net263.ccs.job; import java.io.File; import java.util.TimerTask; import org.apache.log4j.Logger; import org.springframework.util.StringUtils; import com.net263.ccs.common.Constants; import com.net263.ccs.util.DateUtil; /** * 类描述:录音文件删除 * * @author ming.li * @time 2011-5-17 上午11:16:33 */ public class RecordDeleteJob extends TimerTask { /** 本地目录 */ private String localPath; /** 日志logger */ protected final Logger logger = Logger.getLogger(getClass()); /** * 定时删除录音文件 */ @Override public void run() { // 获取今天 String today = DateUtil.getCurrentSimpleTime().substring(0, 8); // 根目录 File f = new File(localPath); if (!f.exists()) { logger.error("文件夹不存在"); return; } // 取次级目录 String[] dirs = f.list(); if (dirs != null && dirs.length > 0) for (String dir : dirs) { if (StringUtils.hasText(dir)) { File d = new File(localPath + dir); String[] dirDateTemp = dir.substring(11).split("-"); String dirDate = dirDateTemp[0] + dirDateTemp[1] + dirDateTemp[2]; // 判断超过一个月 if (Integer.parseInt(today) - Integer.parseInt(dirDate) > Constants.MIN_DELETE) if (d.delete()) { // 删除成功 } else { File[] fs = d.listFiles(); for (int i = 0; i < fs.length; i++) { if (!fs[i].isDirectory()) { fs[i].delete(); logger.info("删除文件:" + fs[i].getAbsolutePath()); } } d.delete(); logger.info("删除除文件夹"); } } } } /** * @param localPath * the localPath to set */ public void setLocalPath(String localPath) { this.localPath = localPath; } }
2.java也有自己的定时器,而且实现起来更加简单.
首先创建一个Test类
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.Timer; public class Test { private static int time; private static Properties p; // Properties p = System.getProperties(); static { InputStream is = TestJob.class.getClassLoader().getResourceAsStream("common.properties"); p = new Properties(); try { p.load(is); time = Integer.parseInt(p.getProperty("test.delay")); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { Timer timer = new Timer(); timer.schedule(new TestJob(), 1000, time);// 在1秒后执行此任务,每次间隔xxx秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务. } }
然后创建一个TestJob类,继承TimerTask
import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.TimerTask; public class TestJob extends TimerTask { private static Properties p; // Properties p = System.getProperties(); /** 日志logger */ static { InputStream is = TestJob.class.getClassLoader().getResourceAsStream("common.properties"); p = new Properties(); try { p.load(is); System.out.println("初始化成功"); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { } }
run方法里写要循环调用的方法,如此简单!