Spring定时任务--清理日志

Spring配置定时任务–清理日志

1.首先在Web.xml中配置:

<context-param>
    <param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,classpath:applicationContext-TimeTask.xml</param-value>
</context-param>

2.Spring的配置文件中配置定时器任务类

<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
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
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task-4.0.xsd">


<task:annotation-driven />
<!-- 删除tomcat日志-->
<bean id="DelLogs" class="com.feng.task.logs.DelLogs" />
<task:scheduled-tasks> 

    <!-- 每天早上6点到第二天凌晨2点,每5分钟检查一次        
    <task:scheduled ref="checkConnect" method="checkConnectTask" cron="0 0/5 6-23,0-1 * * ?" /> 
-->

    <!-- 删除tomcat日志,每天早上2点18清理tomcat日志 -->      
        <task:scheduled ref="DelLogs" method="delAdvert" cron="0 18 02 * * ?" />
</task:scheduled-tasks> 
</beans>

3.定时任务类

package com.feng.task.logs;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.log4j.Logger;


/**
 * 名称 :DelLogs 描述 :定时删除服务器日志 
 */
public class DelLogs {

private static final Logger logger = Logger.getLogger(DelLogs.class);
public void delAdvert() {
    String delpath = "E:\\apache-tomcat-6.0.41\\logs\\SearchCar";
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    //获取所有文件名
        File file1 = new File(delpath);
        File[] files = file1.listFiles();
        //递归删除文件
            for (int i = 0; i < files.length; i++) {
                File file2 = files[i];
                String logsName = file2.getName();//文件名
                //获取当天以外所有日志
                if(logsName.length()>10){
                     try {
                    //获取文件创建日期
                    int beginIndex = logsName.indexOf(".");
                    int endIndex = logsName.lastIndexOf(".");
                    String logsDate = logsName.substring(beginIndex+1, endIndex);

                    Date lastLogs = format.parse(logsDate);//日期类型的文件名
                            //获取7天以前日志并删除
                            Calendar lastDate = Calendar.getInstance();
                            lastDate.roll(Calendar.DATE, -7);//日期回滚7天
                            String beforeLog1 = format.format(lastDate.getTime());
                            Date before7DaysLog = format.parse(beforeLog1);
                            //如果文件是在7天前的文件那么删除
                            if(lastLogs.before(before7DaysLog)){
                                String delPath = delpath+"\\"+logsName;
                                File deleteFile = new File(delPath);
                                deleteFile.delete();
                                logger.debug("已经删除7天前日志:"+delPath);
                            }
                    } catch (ParseException e) {
                        logger.debug("定时删除日志程序异常!");
                    }
                }
            }
        }
    }

5.如果您觉得解决了您的一个小困难,伸出您的小手,感谢打赏大笑大笑
这里写图片描述

发布了6 篇原创文章 · 获赞 2 · 访问量 5175

猜你喜欢

转载自blog.csdn.net/qq_33435280/article/details/80807361