工程结构
其中的Quartz管理工具 该类参考网络代码
package my.ceshi.data.deletelog;
import java.text.ParseException;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
/**
* 定时任务管理类
*/
public class QuartzManager {
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
private static String JOB_GROUP_NAME = "ceshi";
private static String TRIGGER_GROUP_NAME = "ceshi";
/**
* 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
*
* @param jobName 任务名
* @param jobClass 任务
* @param time 时间设置,参考quartz说明文档
* @throws SchedulerException
* @throws ParseException
*/
public static void addJob(String jobName, String jobClass, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, Class.forName(jobClass));// 任务名,任务组,任务执行类
// 触发器
CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);// 触发器名,触发器组
trigger.setCronExpression(time);// 触发器时间设定
sched.scheduleJob(jobDetail, trigger);
// 启动
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 添加一个定时任务
*
* @param jobName 任务名
* @param jobGroupName 任务组名
* @param triggerName 触发器名
* @param triggerGroupName 触发器组名
* @param jobClass 任务
* @param time 时间设置,参考quartz说明文档
* @throws SchedulerException
* @throws ParseException
*/
public static void addJob(String jobName, String jobGroupName, String triggerName,
String triggerGroupName, String jobClass, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
JobDetail jobDetail = new JobDetail(jobName, jobGroupName, Class.forName(jobClass));// 任务名,任务组,任务执行类
// 触发器
CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组
trigger.setCronExpression(time);// 触发器时间设定
sched.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 修改一个任务的触发时间(使用默认的任务组名,触发器名,触发器组名)
*
* @param jobName
* @param time
*/
public static void modifyJobTime(String jobName, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
CronTrigger trigger = (CronTrigger) sched.getTrigger(jobName, TRIGGER_GROUP_NAME);
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
JobDetail jobDetail = sched.getJobDetail(jobName, JOB_GROUP_NAME);
Class objJobClass = jobDetail.getJobClass();
String jobClass = objJobClass.getName();
removeJob(jobName);
addJob(jobName, jobClass, time);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 修改一个任务的触发时间
*
* @param triggerName
* @param triggerGroupName
* @param time
*/
public static void modifyJobTime(String triggerName, String triggerGroupName, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName, triggerGroupName);
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
CronTrigger ct = (CronTrigger) trigger;
// 修改时间
ct.setCronExpression(time);
// 重启触发器
sched.resumeTrigger(triggerName, triggerGroupName);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
*
* @param jobName
*/
public static void removeJob(String jobName) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);// 停止触发器
sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);// 移除触发器
sched.deleteJob(jobName, JOB_GROUP_NAME);// 删除任务
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 移除一个任务
*
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
*/
public static void removeJob(String jobName, String jobGroupName, String triggerName,
String triggerGroupName) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.pauseTrigger(triggerName, triggerGroupName);// 停止触发器
sched.unscheduleJob(triggerName, triggerGroupName);// 移除触发器
sched.deleteJob(jobName, jobGroupName);// 删除任务
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 启动所有定时任务
*/
public static void startJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.start();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 关闭所有定时任务
*/
public static void shutdownJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
然后读取配置文件的类
package my.ceshi.data.deletelog;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class ReadConfig {
private static Properties properties = null;
private static final String mylock = "lock";
private ReadConfig() {
}
public static Properties getInstance() {
if (properties == null) {
synchronized (mylock) {
if (properties == null) {
init();
return properties;
}
}
}
return properties;
}
private static void init() {
String s = System.getProperty("user.dir");
FileInputStream in;
try {
in = new FileInputStream(new File(s + "/delete.properties"));
properties = new Properties();
properties.load(in);
System.out.println(s + "/delete.properties");
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
QuartzManager.shutdownJobs();
System.out.println("该目录下没有delete.properties配置文件");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
QuartzManager.shutdownJobs();
System.out.println("IO异常");
}
}
}
执行删除任务的逻辑类
package my.ceshi.data.deletelog;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class DeleteServiceJob implements Job {
SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date();
String returnstr = DateFormat.format(d);
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println(returnstr + "★★★★★★★★★★★");
delete();
}
public void delete() {
Properties properties = ReadConfig.getInstance();
String path = properties.getProperty("delete.dir");
String deleteDay = properties.getProperty("delete.day");
int deleteDayInt = Integer.parseInt(deleteDay);
File file = new File(path);
//yyyy-MM-dd
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dd = new Date();
Date addDays = DateUtils.addDays(dd, deleteDayInt * -1);
String delete = sdf.format(addDays);
if (file.exists()) {
File[] listFiles = file.listFiles();
for (File f : listFiles) {
if (StringUtils.containsAny(f.getName(), delete)) {
f.delete();
}
}
}
}
}
=============================================
该类修改为以下方式 删除多个文件夹下内容
package my.ceshi.data.deletelog;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class DeleteServiceJob implements Job {
SimpleDateFormat DateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date();
String returnstr = DateFormat.format(d);
public void execute(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
System.out.println(returnstr + "★★★★★★★★★★★");
delete();
}
public void delete() {
Properties properties = ReadConfig.getInstance();
String path = properties.getProperty("delete.dir");
String deleteDay = properties.getProperty("delete.day");
int deleteDayInt = Integer.parseInt(deleteDay);
String[] split = StringUtils.split(path, ",");
List
for (String pathTemp : split) {
File file = new File(path);
if (file.isDirectory() && file.exists()) {
File[] listFile = file.listFiles();
list.addAll(Arrays.asList(listFile));
listFile = null;
}
file = null;
}
//yyyy-MM-dd
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date dd = new Date();
Date addDays = DateUtils.addDays(dd, deleteDayInt * -1);
String delete = sdf.format(addDays);
if (!list.isEmpty()) {
list.sort(new MySort());
File[] listFiles = list.toArray(new File[list.size()]);
for (File f : listFiles) {
if (StringUtils.containsAny(f.getName(), delete)) {
f.delete();
}
}
}
}
}
class MySort implements Comparator
@Override
public int compare(File o1, File o2) {
// TODO Auto-generated method stub
return o1.getName().compareTo(o2.getName());
}
}
对应的配置文件需要修改
#该日志必须要包含日期的格式(yyyy-MM-dd)命名
#该删除任务只能删除N天前当天的日志,在之前的不会删除 删除时间
#需要删除的日志文件目录 多个目录以逗号分割
delete.dir=/home/liuhua/gongzuokongjian1/nenoeclipsechanpin/myTest/logs
#删除几天前的日志文件,只删除N天前当天的日志
delete.day=3
#定义什么时间删除日志 建议在凌晨 1-3点
delete.time=0/2 * * * * ?
=============================================
启动程序的主类
package my.ceshi.data.deletelog;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class DeleteMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
SimpleDateFormat DateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
Date d = new Date();
String returnstr = DateFormat.format(d);
DeleteServiceJob job = new DeleteServiceJob();
Properties properties = ReadConfig.getInstance();
String time = properties.getProperty("delete.time");
String job_name = "11";
System.out.println(returnstr + "【系统启动】");
QuartzManager.addJob(job_name, "my.ceshi.data.deletelog.DeleteServiceJob", time); //每2秒钟执行一次
}
}
配置文件内容
#该日志必须要包含日期的格式(yyyy-MM-dd)命名
#该删除任务只能删除N天前当天的日志,在之前的不会删除 删除时间
#需要删除的日志文件目录
delete.dir=/home/liuhua/gongzuokongjian1/nenoeclipsechanpin/myTest/logs
#删除几天前的日志文件,只删除N天前当天的日志
delete.day=3
#定义什么时间删除日志 建议在凌晨 1-3点
delete.time=0/2 * * * * ?
其中pom.xml文件配置
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
其中真正打包后的数据结构