1. 使用quartz
在开始使用scheduler前,首先使用SchedulerFactory创建一个
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schedFact.getScheduler();
sched.start();
// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
.withIdentity("myJob", "group1")
.build();
// Trigger the job to run now, and then every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("myTrigger", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(40)
.repeatForever())
.build();
// Tell quartz to schedule the job using our trigger
sched.scheduleJob(job, trigger);
2. Quartz Api jobs 和 trigger
2.1 Quartz Api
[x] Scheduler
[x] Job
[x] JobDetail
[x] Trigger
[x] JobBuilder
[x] TiggerBuilder
JobBuilder
TriggerBuilder
SimpleScheduleBulder
import static org.quartz.JobBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;
import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.CalendarIntervalScheduleBuilder.*;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.DateBuilder.*;
不同的ScheduleBuilder类有自己的方法定义schedules类型
比如DateBuilder类包含各种易于构造java.util的方法。指定时间点的日期实例
2.2 Jobs and triggers
job interface
一个Job需要实现job 接口
package org.quartz;
public interface Job {
public void execute(JobExecutionContext context)
throws JobExecutionException;
}
当job的trigger触发时,execute(..)方法被==scheduler’s==的一个工作线程执行。JobExecutionContext内含有job的运行信息:执行它的Scheduler,触发它的Trigger,JobDetail 和其他一些items。
JobDetail对象是由Quartz客户机(您的程序)在将作业添加到调度器时创建的。它包含作业的各种属性设置,以及JobDataMap,可用于存储作业类的给定实例的状态信息。它本质上是作业实例的定义,并将在下一课中进一步详细讨论。
触发器对象用于触发作业的执行(或“触发”)。当您希望调度作业时,您将实例化触发器并“优化”其属性,以提供您希望的调度。触发器也可能与JobDataMap关联——这对于将参数传递给特定于触发器触发的作业非常有用。Quartz附带了一些不同的触发器类型,但是最常用的类型是SimpleTrigger和CronTrigger。
如果您需要“一次性”执行(在给定的时间点只执行一个作业),或者需要在给定的时间点触发一个作业,并让它重复N次,两次执行之间的延迟为T,那么SimpleTrigger非常有用。如果您希望基于类似日历的时间表进行触发,例如“每个星期五、中午”或“每个月的第10天10:15”,CronTrigger非常有用。
为什么是工作和触发器?许多作业调度程序没有作业和触发器的独立概念。有些人将“作业”简单地定义为执行时间(或调度)和一些小型作业标识符。其他的类似于Quartz的工作和触发器对象的结合。在开发Quartz时,我们认为在计划和按照计划执行的工作之间创建一个分离是有意义的。这(在我们看来)有许多好处。
例如,作业可以独立于触发器创建和存储在作业调度器中,而且许多触发器可以与相同的作业关联。这种松散耦合的另一个好处是,可以配置在相关触发器过期后仍然保留在调度器中的作业,以便以后可以重新调度它们,而不必重新定义它们。它还允许您修改或替换触发器,而不必重新定义其关联的作业。
2.3 Identities
3. more about job and trigger
使用jobbuilder
import static org.quartz.JobBuilder.*;
上一章节我们简单的定义了一个job
// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
.withIdentity("myJob", "group1") // name "myJob", group "group1"
.build();
// Trigger the job to run now, and then every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("myTrigger", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(40)
.repeatForever())
.build();
// Tell quartz to schedule the job using our trigger
sched.scheduleJob(job, trigger);
来看一下HelloJob的定义
public class HelloJob implements Job {
public HelloJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException
{
System.err.println("Hello! HelloJob is executing.");
}
}
3.1 JobDataMap
JobDataMap 可以用来存储任意大小的数据,(在数据库中存储为Blob格式的数据类型),实现了java map并添加了常用的方法来存储和检索原始类型数据。
// define the job and tie it to our DumbJob class
JobDetail job = newJob(DumbJob.class)
.withIdentity("myJob", "group1") // name "myJob", group "group1"
.usingJobData("jobSays", "Hello World!")
.usingJobData("myFloatValue", 3.141f)
.build();
在job执行期间取出jobDataMap中的数据
public class DumbJob implements Job {
public DumbJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException
{
JobKey key = context.getJobDetail().getKey();
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String jobSays = dataMap.getString("jobSays");
float myFloatValue = dataMap.getFloat("myFloatV