定时清理临时文件

package com.util.quartz;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class DeleteTempJob implements Job {

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
// TODO Auto-generated method stub

// 获得jobDetail传过来的参数
Map<String, Object> map = context.getJobDetail().getJobDataMap();
ServletContext servletContext = (ServletContext) map.get("servletContext");
String reportTemplatePath = servletContext.getRealPath("/resource") + System.getProperty("file.separator");
reportTemplatePath = reportTemplatePath.replace('\\', '/');

String dir = reportTemplatePath.substring(0, reportTemplatePath.indexOf("/resource")) + "/resource/expertTemp/";

File file = new File(dir);
try {
if (file.exists() && file.isDirectory()) {
// Cleans a directory without deleting it.
FileUtils.cleanDirectory(file);
}
} catch (IOException e) {
e.printStackTrace();
}
}

}


----------------------------------------------------------------------
package com.util.quartz;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;

@SuppressWarnings("deprecation")
public class QuartzTrigger extends org.quartz.ee.servlet.QuartzInitializerServlet {

private static final long serialVersionUID = 1L;

/**
* 初始化
*
* @param ServletConfig cfg
* @return
*/
public void init(ServletConfig cfg) throws javax.servlet.ServletException {
super.init(cfg);
run(cfg);
}

/**
* 创建一个调度器<code>Scheduler<code>的实例。 
* 实例化一个监控定制报表表的线程,并启动该线程。
*
* @param ServletConfig cfg
* @return
*/
public void run(ServletConfig cfg) {
ServletContext servletContext = cfg.getServletContext();
SchedulerFactory factory = (SchedulerFactory) cfg.getServletContext().getAttribute(QUARTZ_FACTORY_KEY);
Scheduler sched;
try {
sched = factory.getScheduler();
sched.start();
JobDetail jobDetail = new JobDetail("deleteTempFile", Scheduler.DEFAULT_GROUP, DeleteTempJob.class);
// 将ServletContext对象放到map中,然后从job中取出来,从而取得路径
Map<String, Object> map = new HashMap<String, Object>();
map.put("servletContext", servletContext);
JobDataMap dateMap = new JobDataMap(map);
jobDetail.setJobDataMap(dateMap);

// fire at the first day 02:01 in every month, start right now
Trigger trigger = TriggerUtils.makeDailyTrigger("deleteTempFile", 02, 01);

sched.scheduleJob(jobDetail, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}


-------------------------------------------------------------------
<servlet <description>Quartz定时器,定时清理指定文件夹的临时文件</description>
<servlet-name>DeleteTempJobServlet</servlet-name>
<display-name>DeleteTempJobServlet</display-name>
<servlet-class>com...util.quartz.QuartzTrigger</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>



==============================================================================================================
发现QuartzInitializerServlet 已过时,解决重新写了run方法,init方法去掉
public void run(ServletConfig cfg) {
ServletContext servletContext = cfg.getServletContext();
Scheduler scheduler;
try {
scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
JobDetail jobDetail = new JobDetail("deleteTempFile", Scheduler.DEFAULT_GROUP, DeleteTempJob.class);
// 将ServletContext对象放到map中,然后从job中取出来,从而取得路径
Map<String, Object> map = new HashMap<String, Object>();
map.put("servletContext", servletContext);
JobDataMap dateMap = new JobDataMap(map);
jobDetail.setJobDataMap(dateMap);

// fire at the first day 02:01 in every month, start right now
Trigger trigger = TriggerUtils.makeDailyTrigger("deleteTempFile", 02, 01);

scheduler.scheduleJob(jobDetail, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}

你可能感兴趣的:(文件)