Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。Quartz框架包含了调度器监听、作业和触发器监听。你可以配置作业和触发器监听为全局监听或者是特定于作业和触发器的监听。Quartz 允许开发人员根据时间间隔(或天)来调度作业。它实现了作业和触发器的多对多关系,还能把多个作业与不同的触发器关联。整合了 Quartz 的应用程序可以重用来自不同事件的作业,还可以为一个事件组合多个作业。并且还能和Spring配置整合使用。Quartz在功能上远远超越了JDK自带的Timer,很好很强大!好啦,直接上代码:
- package com.joyce.quartz;
-
- import org.quartz.CronTrigger;
- import org.quartz.JobDetail;
- import org.quartz.Scheduler;
- import org.quartz.SchedulerFactory;
- import org.quartz.impl.StdSchedulerFactory;
-
- 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";
-
-
- @SuppressWarnings("unchecked")
- public static void addJob(String jobName, Class cls, String time) {
- try {
- Scheduler sched = gSchedulerFactory.getScheduler();
- JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, cls);
-
- CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);
- trigger.setCronExpression(time);
- sched.scheduleJob(jobDetail, trigger);
-
- if (!sched.isShutdown()) {
- sched.start();
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
-
- @SuppressWarnings("unchecked")
- public static void addJob(String jobName, String jobGroupName,
- String triggerName, String triggerGroupName, Class jobClass,
- String time) {
- try {
- Scheduler sched = gSchedulerFactory.getScheduler();
- JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);
-
- CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);
- trigger.setCronExpression(time);
- sched.scheduleJob(jobDetail, trigger);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
-
- @SuppressWarnings("unchecked")
- public static void modifyJobTime(String jobName, String time) {
- try {
- Scheduler sched = gSchedulerFactory.getScheduler();
- CronTrigger trigger = (CronTrigger) sched.getTrigger(jobName,TRIGGER_GROUP_NAME);
- if (trigger == null) {
- return;
- }
- String oldTime = trigger.getCronExpression();
- if (!oldTime.equalsIgnoreCase(time)) {
- JobDetail jobDetail = sched.getJobDetail(jobName,JOB_GROUP_NAME);
- Class objJobClass = jobDetail.getJobClass();
- removeJob(jobName);
- addJob(jobName, objJobClass, time);
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
-
- public static void modifyJobTime(String triggerName,
- String triggerGroupName, String time) {
- try {
- Scheduler sched = gSchedulerFactory.getScheduler();
- CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName,triggerGroupName);
- if (trigger == null) {
- return;
- }
- String oldTime = trigger.getCronExpression();
- if (!oldTime.equalsIgnoreCase(time)) {
- CronTrigger ct = (CronTrigger) trigger;
-
- ct.setCronExpression(time);
-
- sched.resumeTrigger(triggerName, triggerGroupName);
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
-
- public static void removeJob(String jobName) {
- try {
- Scheduler sched = gSchedulerFactory.getScheduler();
- sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);
- sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);
- sched.deleteJob(jobName, JOB_GROUP_NAME);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
-
- public static void removeJob(String jobName, String jobGroupName,
- String triggerName, String triggerGroupName) {
- try {
- Scheduler sched = gSchedulerFactory.getScheduler();
- sched.pauseTrigger(triggerName, triggerGroupName);
- sched.unscheduleJob(triggerName, triggerGroupName);
- sched.deleteJob(jobName, jobGroupName);
- } 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);
- }
- }
- }
以上就是quartz任务调度对于任务的常用操作,封装起来以便在外部调用!这样我们就需要任务的执行了:
- package com.joyce.quartz;
-
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- import org.quartz.Job;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
-
- public class QuartzJob implements Job {
-
- @Override
- public void execute(JobExecutionContext arg0) throws JobExecutionException {
- System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+ "★★★★★★★★★★★");
- }
- }
Ok,我们来测试一下:
- package com.joyce.quartz.main;
-
- import com.joyce.quartz.QuartzJob;
- import com.joyce.quartz.QuartzManager;
-
- public class QuartzTest {
- public static void main(String[] args) {
- try {
- String job_name = "动态任务调度";
- System.out.println("【系统启动】开始(每1秒输出一次)...");
- QuartzManager.addJob(job_name, QuartzJob.class, "0/1 * * * * ?");
-
- Thread.sleep(5000);
- System.out.println("【修改时间】开始(每2秒输出一次)...");
- QuartzManager.modifyJobTime(job_name, "10/2 * * * * ?");
- Thread.sleep(6000);
- System.out.println("【移除定时】开始...");
- QuartzManager.removeJob(job_name);
- System.out.println("【移除定时】成功");
-
- System.out.println("【再次添加定时任务】开始(每10秒输出一次)...");
- QuartzManager.addJob(job_name, QuartzJob.class, "*/10 * * * * ?");
- Thread.sleep(60000);
- System.out.println("【移除定时】开始...");
- QuartzManager.removeJob(job_name);
- System.out.println("【移除定时】成功");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }