quartz
- 下载 http://www.quartz-scheduler.org/downloads/
- 导入 lib 或 maven 依赖
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
-
核心接口
a. scheduler 调度器
b. Job 任务
c. trigger 触发器
d. JobDetail 任务信息 -
trigger 触发器
a. SimpleTrigger – 功能简单 指定时间、周期
b. CronTrigger – 基于 cron 表达式触发器cron 表达式 : 秒 分 时 日 月 周 年
package asiainfo.quartz;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
/**
* @program: quartz
* @description: ${description}
* @author: Elk love madness
* @create: 2018-09-17 10:04
*/
public class QuartzTest {
public static void main(String[] args) throws Exception{
// 获取调度器器
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// 包装任务内容
JobDetail job = newJob(MyJob.class)
.withIdentity("job1", "group1")
.build();
// 定义触发器器
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(1)
.repeatForever())
.build();
// 组装任务
scheduler.scheduleJob(job, trigger);
// 启动调度器器 开始调度
scheduler.start();
}
}
package asiainfo.quartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;
/**
* @program: quartz
* @description: ${description}
* @author: Elk love madness
* @create: 2018-09-17 10:03
*/
public class MyJob implements Job {
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("当前系统时间:"+new Date());
}
}