1、修改 监测数据删除 的bug
This commit is contained in:
@@ -211,7 +211,7 @@ public class BaseConfig extends JFinalConfig{
|
||||
timer.schedule(new DetectDataFileReadTask(timer), 2000);
|
||||
logger.info("文件定时读取任务启动");
|
||||
Timer clearTableTimer=new Timer();
|
||||
clearTableTimer.schedule(new LogTimerTask(clearTableTimer), new Date());
|
||||
clearTableTimer.scheduleAtFixedRate(new LogTimerTask(), 1000L, 24*60*60*1000);//每天执行一次删除7天之前的数据
|
||||
logger.info("监测数据定时清理任务启动");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,70 +1,53 @@
|
||||
package com.nis.gloam.task;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.jfinal.kit.PropKit;
|
||||
import com.jfinal.plugin.activerecord.Db;
|
||||
/**
|
||||
* 定时器 定时清除日志表中的数据
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
public class LogTimerTask extends TimerTask {
|
||||
private Timer timer;
|
||||
private Logger logger=Logger.getLogger(LogTimerTask.class);
|
||||
public LogTimerTask(Timer timer){
|
||||
this.timer = timer;
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
int date = 7;//默认七天
|
||||
try {
|
||||
PropKit.use("config.properties");
|
||||
date=Integer.parseInt(PropKit.get("delete_logtime"));
|
||||
} catch (Exception e) {
|
||||
logger.info("获取时间配置失败!使用默认时间:"+date);
|
||||
}
|
||||
|
||||
try {
|
||||
String[] clearTables=PropKit.get("detect_info_clear").split(",");
|
||||
logger.info("clearTables-->"+Arrays.toString(clearTables));
|
||||
Calendar calendar=new GregorianCalendar();
|
||||
calendar.setTime(new Date());
|
||||
calendar.add(calendar.DATE, -date);
|
||||
for(String table:clearTables){
|
||||
if(table.startsWith("_")){
|
||||
if(table.equals("_nmsc")){
|
||||
table="delect_info"+table;
|
||||
}else{
|
||||
table="detect_info"+table;
|
||||
}
|
||||
|
||||
}
|
||||
String sql="delete from "+table+" where DATA_CHECK_TIME_DIGITAL<?";
|
||||
logger.debug("监测表清理sql-->"+sql+calendar.getTime().getTime());
|
||||
|
||||
Db.update(sql,calendar.getTime().getTime());
|
||||
logger.info("清理表:"+table+"完成");
|
||||
|
||||
|
||||
}
|
||||
restart(date);
|
||||
} catch (Exception e) {
|
||||
logger.error("监测表清理失败!",e);
|
||||
restart(date);
|
||||
}
|
||||
}
|
||||
//定时删除日志 一周清除一次一周前的日志 可修改配置文件来设定日志保存天数
|
||||
public void restart(int day){
|
||||
timer.schedule(new LogTimerTask(timer), day*24*3600*1000);
|
||||
logger.info("数据清除任务重启完成!");
|
||||
}
|
||||
|
||||
}
|
||||
package com.nis.gloam.task;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.TimerTask;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.jfinal.kit.PropKit;
|
||||
import com.jfinal.plugin.activerecord.Db;
|
||||
|
||||
/**
|
||||
* 定时器 定时清除日志表中的数据
|
||||
*
|
||||
* @author Administrator
|
||||
*
|
||||
*/
|
||||
public class LogTimerTask extends TimerTask {
|
||||
private Logger logger = Logger.getLogger(LogTimerTask.class);
|
||||
|
||||
public LogTimerTask() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
PropKit.use("config.properties");
|
||||
int date = PropKit.getInt("delete_logtime",7);
|
||||
String[] clearTables = PropKit.get("detect_info_clear").split(",");
|
||||
logger.info("clearTables-->" + Arrays.toString(clearTables));
|
||||
Calendar calendar = new GregorianCalendar();
|
||||
calendar.setTime(new Date());
|
||||
calendar.add(calendar.DATE, -date);
|
||||
for (String table : clearTables) {
|
||||
if (table.startsWith("_")) {
|
||||
if (table.equals("_nmsc")) {
|
||||
table = "delect_info" + table;
|
||||
} else {
|
||||
table = "detect_info" + table;
|
||||
}
|
||||
}
|
||||
String sql = "delete from " + table + " where DATA_CHECK_TIME_DIGITAL<?";
|
||||
logger.debug("监测表清理sql-->" + sql + calendar.getTime().getTime());
|
||||
Db.update(sql, calendar.getTime().getTime());
|
||||
logger.info("清理表:" + table + "完成");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("监测表清理失败!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user