1.增加quartz的jar包
quartz-2.2.1.jar
quartz-jobs-2.2.1.jar
2.web.xml中增加使用quartz的配置文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml,classpath:spring-task.xml</param-value>
</context-param>
3.spring-task.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 启动触发器的配置开始 -->
<bean name="startQuertz" lazy-init="false" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cleanUpThumbnailTrigger" />
<!-- <ref bean="licenseTrigger" /> -->
</list>
</property>
</bean>
<!-- 启动触发器的配置结束 -->
<bean id="cleanUpThumbnailTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="cleanUpThumbnailDetail" />
</property>
<property name="cronExpression">
<!--middle night 24. do it -->
<value>0 0 0 */7 * ?</value>
</property>
</bean>
<!-- <bean id="licenseTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="licenseJobDetail"/>
10 seconds
<property name="startDelay" value="0" />
repeat every 5 seconds
<property name="repeatInterval" value="5000" />
</bean> -->
<!-- 调度的配置结束 -->
<!-- job的配置开始 -->
<bean id="cleanUpThumbnailDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="cleanUpThumbnailJob" />
</property>
<property name="targetMethod">
<value>execute</value>
</property>
</bean>
<!-- job的配置结束 -->
<!-- 工作的bean -->
<bean id="cleanUpThumbnailJob" class="com.hytera.eems.job.cleanUpThumbnailJob" />
</beans>
4.class文件
package com.hytera.eems.job;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import com.hytera.eems.util.ice.log.IceLogService;
/**
* 清理缩略图作业
* @author y15212
*
*/
public class cleanUpThumbnailJob{
/**
* 执行缩略图文件删除任务
*/
private static final Logger LOGGER = LoggerFactory.getLogger(cleanUpThumbnailJob.class);
public void execute() {
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
String aString = servletContext.getRealPath("").replace("\\", "/")+"images";
delImageCache(aString);
LOGGER.debug("七天了,要开始执行删除图片缓存的任务了");
IceLogService.writeIceLog("Seven days, to start the task of deleting the image cache", 0);
}
public void delImageCache(String path){
File f = new File(path);
if (!f.exists()) {
System.out.println(path + " not exists");
return;
}
File fa[] = f.listFiles();
for (int i = 0; i < fa.length; i++) {
File fs = fa[i];
if (fs.isDirectory()) {
String filedirname = fs.getName();
SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd");
String nowdate = formart.format(new Date());
if(!nowdate.equals(filedirname)){//不是今天创建的文件,则全部删除
File[] children = fs.listFiles();
//递归删除目录中的子目录下
for (int j=0; j<children.length; j++) {
boolean success =children[j].delete();
if (!success) {
return ;
}
}
// 目录此时为空,可以删除
fs.delete();
}
}
}
}
}