定时器quartz工具类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了定时器quartz工具类相关的知识,希望对你有一定的参考价值。
一、gradle配置
// https://mvnrepository.com/artifact/org.quartz-scheduler/quartz compile group: ‘org.quartz-scheduler‘, name: ‘quartz‘, version: ‘2.2.3‘ // https://mvnrepository.com/artifact/org.quartz-scheduler/quartz-jobs compile group: ‘org.quartz-scheduler‘, name: ‘quartz-jobs‘, version: ‘2.2.3‘
二、工具类
1 import static org.quartz.CronScheduleBuilder.cronSchedule; 2 import static org.quartz.JobBuilder.newJob; 3 import static org.quartz.SimpleScheduleBuilder.simpleSchedule; 4 import static org.quartz.TriggerBuilder.newTrigger; 5 6 import java.util.Collection; 7 import java.util.Date; 8 import java.util.Iterator; 9 import java.util.List; 10 import java.util.Set; 11 12 import org.quartz.JobDataMap; 13 import org.quartz.JobDetail; 14 import org.quartz.JobKey; 15 import org.quartz.Scheduler; 16 import org.quartz.SchedulerException; 17 import org.quartz.SchedulerFactory; 18 import org.quartz.Trigger; 19 import org.quartz.impl.StdSchedulerFactory; 20 import org.quartz.impl.matchers.GroupMatcher; 21 22 public class QuartzUtil { 23 public static SchedulerFactory schedulerFactory = new StdSchedulerFactory(); 24 25 @SuppressWarnings({ "unchecked", "rawtypes" }) 26 public void startDelayTimeJob(String key, String group, int delayMillisecond, Class clz) { 27 try { 28 Scheduler sched = schedulerFactory.getScheduler(); 29 sched.start(); 30 long fTime = System.currentTimeMillis() + delayMillisecond; 31 JobDetail job = newJob(clz).withIdentity(key, group).build(); 32 Trigger trigger = newTrigger().withIdentity(key, group).startAt(new Date(fTime)) 33 .withSchedule(simpleSchedule().withRepeatCount(0)).build(); 34 sched.scheduleJob(job, trigger); 35 36 } catch (SchedulerException e) { 37 e.printStackTrace(); 38 } 39 } 40 41 @SuppressWarnings({ "unchecked", "rawtypes" }) 42 public void startFixedDateJob(String key, String group, Date triggerStartTime, Class clz) { 43 try { 44 Scheduler sched = schedulerFactory.getScheduler(); 45 sched.start(); 46 JobDetail job = newJob(clz).withIdentity(key, group).build(); 47 Trigger trigger = newTrigger().withIdentity(key, group).startAt(triggerStartTime) 48 .withSchedule(simpleSchedule().withRepeatCount(0)).build(); 49 sched.scheduleJob(job, trigger); 50 51 } catch (SchedulerException e) { 52 e.printStackTrace(); 53 } 54 } 55 56 @SuppressWarnings({ "unchecked", "rawtypes" }) 57 public void startCycleLimitedJob(String key, String group, int interval, int count, Class clz) { 58 try { 59 Scheduler sched = schedulerFactory.getScheduler(); 60 sched.start(); 61 JobDetail job = newJob(clz).withIdentity(key, group).build(); 62 Trigger trigger = newTrigger().withIdentity(key, group).startNow() 63 .withSchedule(simpleSchedule().withIntervalInSeconds(interval).withRepeatCount(count)).build(); 64 sched.scheduleJob(job, trigger); 65 66 } catch (SchedulerException e) { 67 e.printStackTrace(); 68 } 69 } 70 71 @SuppressWarnings({ "unchecked", "rawtypes" }) 72 public void startCronJob(String key, String group, String cron, Class clz) { 73 try { 74 Scheduler sched = schedulerFactory.getScheduler(); 75 sched.start(); 76 JobDetail job = newJob(clz).withIdentity(key, group).build(); 77 Trigger trigger = newTrigger().withIdentity(key, group).startNow().withSchedule(cronSchedule(cron)).build(); 78 sched.scheduleJob(job, trigger); 79 80 } catch (SchedulerException e) { 81 e.printStackTrace(); 82 } 83 } 84 85 @SuppressWarnings({ "unchecked", "rawtypes" }) 86 public void startCronJobWithData(String key, String group, String cron, Class clz, JobDataMap map) { 87 try { 88 Scheduler sched = schedulerFactory.getScheduler(); 89 sched.start(); 90 JobDetail job = newJob(clz).withIdentity(key, group).setJobData(map).build(); 91 Trigger trigger = newTrigger().withIdentity(key, group).startNow().withSchedule(cronSchedule(cron)).build(); 92 sched.scheduleJob(job, trigger); 93 94 } catch (SchedulerException e) { 95 e.printStackTrace(); 96 } 97 } 98 99 @SuppressWarnings({ "unchecked", "rawtypes" }) 100 public void startCronJobWithData(String jobName, String jobGroup, String triggerName, String triggerGroup, 101 String cron, Class clz, JobDataMap map) { 102 try { 103 Scheduler sched = schedulerFactory.getScheduler(); 104 sched.start(); 105 JobDetail job = newJob(clz).withIdentity(jobName, jobGroup).setJobData(map).build(); 106 Trigger trigger = newTrigger().withIdentity(triggerName, triggerGroup).startNow() 107 .withSchedule(cronSchedule(cron)).build(); 108 sched.scheduleJob(job, trigger); 109 110 } catch (SchedulerException e) { 111 e.printStackTrace(); 112 } 113 } 114 115 @SuppressWarnings({ "unchecked", "rawtypes" }) 116 public void startCronJob(String jobName, String jobGroup, String triggerName, String triggerGroup, String cron, 117 Class clz) { 118 try { 119 System.out.println("schedFactoryId : " + schedulerFactory.toString()); 120 Scheduler sched = schedulerFactory.getScheduler(); 121 sched.start(); 122 System.out.println("schedId : " + sched.getSchedulerInstanceId() + ",schedName : " 123 + sched.getSchedulerName() + ", " + sched.toString()); 124 JobDetail job = newJob(clz).withIdentity(jobName, jobGroup).build(); 125 Trigger trigger = newTrigger().withIdentity(triggerName, triggerGroup).startNow() 126 .withSchedule(cronSchedule(cron)).build(); 127 sched.scheduleJob(job, trigger); 128 129 } catch (SchedulerException e) { 130 e.printStackTrace(); 131 } 132 } 133 134 public void stopJob(String jobName, String jobGroup) { 135 JobKey jk = new JobKey(jobName, jobGroup); 136 Collection<Scheduler> collection; 137 try { 138 collection = schedulerFactory.getAllSchedulers(); 139 Iterator<Scheduler> iter = collection.iterator(); 140 while (iter.hasNext()) { 141 Scheduler sched = iter.next(); 142 for (String groupName : sched.getJobGroupNames()) { 143 for (JobKey jobKey : sched.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) { 144 if (jobKey.equals(jk)) { 145 sched.deleteJob(jk); 146 System.out.println("[Stop] job : " + jobKey); 147 } 148 } 149 } 150 } 151 } catch (SchedulerException e) { 152 e.printStackTrace(); 153 } 154 155 } 156 157 public void printJob() { 158 Collection<Scheduler> collection; 159 try { 160 collection = schedulerFactory.getAllSchedulers(); 161 System.out.println("[Print] Current Scheduler Size : " + collection.size()); 162 Iterator<Scheduler> iter = collection.iterator(); 163 while (iter.hasNext()) { 164 Scheduler sched = iter.next(); 165 List<String> groupList = sched.getJobGroupNames(); 166 System.out.println("[Print] Current Group Size : " + groupList.size()); 167 for (String groupName : groupList) { 168 Set<JobKey> jobKeySet = sched.getJobKeys(GroupMatcher.jobGroupEquals(groupName)); 169 System.out.println("[Print] Current JOB Size : " + jobKeySet.size()); 170 for (JobKey jobKey : jobKeySet) { 171 System.out.println("[Print] Current JOB : " + jobKey); 172 // System.out.println(sched.getTriggersOfJob(jobKey)); 173 } 174 } 175 } 176 } catch (SchedulerException e) { 177 e.printStackTrace(); 178 } 179 } 180 181 }
三、希望每个需要的人都能轻松的看懂,尽管我没有注释。
以上是关于定时器quartz工具类的主要内容,如果未能解决你的问题,请参考以下文章
quartz任务调度怎么实现远程控制Job,麻烦大哥大嫂帮忙看下
java sql 编辑器 数据库备份还原 quartz 定时任务调度 自定义表单 java 图片爬虫 集成代码生成器
java sql编辑器 数据库备份还原 quartz定时任务调度 自定义表单 java图片爬虫 java代码生成器