Quartz
调度器(scheduler):定时定频率的去执行任务
任务(job):业务逻辑
触发器(trigger):让任务生效的时间
JobDetail(包含任务实现类,任务信息)
trigger(触发器)
SimpleTrigger()
CronTrigger(实现复杂的业务逻辑 常用)
scheduler
重要组成
Job
JobDetail(通过反射机制实现job)
JobBuilder
JobStore
Trigger(类)
TriggerBuilder
ThreadPool
Scheduler
Calendar
监听器
JobListener TriggerListener SchedulerListener
Quartz使用写自己的job类实现job接口
在execute方法中编写业务逻辑
自定义类
定义jobDetail trigger 对象
Demo
myjob
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
// 打印当前时间
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Current Exec Time Is:" + sf.format(date));
// 编写具体的业务逻辑
System.out.println("Hello World!");
}
}
package cn.miye.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class HelloScheduler {
public static void main(String[] args) throws SchedulerException {
// 创建一个JobDetail实例,将该实例与HelloJobClass绑定
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
.withIdentity("myJob", "group1")
.usingJobData("message","hello myJob1")
.usingJobData("FloatJobValue",3.14F)
.build();
// 创建一个Trigger实例,定义该job立即执行,并每隔两秒钟执行一次,直到程序停止
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("mytrigger", "group1")
.usingJobData("message","hello myTrigger1")
.usingJobData("DoubleTriggerValue",2.0D)
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(2).repeatForever()).build();
// 创建Scheduler实例
SchedulerFactory sfact = new StdSchedulerFactory();
Scheduler scheduler = sfact.getScheduler();
scheduler.start();
// 打印当前时间
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Current Exec Time Is:" + sf.format(date));
scheduler.scheduleJob(jobDetail, trigger);
}
}
job实例在Quartz中额生命周期
每次调度器执行job时,它在调用execute方法前会创建一个新的job实例
当调用完成后,关联的job对象实例会被释放,释放的实例会被垃圾回收机制回收
jobDetai
JobDetail为job实例提供了许多设置属性,以及jobDataMap成员变量属性,它用来存储特定job实例的状态信息,调度器需要借助jobDetail对象来添加job实例
jobdetail用来绑定job,携带job的一些信息
job的一些属性 name group jobClass jobDataMap
组默认不写是DEFAULT
取出jibDetail的属性
jobDetail.getKey().getName();
jobDetail.getKey().getGroup();
JobExectionContext
当Scheduler调用一个job,就会将jobExecutionContext传递给job的execute()方法
job能通过jobExecutionContext对象访问到Quartz运行时候的环境及job本身的明细数据
JobDataMap
在进行任务调度时JobDataMap存储在JobExecutionContext中,非常方便获取
JobDataMap可以用来装载任何可序列化的数据对象
当job实例对象被执行时这些参数对象会传递给它
JobDataMap实现JDK的Map接口,并且添加了一些非常方便的方法用来存取基本数据类型
(键值对形式存取)
获取JobDataMap的两种方式
从Map中直接获取
map中直接获取
package cn.miye.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.TriggerKey;
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
// 打印当前时间
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Current Exec Time Is:" + sf.format(date));
// 编写具体的业务逻辑
System.out.println("Hello World!");
//获取jobDetail的名称和分组
JobKey key = context.getJobDetail().getKey();
System.out.println(key.getName()+":"+key.getGroup());
//获取trigger的名称和分组
TriggerKey trkey = context.getTrigger().getKey();
System.out.println(trkey.getName()+":"+trkey.getGroup());
//获取自定义传入的自定义参数的值
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
JobDataMap tdataMap = context.getTrigger().getJobDataMap();
String jobMsg = dataMap.getString("message");
float jobFloatValue = dataMap.getFloat("FloatJobValue");
String triggerMsg = tdataMap.getString("message");
Double triggerDoubleValue = tdataMap.getDouble("DoubleTriggerValue");
System.out.println(jobMsg+"--"+jobFloatValue);
System.out.println(triggerMsg+"--"+triggerDoubleValue);
//获取jobDetail和trigger合并的key
//如果jobDetail和trigger的key值相同,那么trigger会覆盖jobDetail
JobDataMap mergedJobDataMap = context.getMergedJobDataMap();
String msg = mergedJobDataMap.getString("message");
Float mergetJobFloatValue = mergedJobDataMap.getFloat("FloatJobValue");
Double mergeTriggerDoubleValue = mergedJobDataMap.getDouble("DoubleTriggerValue");
System.out.println(msg);
System.out.println(mergetJobFloatValue);
System.out.println(mergeTriggerDoubleValue);
}
}
job实现类中添加setter方法对应JobDataMap的键值
(Quartz框架默认的JobFactory实现类在初始化job实例对象时会自动的调用这些setter方法)
通过setter方法获取(定义的成员变量名称应与传入的key的名称相同)
package cn.miye.web;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.TriggerKey;
public class HelloJob implements Job {
private String message;
private Float FloatJobValue;
private Double DoubleTriggerValue;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Float getFloatJobValue() {
return FloatJobValue;
}
public void setFloatJobValue(Float floatJobValue) {
FloatJobValue = floatJobValue;
}
public Double getDoubleTriggerValue() {
return DoubleTriggerValue;
}
public void setDoubleTriggerValue(Double doubleTriggerValue) {
DoubleTriggerValue = doubleTriggerValue;
}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
// 打印当前时间
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("Current Exec Time Is:" + sf.format(date));
System.out.println(message);
System.out.println(FloatJobValue);
System.out.println(DoubleTriggerValue);
}
}
传递自定义参数
.usingJobData("key1","value1").usingJobData("key2",value2)