java 定时任务,定时删除缓存文件
2017-06-19 13:36
411 查看
//每天零点执行删除tomcat下所有的临时文件,用spring的定时任务注解,防止多余的临时文件占用空间
@Component public class TestTimer { @Scheduled(cron="0 0 0 * * ?") //定时任务,每天零点触发 public void myTimer(){ try { String path=File.createTempFile("datas", ".txt").getPath(); //获取临时文件路径 File file=new File(path).getParentFile(); //获取临时文件存放的文件夹 File[] files=file.listFiles(); //取文件夹下所有文件 for(File f:files){ //遍历删除所有文件 delete(f); System.out.println("当前时间是"+new Date().toString()); } } catch (IOException e) { e.printStackTrace(); } } /** * 将file文件夹下的所有文件清空 * @param file */ private boolean delete(File file){ if (file.isDirectory()) { String[] children = file.list(); for (int i=0; i<children.length; i++) { //递归删除目录中的子目录下 delete(new File(file, children[i])); } } // 目录此时为空,可以删除 return file.delete(); } }
在application.xml里的beans属性中添加
xmlns:task="http://www.springframework.org/schema/task"
然后在beans属性里的 xsi:schemaLocation 属性参数末尾添加
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd 最后在application.xml中增加定时任务配置,自动扫描对应包下的文件。***代表扫描路径
<task:annotation-driven/> <context:annotation-config/> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <context:component-scan base-package="com.***"/>
相关文章推荐
- spring+quartz实现定时任务(每天零点删除指定文件夹中缓存文件)
- php定时删除文件夹下文件(清理缓存文件)
- java监听器定时删除服务器上的文件
- linux实现crontab定时任务计划并删除之前的文件备份
- LINUX 下定时任务删除N天前的文件
- 用java打包成jar可执行文件并执行windows定时任务
- Java 定时删除文件
- Java定时任务调度工具详解(8)— Quartz 之 quartz.properties文件
- Linux定时任务执行,删除某个时间段的文件
- windows计划任务后台定时执行java的jar文件
- Window bat expdp 定时任务逻辑备份 定时删除N天前的旧文件
- Linux笔记整理:设置任务定时删除文件
- win 系统设置weblogic 进行定时自动重启并删除其日志和缓存文件,定时监控cpu是否达到100%并重启weblogic服务
- SQL Server 2005使用作业实现定时备份任务和删除指定天数前的备份文件
- java定时删除文件夹及文件
- Linux定时任务执行,删除某个时间段的文件
- Java定时框架Quartz实例详解与定时任务的Mongodb、Mysql持久化实现(二)Quartz配置文件
- 定时器的简单应用(二)——定时删除缓存文件
- crontab定时任务打包备份文件并删除过期文件
- 使用java实现定时器定时删除系统文件