相关概念:
Scheduler - 与调度程序交互的主要API。
Job - 执行的任务需要实现的接口(任务模板)
JobDetail - 用于定义任务属性(任务实例)
Trigger - 用于触发定义任务的如何触发(触发器)
JobBuilder - 用于构建JobDetail实例(常用静态导入)
TriggerBuilder - 用于构建触发器实例(常用静态导入)
所需jar包获取地址
一.创建简单job类(实现org.quartz.Job接口,重写execute方法即可):
二.任务实例创建和调度过程:
相关步骤:
1.获取调度器
2.定义jobDetail实例并与HelloJob关联
3.创建trigger(定义job触发方式)
4.告诉Quartz使用我们定义的触发器来调度job实例
5.启动调度程序(触发器等在调度程序启动后才能真正运行)
详细过程:
告诉Quartz使用我们的触发器来调度job:
scheduler.scheduleJob(job,trigger);
启动调度(触发器等在调度程序启动后才能真正运行):
scheduler.start();
实际调度过程:
运行结果:
调度完整代码:
/**
* Copyright (C), 2015-2018, Nxin
* FileName: SimpleTest1
* Author: xff
* Date: 2018/5/26 11:04
* Description: HelloJob简单调度
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.feifei.test1;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* 使用静态导入维持代码的简洁性
*/
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.DateBuilder.evenMinuteDate;
/**
* 〈一句话功能简述〉<br>
* 〈HelloJob简单调度〉
*
* @author xff
* @create 2018/5/26
* @since 1.0.0
*/
public class SimpleTest1 {
/*
创建调度工厂,并获取调度器
*/
public static Scheduler getScheduler() throws SchedulerException {
SchedulerFactory schedulerFactory=new StdSchedulerFactory();
return schedulerFactory.getScheduler();
}
/**
* 定义任务细节并将其关联到实际的Job类
*/
public static JobDetail defineJobDetailAndTieToRealJobClass(){
JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();
return job;
}
/**
* 定义并获取trigger
* @param runTime 触发开始时间
* @return
*/
public static Trigger getTrigger(Date runTime){
Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
return trigger;
}
public void run() throws SchedulerException {
Logger logger= LoggerFactory.getLogger(SimpleTest1.class);
//1.获取调度器
Scheduler scheduler=getScheduler();
//2.定义job并与实际jobClass关联
JobDetail job= defineJobDetailAndTieToRealJobClass();
//3.创建Trigger
Date runTime=evenMinuteDate(new Date());
Trigger trigger=getTrigger(runTime);
//4.告诉Quartz使用我们的触发器来调度job
scheduler.scheduleJob(job,trigger);
//5.启动调度
scheduler.start();
try {
logger.info("睡眠65s等待任务执行...");
TimeUnit.SECONDS.sleep(75);
} catch (InterruptedException e) {
logger.error(e.getMessage());
}
}
public static void main(String[] args) throws SchedulerException {
new SimpleTest1().run();
}
}
HelloJob完整代码:
/**
* Copyright (C), 2015-2018, Nxin
* FileName: HelloJob
* Author: xff
* Date: 2018/5/26 11:00
* Description: 一个输出helllo world的简单job
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.feifei.test1;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 〈一句话功能简述〉<br>
* 〈一个输出helllo world的简单job〉
*
* @author xff
* @create 2018/5/26
* @since 1.0.0
*/
public class HelloJob implements Job{
private static Logger logger = LoggerFactory.getLogger(HelloJob.class);
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
logger.info("hello world!!!");
}
}
未完待续...