|
@@ -0,0 +1,76 @@
|
|
|
+package com.zcxk.config;
|
|
|
+import com.zcxk.job.TestJob;
|
|
|
+import org.quartz.*;
|
|
|
+import org.quartz.impl.JobDetailImpl;
|
|
|
+import org.quartz.impl.StdSchedulerFactory;
|
|
|
+
|
|
|
+import javax.print.attribute.standard.JobName;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+import static java.lang.Thread.sleep;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description: 定时任务管理类
|
|
|
+ * Created by zhangxb on 2017/2/10.
|
|
|
+ */
|
|
|
+public class QuartzManager {
|
|
|
+ private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
|
|
|
+ private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";
|
|
|
+ private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Description: 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
|
|
|
+ *
|
|
|
+ * @param jobName
|
|
|
+ * 任务名
|
|
|
+ * @param cls
|
|
|
+ * 任务
|
|
|
+ * @param time
|
|
|
+ * 时间设置,参考quartz说明文档
|
|
|
+ *
|
|
|
+ * @Title: QuartzManager.java
|
|
|
+ */
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static void addJob(String jobName, Class cls, String time,Object params) {
|
|
|
+ try {
|
|
|
+ Scheduler sched = gSchedulerFactory.getScheduler();
|
|
|
+ JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(jobName, JOB_GROUP_NAME )// 任务名称和组构成任务key
|
|
|
+ .build();
|
|
|
+ jobDetail.getJobDataMap().put("params", params);
|
|
|
+ // 触发器
|
|
|
+ Trigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, JOB_GROUP_NAME)// 触发器key
|
|
|
+ .startAt(DateBuilder.futureDate(1, DateBuilder.IntervalUnit.SECOND))
|
|
|
+ .withSchedule(CronScheduleBuilder.cronSchedule(time)).startNow().build();
|
|
|
+ sched.scheduleJob(jobDetail, trigger);
|
|
|
+ // 启动
|
|
|
+ if (!sched.isShutdown()) {
|
|
|
+ sched.start();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ public static void deleteJob(String jobName) {
|
|
|
+ JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME);
|
|
|
+ try {
|
|
|
+ Scheduler sched = gSchedulerFactory.getScheduler();
|
|
|
+ sched.deleteJob(jobKey);
|
|
|
+ } catch (SchedulerException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ QuartzManager.addJob("test", TestJob.class,"*/5 * * * * ?",new HashMap<>());
|
|
|
+ try {
|
|
|
+ sleep(30000);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ QuartzManager.deleteJob("test");
|
|
|
+ }
|
|
|
+}
|