在使用quartz时,使用cornTrigger再设置一个结束时间 endAt(“结束的时间”),实现在任务执后自动销毁任务,在到了结束时间之后,job并不会留在内存中,job是直接被delete掉,所以不担心会有内存满的情况;
代码:
//按新的cronExpression表达式构建一个新的triggertrigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName).withSchedule(scheduleBuilder).endAt(endDate).build();
附属上查询所有job代码:
/*** 查询所有的job*/public static void getAllJobs(){try {Scheduler scheduler = sf.getScheduler();for (String groupName : scheduler.getJobGroupNames()) {for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {String jobName = jobKey.getName();String jobGroup = jobKey.getGroup();//get job's triggerList<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(jobKey);Date nextFireTime = triggers.get(0).getNextFireTime();System.out.println("[jobName] : " + jobName + " [groupName] : "+ jobGroup + " - " + nextFireTime);}}} catch (Exception e) {e.printStackTrace();}}
下面是完整的quartz工具类:
import org.quartz.*;import org.quartz.impl.StdSchedulerFactory;import org.quartz.impl.matchers.GroupMatcher;import qgs.framework.core.task.JobFactory;import qgs.framework.util.utilty.DateUtil;import java.text.ParseException;import java.util.Date;import java.util.List;/*** create by XXX on /09/03* 定时任务工具类*/public class QuartzUtil {private static SchedulerFactory sf = new StdSchedulerFactory();private static JobFactory jobFactory = new JobFactory();private static String JOB_GROUP_NAME = "group1";private static String TRIGGER_GROUP_NAME = "trigger1";/*** 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名** @param jobName 任务名* @param jobClass 任务类名* @param time时间设置,参考quartz说明文档* @param data定时任务所带参数数据* @param endDate 定时任务生命周期结束时间* @throws ParseException*/public static void saveJobCron(String jobName, Class<? extends Job> jobClass, String time, JobDataMap data, Date endDate) throws Exception {saveJobCron(jobName, JOB_GROUP_NAME, jobName, TRIGGER_GROUP_NAME, jobClass, time, data, endDate);}/*** 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名** @param jobName 任务名* @param jobClass 任务类名* @param time时间设置,参考quartz说明文档* @throws ParseException*/public static void saveJobCron(String jobName, Class<? extends Job> jobClass, String time) throws Exception {saveJobCron(jobName, JOB_GROUP_NAME, jobName, TRIGGER_GROUP_NAME, jobClass, time);}/** *//*** 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名** @param jobName 任务名* @param jobClass 任务类名* @param time时间间隔 每隔多少时间执行一次 单位毫秒 如 1000*60*60 = 1小时* @throws ParseException*/public static void saveJobSimple(String jobName, Class<? extends Job> jobClass, Integer time) throws Exception {saveJobSimple(jobName, JOB_GROUP_NAME, jobName, TRIGGER_GROUP_NAME, jobClass, time, true);}public static void saveJobSimple(String jobName, Class<? extends Job> jobClass, Integer time, boolean firstRun) throws Exception {saveJobSimple(jobName, JOB_GROUP_NAME, jobName, TRIGGER_GROUP_NAME, jobClass, time, firstRun);}/** *//*** 添加一个定时任务** @param jobName任务名* @param jobGroupName任务组名* @param triggerName触发器名* @param triggerGroupName 触发器组名* @param jobClass 任务类名* @param time 时间间隔 每隔多少时间执行一次 单位秒 如 1000*60*60 = 1小时* @throws SchedulerException* @throws ParseException* @throws ParseException*/public static void saveJobSimple(String jobName, String jobGroupName, String triggerName,String triggerGroupName, Class<? extends Job> jobClass, Integer time, boolean firstRun)throws Exception {Scheduler scheduler = sf.getScheduler();scheduler.setJobFactory(jobFactory);TriggerKey triggerKey = TriggerKey.triggerKey(jobName, triggerGroupName);SimpleTrigger trigger = (SimpleTrigger) scheduler.getTrigger(triggerKey);//不存在,创建一个if (null == trigger) {JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();//按新的cronExpression表达式构建一个新的triggerTriggerBuilder s = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName);if (!firstRun) {s.startAt(DateUtil.addSecond(new Date(), time));}trigger = (SimpleTrigger) s.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(time).repeatForever()).build();scheduler.scheduleJob(jobDetail, trigger);//启动if (!scheduler.isShutdown())scheduler.start();} else {// Trigger已存在,那么更新相应的定时设置//按新的cronExpression表达式重新构建triggertrigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(time).repeatForever()).build();//按新的trigger重新设置job执行scheduler.rescheduleJob(triggerKey, trigger);}}/*** 添加一个定时任务** @param jobName任务名* @param jobGroupName任务组名* @param triggerName触发器名* @param triggerGroupName 触发器组名* @param jobClass 任务类名* @param time 时间设置,参考quartz说明文档* @param data 对应定时认为中所涉及的数据* @param endDate定时任务周期结束时间* @throws SchedulerException* @throws ParseException* @throws ParseException*/public static void saveJobCron(String jobName, String jobGroupName, String triggerName,String triggerGroupName, Class<? extends Job> jobClass, String time,JobDataMap data, Date endDate)throws Exception {Scheduler scheduler = sf.getScheduler();scheduler.setJobFactory(jobFactory);TriggerKey triggerKey = TriggerKey.triggerKey(jobName, triggerGroupName);CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);//不存在,创建一个if (null == trigger) {JobDetail jobDetail =JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).setJobData(data).build();//表达式调度构建器CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(time);//按新的cronExpression表达式构建一个新的triggertrigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName).withSchedule(scheduleBuilder).endAt(endDate).build();scheduler.scheduleJob(jobDetail, trigger);//启动if (!scheduler.isShutdown())scheduler.start();} else {// Trigger已存在,那么更新相应的定时设置//表达式调度构建器CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(time);//按新的cronExpression表达式重新构建triggertrigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).startAt(new Date()).endAt(endDate).build();//按新的trigger重新设置job执行scheduler.rescheduleJob(triggerKey, trigger);}}/*** 添加一个定时任务** @param jobName任务名* @param jobGroupName任务组名* @param triggerName触发器名* @param triggerGroupName 触发器组名* @param jobClass 任务类名* @param time 时间设置,参考quartz说明文档* @throws SchedulerException* @throws ParseException* @throws ParseException*/public static void saveJobCron(String jobName, String jobGroupName, String triggerName,String triggerGroupName, Class<? extends Job> jobClass, String time) throws Exception {Scheduler scheduler = sf.getScheduler();scheduler.setJobFactory(jobFactory);TriggerKey triggerKey = TriggerKey.triggerKey(jobName, triggerGroupName);CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);//不存在,创建一个if (null == trigger) {JobDetail jobDetail =JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();//表达式调度构建器CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(time);//按新的cronExpression表达式构建一个新的triggertrigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName).withSchedule(scheduleBuilder).build();scheduler.scheduleJob(jobDetail, trigger);//启动if (!scheduler.isShutdown())scheduler.start();} else {// Trigger已存在,那么更新相应的定时设置//表达式调度构建器CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(time);//按新的cronExpression表达式重新构建triggertrigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();//按新的trigger重新设置job执行scheduler.rescheduleJob(triggerKey, trigger);}}/** *//*** 移除一个任务(使用默认的任务组名,触发器名,触发器组名)** @param jobName* @throws SchedulerException*/public static void removeJob(String jobName) throws Exception {removeJob(jobName, JOB_GROUP_NAME, jobName, TRIGGER_GROUP_NAME);}/** *//*** 移除一个任务** @param jobName* @param jobGroupName* @param triggerName* @param triggerGroupName* @throws SchedulerException*/public static void removeJob(String jobName, String jobGroupName, String triggerName,String triggerGroupName) throws Exception {Scheduler scheduler = sf.getScheduler();TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName);scheduler.pauseTrigger(triggerKey);// 停止触发器scheduler.unscheduleJob(triggerKey);// 移除触发器scheduler.deleteJob(JobKey.jobKey(jobName, jobGroupName));// 删除任务}public static void shutdownJobs() throws Exception {Scheduler scheduler = sf.getScheduler();try {if (!scheduler.isShutdown()) {scheduler.shutdown();}} catch (Exception e) {throw new RuntimeException(e);}}/*** 查询所有的job*/public static void getAllJobs(){try {Scheduler scheduler = sf.getScheduler();for (String groupName : scheduler.getJobGroupNames()) {for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {String jobName = jobKey.getName();String jobGroup = jobKey.getGroup();//get job's triggerList<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(jobKey);Date nextFireTime = triggers.get(0).getNextFireTime();System.out.println("[jobName] : " + jobName + " [groupName] : "+ jobGroup + " - " + nextFireTime);}}} catch (Exception e) {e.printStackTrace();}}}
如果觉得《对于quartz定时任务设置结束时间》对你有帮助,请点赞、收藏,并留下你的观点哦!