web项目中quartz使用request获取指定的文件路径

来源:互联网 发布:java join() 编辑:程序博客网 时间:2024/06/16 00:19

1.执行删除昨天的文件夹

public class CacheFileClaearJob implements Job,Runnable {


    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
             ServletContext servletContext =(ServletContext)context.getScheduler().getContext().get("servletContext");
             String realPath = servletContext.getRealPath("/").replace("\\", "/");
             String oldTempateFile  = realPath+"/template/"+StringUtil.dateFormat(DateUtils.getDate(-1), "yyyy-MM-dd");
             File file = new File(oldTempateFile);
             if(file.exists()){
                 FileUtil.deleteDir(file);
             }
        } catch (SchedulerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void run() {
        System.out.println("i am here run!");
    }

}


2.配置web启动监听器

public class QuartzListener  implements ServletContextListener {

    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        try {
             Scheduler sched = new StdSchedulerFactory().getScheduler();
             sched.getContext().put("servletContext", servletContextEvent.getServletContext());
        } catch (SchedulerException e) {
            e.printStackTrace();
        }   
           
        
    }

}

3.配置web. xml

<listener>
        <listener-class>cn.com.jx.core.listener.QuartzListener</listener-class>
    </listener><listener>
        <listener-class>cn.com.jx.core.listener.QuartzListener</listener-class>
    </listener>

0 0
原创粉丝点击