一、QuartZ是什么?
二、获取
三、核心接口
- Scheduler - 与调度程序交互的主要API。
- Job - 由希望由调度程序执行的组件实现的接口。
- JobDetail - 用于定义作业的实例。
- Trigger(即触发器) - 定义执行给定作业的计划的组件。
- JobBuilder - 用于定义/构建JobDetail实例,用于定义作业的实例。
- TriggerBuilder - 用于定义/构建触发器实例。
四、触发器Tigger
1、SimpleTrigger
2、CronTrigger
3、Cron Expressions
4、Cron生成表达式工具
百度查
5、入门案例
package com.yiibai.common; 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 { System.out.println("Hello Quartz!"); } }
package com.yiibai.quartz; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import com.yiibai.common.HelloJob; public class SimpleTriggerExample { public static void main(String[] args) throws Exception { //创建job JobDetail job = JobBuilder.newJob(HelloJob.class) .withIdentity("bky_lzw", "group1").build(); //创建调度器 SimpleTrigger trigger = TriggerBuilder.newTrigger() .withIdentity("bky_lzw", "group1") .withSchedule( SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(5) .repeatForever() ) .build(); //schedult执行 Scheduler scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger); } }
package com.yiibai.quartz; import org.quartz.CronScheduleBuilder; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import com.yiibai.common.HelloJob; public class CronTriggerExample { public static void main(String[] args) throws SchedulerException { JobDetail job = JobBuilder.newJob(HelloJob.class) .withIdentity("bky_lzw", "group1").build(); Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("bky_lzw", "group1") .withSchedule( CronScheduleBuilder.cronSchedule("0/5 * * * * ?") ) .build(); Scheduler scheduler = new StdSchedulerFactory().getScheduler(); scheduler.start(); scheduler.scheduleJob(job, trigger); } }