Quartz工具类
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzUtil {
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
private static String JOB_GROUP_NAME = "FH_JOBGROUP_NAME";
private static String TRIGGER_GROUP_NAME = "FH_TRIGGERGROUP_NAME";
public static void addJob(String jobName, Class<? extends Job> cls, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(jobName, JOB_GROUP_NAME).build();
CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger()
.withIdentity(jobName, TRIGGER_GROUP_NAME)
.withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
sched.scheduleJob(jobDetail, trigger);
if (!sched.isShutdown()) {
sched.start();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void startJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void shutdownJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}