一.例子
1.下载Quartz
File:quartz.properties:
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
2.Quartz作业:
import org,quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job{
public void execute(JobExecutionContext context) throws JobExecutionException{
System.out.println("Hello Quartz!");
}
}
3.Quartz 触发器{两种:①SimpleTrigger(允许设置开始时间,结束时间,重复间隔)②CronTrigger(允许UNIX cron表达式来指定日期和时间来运行作业)}
SimpleTrigger --每5秒运行
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName","group1")
.withScheduler(
simpleSchedulerBuilder.simpleSchedule()
.withIntervalInSeconds(5).repeatForver())
.build();
CronTrigger --每5秒运行
Trigger trigger = TriggerBuilder
.newTrigger()
.withIndentity("dummyTriggerName","group1")
.withScheduler(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();
4.Scheduler
调度类链接"工作"和"触发器"到一起,并执行它。
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job,trigger);
完整例子
public class TriggerExample{
public static void main(String[] args) throws Exception {
JobDetail job = JobBuilder.newJob(Hello.class)
.withIdentity("dummyJobName","group1").build();
//simpleTrigger
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName","group1")
.withScheduler(
simpleScheduleBuiler.simpleSchedule()
.withIntervalInSeconds(5).repeatForever())
.build();
//cronTrigger
/**
*Trigger trigger = new TriggerBuilder()
* .newTrigger()
* .withIdentity("dummyTriggerName","group1")
* .withScheduler(
* CronSchedulerBuilder().cronSchedule("0/5 * * * * ?"))
* .build();
*/
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
Scheduler.start();
Scheduler.scheduleJob(job,trigger);
}
}
二.Quartz作业监听
1.Quartz作业
public class HelloJob implements Job{
public void execute(JobExecutionContext context) throws JobExecutionException{
//作业--打印信息并抛出一个JobExcecutionException测试
System.out.println("Hello Gaza");
throw new JobExecutionException("Testiing Exception");
}
}
2.JobListener
创建JobListener,只是实现JobListener接口,并覆盖所有的接口方法。
public class HelloJobListener implements JobListener{
public static final String LISTENER_NAME = "dummyJobListenerName";
public String getName(){
return LISTENER_NAME;
}
public void JobToBeExecuted(JobExecutionContext context){
String jobName = context.getJobDetail().getKey().toString();
System.out.println("JobToBeExecuted");
System.out.println("Job:"+jobName+"is going to start...");
}
public void jobExecutionVetoed(JobExecutionContext context){
System.out,println("jobExecutionVetoed");
}
public void jobWasExecuted(JobExecutionContext context,JobExecutionException jobException) {
System.out.println("jobExecuted");
String jobName = context.getJobDetail().getKey().toString();
System.out.println("Job:"+jobName+"is finished...");
if(!jobException.getMessage().equals("")){
System.out.println("Exception thrown by:"+jobName+"Exception:" +jobException.getMessage()):
}
}
}
3.CronTrigger
HelloJobListener连接调度和监控作业的状态。
public class CronTriggerExample{
public static void main(String[] args)throws Exception{
JobKey jobKey = new JobKey("dummyJobName","group1");
JobDetail job = JobBuilder.newJob(HelloJob.class)
.withIdentity(jobKey).build();
Trigger trigger = TriggerBuilder
.newTrigger()
.withIndentity(JobKey)
.withSchedule(CronTriggerBuilder.cronSchedule("0/5 * * * * ?"))
.build();
Scheduler scheduler = new SchedulerFactory().getScheduler();
scheduler.getListenerManager().addJobListener(
new HelloJobListener(),KeyMatcher.keyEquals(jobKey)
);
scheduler.start();
scheduler.scheduleJob(job,trigger);
}
}
运行CronTriggerExample.java 输出结果:
jobToBeExecuted
Job : group1.dummyJobName is going to start...
Hello Gaza
jobWasExecuted
Job : group1.dummyJobName is started and finished...
Exception thrown by: group1.dummyJobName Exception: Testing Exception
jobToBeExecuted
Job : group1.dummyJobName is going to start...
Hello Gaza
jobWasExecuted
Job : group1.dummyJobName is started and finished...
Exception thrown by: group1.dummyJobName Exception: Testing Exception
三.Quartz执行多作业
通过Quartz API 多个作业,每个作业将被连接到一个唯一的触发,并且由调度器运行它。
PS:在Quartz中,一个触发器触发多个作业是不可取的。
1.Quartz APIs
创建3个作业,JobA,JobB,JobC。
public class JobA implements Job{
public void execute(JobExecutionContext context) throws JobExecutionException{
System.out.println("JobA is runing//every 5 seconds");
}
}
public class JobB implements Job{
public void execute(JobExecutionContext context) throws JobExecutionException{
System.out.println("JobB is runing");
}
}
public class JobC implements Job{
public void execute(JobExecutionContext context) throws JobExecutionException{
System.out.println("JobC is runing");
}
}
使用QuartzAPI 声明上述3个作业,分配他们到特定的触发器并调度它。
public class CronTrigger{
public static void main(String[] args) throws Exception{
JobKey jobKeyA = new JobKey("jobA","group1");
JobDetail jobA = JobBuilder.newJob(JobA.class)
.withIdentity(jobKeyA).build();
Jobkey jobKeyB = new JobKey("jobB","group1");
JobDetail jobA = JobBuilder.newJob(JobB.class)
.withIdentity(jobKeyB).build();
JobKey jobKeyC = new JobKey("jobC","group1");
JobDetail jobA = JobBuilder.newJob(jobC.class)
.withIdentity(jobKeyC).build();
Trigger trigger1 = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName1","group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();
Trigger trigger2 = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName2","group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();
Trigger trigger3 = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName3","group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.build();
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(jobA,trigger1);
scheduler.scheduleJob(jobB,trigger2);
scheduler.scheduleJob(jobC,trigger3);
}
}
输出结果如上:
Job A is runing //every 5 seconds
Job B is runing
Job C is runing
Job A is runing //every 5 seconds
Job B is runing
Job C is runing
四、Quartz列出调度器所有任务
Quartz 2.21示例
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
for(String groupName : scheduler.getJobGroupNames()){
for(JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))){
String jobName = jobKey.getName();
String jobGroup = jobKey.getGroup();
List<Trigger> triggers = (List<Trigger>) shceduler.getTriggersOfJob(jobKey);
Date nextFireTime = triggers.get(0).getNextFireTime();
System.out.println("[jobName]:"+jobName+" [groupName]:"+groupName+" - " +nextFireTime);
}
}