介绍
Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs;
Quartz框架是一个全功能、开源的任务调度服务,可以集成几乎任何的java应用程序—从小的单片机系统到大型的电子商务系统。Quartz可以执行上千上万的任务调度。
核心概念
Quartz核心的概念:scheduler任务调度、Job任务、Trigger触发器、JobDetail任务细节
Job任务:其实Job是接口,其中只有一个execute方法:
package org.quartz;
public abstract interface Job
{
public abstract void execute(JobExecutionContext paramJobExecutionContext)
throws JobExecutionException;
}
我们开发者只要实现此接口,实现execute方法即可。把我们想做的事情,在execute中执行即可。
JobDetail:任务细节,Quartz执行Job时,需要新建个Job实例,但是不能直接操作Job类,所以通过JobDetail来获取Job的名称、描述信息。
Trigger触发器:执行任务的规则;比如每天,每小时等。
一般情况使用SimpleTrigger,和CronTrigger,这个触发器实现了Trigger接口。
对于复杂的时间表达式来说,比如每个月15日上午几点几分,使用CronTrigger
对于简单的时间来说,比如每天执行几次,使用SimpleTrigger
scheduler任务调度:是最核心的概念,需要把JobDetail和Trigger注册到scheduler中,才可以执行。
注意:
不同的版本的jar包,具体的操作不太相同,但是思路是相同的;比如1.8.6jar包中,JobDetail是个类,直接通过构造方法与Job类关联。SimpleTrigger和CornTrigger是类;在2.0.2jar包中,JobDetail是个接口,SimpleTrigger和CornTrigger是接口。
我的任务类
package com.smw.common.test;
import java.util.Calendar;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println(Calendar.getInstance().getTime().toLocaleString()+ "★★★★★★★★★★★");
}
}
定时任务管理器
package com.smw.common.test;
import java.text.ParseException;
import java.util.Date;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
/**
* 定时任务管理类
*
*/
public class Quartz {
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory(); // 通过schedulerFactory获取一个调度器
private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME"; // 任务组名称
private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME"; // 触发器组名称
/**
* 添加一个定时任务,使用默认的任务组名,触发器名,触发器组名
*
* @param jobName
* 任务名
* @param jobClass
* 任务类
* @param startTime
* 启动时间
* @param endTime
* 间隔时间
* @throws SchedulerException
* @throws ParseException
*/
public static void addJob(String jobName, String jobClass, Long startTime,Long endTime) {
try {
Scheduler sched = gSchedulerFactory.getScheduler(); // 通过schedulerFactory获取一个调度器
// JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME,Class.forName(jobClass));// 任务名,任务组,任务执行类
JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME,MyJob.class);// 任务名,任务组,任务执行类
/*
* // 触发器 CronTrigger trigger = new CronTrigger(jobName,
* TRIGGER_GROUP_NAME);// 触发器名,触发器组
* trigger.setCronExpression(time);// 触发器时间设定
*/
// 定义调度触发规则,比如每1秒运行一次
SimpleTrigger trigger = new SimpleTrigger(jobName, TRIGGER_GROUP_NAME);
// 启动时间
trigger.setStartTime(new Date(startTime));
// 间隔时间
trigger.setRepeatInterval(endTime);
// 运行次数
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
sched.scheduleJob(jobDetail, trigger); // 把作业和触发器注册到任务调度中
// 启动
if (!sched.isShutdown()) {
sched.start(); // 启动调度
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 添加一个定时任务
*
* @param jobName
* 任务名
* @param jobGroupName
* 任务组名
* @param triggerName
* 触发器名
* @param triggerGroupName
* 触发器组名
* @param jobClass
* 任务
* @param time
* 时间设置,参考quartz说明文档
* @throws SchedulerException
* @throws ParseException
*/
public static void addJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName, String jobClass,
String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
JobDetail jobDetail = new JobDetail(jobName, jobGroupName,
Class.forName(jobClass));// 任务名,任务组,任务执行类
// 触发器
CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 触发器名,触发器组
trigger.setCronExpression(time);// 触发器时间设定
sched.scheduleJob(jobDetail, trigger);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 修改一个任务的触发时间和间隔时间(使用默认的任务组名,触发器名,触发器组名)
*
* @param jobName 任务名称
* @param newStartTime 出发时间
* @param newStartTime 间隔时间
*/
public static void modifyJobTime(String jobName, Long newStartTime,Long newEndTime) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
//CronTrigger trigger = (CronTrigger) sched.getTrigger(jobName,TRIGGER_GROUP_NAME);
SimpleTrigger trigger = (SimpleTrigger) sched.getTrigger(jobName, TRIGGER_GROUP_NAME);
if (trigger == null) {
return;
}
//String oldTime = trigger.getCronExpression();
//if (!oldTime.equalsIgnoreCase(time)) {
Long startTime = trigger.getStartTime().getTime();
if (startTime!=newStartTime) {
JobDetail jobDetail = sched.getJobDetail(jobName,JOB_GROUP_NAME);
Class jJobClass = jobDetail.getJobClass();
String jobClass = jJobClass.getName();
removeJob(jobName);
addJob(jobName, jobClass, newStartTime,newEndTime);
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 修改一个任务的触发时间
*
* @param triggerName
* @param triggerGroupName
* @param time
*/
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) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 移除一个任务(使用默认的任务组名,触发器名,触发器组名)
*
* @param jobName
*/
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) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 移除一个任务
*
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
*/
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) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 启动所有定时任务
*/
public static void startJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.start();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 关闭所有定时任务
*/
public static void shutdownJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 暂停任务
* @param jobName 任务名称
*/
public static void pauseJob(String jobName){
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.pauseJob(jobName, JOB_GROUP_NAME);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 恢复任务
* @param jobName 任务名称
*/
public static void recoverJob(String jobName){
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.resumeJob(jobName, JOB_GROUP_NAME);
} catch (SchedulerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取任务的状态
* None:Trigger已经完成,且不会在执行,或者找不到该触发器,或者Trigger已经被删除 == -1
NORMAL:正常状态 === 0
PAUSED:暂停状态 === 1
COMPLETE:触发器完成,但是任务可能还正在执行中 == 2
BLOCKED:线程阻塞状态 == 4
ERROR:出现错误 == 3
* @param jobName
*/
public static int getJobState(String jobName){
int state = 10;
try {
Scheduler sched = gSchedulerFactory.getScheduler();
state = sched.getTriggerState(jobName, TRIGGER_GROUP_NAME);
} catch (SchedulerException e) {
e.printStackTrace();
}
return state;
}
}
测试类
package com.smw.common.test;
import java.text.ParseException;
import java.util.Date;
public class updateQuzrtz {
public static void main(String[] args) throws ParseException, InterruptedException {
String jobName="hello";
Quartz.addJob(jobName, "com.smw.common.test.MyJob",new Date().getTime(),1000L);
System.out.println("========"+Quartz.getJobState(jobName));
Thread.sleep(5000);
System.out.println("【修改时间】开始(每2秒输出一次)...");
Quartz.modifyJobTime(jobName, System.currentTimeMillis()+1000, 9000L);
System.out.println("========"+Quartz.getJobState(jobName));
Thread.sleep(5000);
System.out.println("【移除定时】开始...");
Quartz.removeJob(jobName);
System.out.println("【移除定时】成功");
System.out.println("========"+Quartz.getJobState(jobName));
Quartz.addJob("world", "com.smw.common.test.MyJob",new Date().getTime(),1000L);
Thread.sleep(5000);
System.out.println("暂停任务");
Quartz.pauseJob("world");
System.out.println("========"+Quartz.getJobState("world"));
Thread.sleep(8000);
System.out.println("恢复任务");
Quartz.recoverJob("world");
System.out.println("========"+Quartz.getJobState("world"));
}
}