如何对quartz定时任务设置结束时间
更新时间:2023年12月14日 09:12:08 作者:不喜欢吃猫的鱼
这篇文章主要介绍了如何对quartz定时任务设置结束时间问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
quartz定时任务设置结束时间
在使用quartz时,使用cornTrigger再设置一个结束时间 endAt(“结束的时间”),实现在任务执后自动销毁任务,在到了结束时间之后,job并不会留在内存中,job是直接被delete掉,所以不担心会有内存满的情况;
代码:
//按新的cronExpression表达式构建一个新的trigger trigger = 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 trigger List<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 2018/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表达式构建一个新的trigger TriggerBuilder 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表达式重新构建trigger trigger = 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表达式构建一个新的trigger trigger = 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表达式重新构建trigger trigger = 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表达式构建一个新的trigger trigger = 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表达式重新构建trigger trigger = 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 trigger List<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(); } } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
如何解决 Java 中的 IndexOutOfBoundsException 异
当我们在 Java 中使用 List 的时候,有时候会出现向 List 中不存在的位置设置新元素的情况,从而导致 IndexOutOfBoundsException 异常,本文将会介绍这个问题的产生原因以及解决方案2023-10-10
最新评论