1、创建spring配置文件:applicationContext-quartz.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 0 0 0 1 * ? -->
<!-- 1.秒(0-59) -->
<!-- 2.分钟(0-59) -->
<!-- 3.小时(0-23) -->
<!-- 4.月份中的是期(1-31) -->
<!-- 5.月份(1-12或SUN-DEC) -->
<!-- 6.星期中的日期(1-7或SUN-SAT) -->
<!-- 7.年份(1970-2099) -->
<!-- 例子: -->
<!-- 0 0 10,14,16 * * ? 每天上午10点,下午2点和下午4点 -->
<!-- 0 0,15,30,45 * 1-10 * ? 每月前10天每隔15分钟 -->
<!-- 30 0 0 1 1 ? 2012 在2012年1月1日午夜过30秒时 -->
<!-- 0 0 8-5 ? * MON-FRI 每个工作日的工作时间 -->
<!-- 0 0/2 * * * ? 时间间隔为每2分钟执行一次 -->
<bean id="iTimerTongji" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<bean class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<bean class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.quartz.ExcelFileDeleteAutoJob</value>
</property>
</bean>
</property>
<property name="cronExpression">
<!-- 每30分钟 -->
<value>0 0/10 * * * ?</value>
</property>
<property name="startDelay">
<!-- 延迟60秒 -->
<value>60000</value>
</property>
</bean>
</list>
</property>
</bean>
</beans>
2、创建ExcelFileDeleteAutoJob类:
package com.quartz;
import java.io.File;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.util.ServiceHelper;
public class ExcelFileDeleteAutoJob implements Job {
private Logger logger = LoggerFactory.getLogger(getClass());
private PropertyManager propertyManager;
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
if (propertyManager == null) {
propertyManager = (PropertyManager) ServiceHelper.getService("propertyManager");
}
// TODO Auto-generated method stub
Date date = new Date();
// 获取当前时间:
long nowTimes = date.getTime();
// 获取删除的文件时间 小于当前时间1分钟
long delTimes = nowTimes - 10 * 60 * 1000;
// 获取文件
String filePath = "/excel/";
File fileDoc = new File(filePath);
File[] files = fileDoc.listFiles();
if (files.length > 0) {
for (File file : files) {
// 判断是否文件
if (file.isFile()) {
logger.debug("删除文件名称:" + file.getName());
// 获取文件最近修改的时间
long modifiedTimes = file.lastModified();
logger.info("文件名【" + modifiedTimes + "】删除成功!");
if (modifiedTimes < delTimes) {
file.delete();
}
logger.info("文件名【" + file.getName() + "】删除成功!");
}
}
}
}
}
3、创建ServiceHelper类:
package com.util;
import org.springframework.web.context.WebApplicationContext;
/**
* 获取Spring容器中的service bean
*
* @author Admin
*
*/
public final class ServiceHelper {
public static WebApplicationContext content;
public static Object getService(String serviceName) {
if (content != null) {
return content.getBean(serviceName);
} else {
return null;
}
}
}