Quartz框架是Java开源的定时任务调度器,Quartz框架中有如下核心概念:
1. Job
任务接口,接口中只声明方法void execute(JobExecutionContext context),接口的声明如下:
public interface Job { void execute(JobExecutionContext context) throws JobExecutionException; }
2. JobDetail
Quartz执行Job时,需要重新创建新Job实例,所以Quartz不可直接接受Job的实例,相反它接收一个Job实现类以便运行时通过Java反射机制实例化Job。因此需要通过一个类来描述Job的实现类及其它相关的静态信息,如Job名字、描述、关联监听器等信息, JobDetail封装了这些信息。
3. Trigger
Quartz框架任务执行的触发器,在Quartz框架中提供了四类任务触发器:SimpleTrigger、CronTrigger、DailyTimeIntervalTrigger、CalendarIntervalTrigger
在Qunar框架中,JobDetail的对象是通过JobBuilder构建,Trigger对象时通过TriggerBuilder构建,下面演示一个简单的示例:
public class SchedulerDemo { private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static SchedulerFactory schedulerFactory ; static { try { Properties props = new Properties(); props.load(SchedulerDemo.class.getResourceAsStream("quartz.properties")); schedulerFactory = new StdSchedulerFactory(props); } catch (Exception e) { System.out.println("[StdSchedulerFactory] init error"); } } public static class HelloQuartzJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { System.out.println("Hello Quartz! - executing its Job at [" + SDF.format(new Date()) + "] by ["
+ context.getTrigger().getDescription() + "]"); } } /** * JobDetail的构造通过JobBuilder构建,JobBuilder在内部调用JobDetail的实现类JobDetailImpl * */ public void run(Trigger trigger) throws SchedulerException { Scheduler scheduler = schedulerFactory.getScheduler(); JobDetail jobDetail = JobBuilder.newJob(HelloQuartzJob.class) .withIdentity("helloQuartz", "demo") .build(); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); } /** * Trigger的构造通过TriggerBuilder创建 * */ public Trigger getSimpleTrigger() { SimpleTrigger simpleTrigger = (SimpleTrigger) TriggerBuilder.newTrigger() .withIdentity("simpleTrigger", "demo") .withDescription("SimpleTrigger") .startNow() .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10) .withRepeatCount(5)) .build(); return simpleTrigger; } /** * CornExpress允许的字符 * 秒 0-59 , - * / * 分 0-59 , - * / * 小时 0-23 , - * / * 日期 1-31 , - * ? / L W C * 月份 1-12 或者 JAN-DEC , - * / * 星期 1-7或者 SUN-SAT , - * ? / L C # * 年(可选) 留空, 1970-2099 , - * / * 特殊字符 意义 * * 表示所有值 * ? 表示未说明的值,即不关心它为何值 * - 表示一个指定的范围 * , 表示附加一个可能值 * / 符号前表示开始时间,符号后表示每次递增的值; * 举例: * "0 0 12 * * ?" 每天中午12点触发 * "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 * "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 * */ public Trigger getCronTrigger() { CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger() .withIdentity("cronTrigger", "demo") .withDescription("CronTrigger") .startNow() .withSchedule(CronScheduleBuilder.cronSchedule("0 17 17 * * ? *")) .build(); return trigger; } public Trigger getDailyTimeIntervalTrigger() { DailyTimeIntervalTrigger trigger = (DailyTimeIntervalTrigger) TriggerBuilder.newTrigger() .withIdentity("dailyTimeIntervalTrigger", "demo") .withDescription("dailyTimeIntervalTrigger") .startNow() .withSchedule(DailyTimeIntervalScheduleBuilder.dailyTimeIntervalSchedule() .onEveryDay() .startingDailyAt(new TimeOfDay(17,32,10)) .withInterval(10, IntervalUnit.SECOND) .withRepeatCount(1)) .build(); return trigger; } public Trigger getCalendarIntervalTrigger() { CalendarIntervalTrigger trigger = TriggerBuilder.newTrigger() .withIdentity("CalendarIntervalTrigger", "demo") .withDescription("CalendarIntervalTrigger") .startNow() .withSchedule(CalendarIntervalScheduleBuilder.calendarIntervalSchedule() .withInterval(10,IntervalUnit.SECOND)) .build(); return trigger; } public static void main(String []args) throws SchedulerException { SchedulerDemo schedulerDemo = new SchedulerDemo(); // schedulerDemo.run(schedulerDemo.getSimpleTrigger()); // schedulerDemo.run(schedulerDemo.getCronTrigger()); // schedulerDemo.run(schedulerDemo.getDailyTimeIntervalTrigger()); schedulerDemo.run(schedulerDemo.getCalendarIntervalTrigger()); } }