Trigger
Quartz中的触发器,用来告诉调度程序什么时候触发,即Trigger对象是用来触发Job的
触发器通用属性
JobKey StartTime EndTimeJobKey表示job实例的表示.触发器被触发时,该指定的job实例会执行StartTime 表示触发器额时间表首次被触发的时间
他的值的类型是Java.util.DateEndTime
指定触发器的不再被触发的时间他的值的类型是Java.util.Date
trigger通过builder模式创建
package cn.miye.web;import java.text.SimpleDateFormat;import java.util.Date;import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.quartz.JobKey;import org.quartz.Trigger;public class HelloJob implements Job { @Override public void execute(JobExecutionContext context) throws JobExecutionException { // 打印当前时间 Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前时间:" + sf.format(date)); Trigger currentTrigger = context.getTrigger(); System.out.println("任务开始时间:"+sf.format(currentTrigger.getStartTime())); System.out.println("任务结束时间:"+sf.format(currentTrigger.getEndTime())); JobKey jobKey = currentTrigger.getJobKey(); System.out.println("JobKey的分组名称:"+jobKey.getGroup()+"JobKey的名称:"+jobKey.getName()); }}
package cn.miye.web;import java.text.SimpleDateFormat;import java.util.Date;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.SchedulerFactory;import org.quartz.SimpleScheduleBuilder;import org.quartz.Trigger;import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory;public class HelloScheduler { public static void main(String[] args) throws SchedulerException { // 打印当前时间 Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Current Exec Time Is:" + sf.format(date)); //获取距离当前时间3s之后的时间 date.setTime(date.getTime()+3000);//3s //获取距离当前时间9s之后的时间 Date endDate = new Date(); endDate.setTime(endDate.getTime()+9000); // 创建一个JobDetail实例,将该实例与HelloJobClass绑定 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class) .withIdentity("myJob", "group1") .build(); // 创建一个Trigger实例,定义该job立即执行,并每隔两秒钟执行一次,直到程序停止 Trigger trigger = TriggerBuilder.newTrigger() .withIdentity("mytrigger", "group1") .startAt(date)//表示在date的时间开始执行 .endAt(endDate)//表示最后的执行时间 .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(2).repeatForever()).build(); // 创建Scheduler实例 SchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); scheduler.start(); scheduler.scheduleJob(jobDetail, trigger); }}
子类
SimpleTrigger
作用 在一个指定时间段内执行一次作业任务 或在指定的时间间隔内多次执行作业任务package cn.miye.web;import java.text.SimpleDateFormat;import java.util.Date;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 { // 打印当前时间 Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前时间:" + sf.format(date)); System.out.println("Hello Word!"); }}
package cn.miye.web;import java.text.SimpleDateFormat;import java.util.Date;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.SchedulerFactory;import org.quartz.SimpleTrigger;import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory;public class HelloScheduler { public static void main(String[] args) throws SchedulerException { // 打印当前时间 Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Current Exec Time Is:" + sf.format(date)); //获取距离当前时间4s之后的时间 date.setTime(date.getTime()+4000L);//4s // 创建一个JobDetail实例,将该实例与HelloJobClass绑定 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class) .withIdentity("myJob", "group1") .build(); // 创建一个SimpleTrigger实例,在距离当前时间4s中后执行且只执行一次 SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger() .withIdentity("mytrigger", "group1") .startAt(date) .build(); // 创建Scheduler实例 SchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); scheduler.start(); scheduler.scheduleJob(jobDetail, trigger); }}
//创建一个SimpleTrigger实例,在距离当前时间4s中后执行且每个两秒执行一次,执行3次SimpleTrigger trigger = (SimpleTrigger)TriggerBuilder.newTrigger().withIdentity("mytrigger", "group1") .startAt(date) .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(2).withRepeatCount(3)).build();
withRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY)//执行无数次
CronTrigger
作用
基于日历的作业调度器 而不是像Simpletrigger 那样精确指定间隔时间,比SimpleTrigger更常用(每月某天或者每周某天)
基于Cron表达式 Cron表达式 用于配置CronTrigger实例 是由7个子表达式组成的字符串,描述了时间表的详细信息 格式:[秒] [分] [时] [日] [月] [周] [年]* 每
简单例子
package cn.miye.web;import java.text.SimpleDateFormat;import java.util.Date;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 { // 打印当前时间 Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("当前时间:" + sf.format(date)); System.out.println("Hello World"); }}
package cn.miye.web;import java.text.SimpleDateFormat;import java.util.Date;import org.quartz.CronScheduleBuilder;import org.quartz.CronTrigger;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.SchedulerFactory;import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory;public class HelloScheduler { public static void main(String[] args) throws SchedulerException { // 打印当前时间 Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Current Exec Time Is:" + sf.format(date)); // 创建一个JobDetail实例,将该实例与HelloJobClass绑定 JobDetail jobDetail = JobBuilder.newJob(HelloJob.class) .withIdentity("myJob", "group1") .build(); // 每年每周每月每天每时每分秒钟执行一次 CronTrigger trigger = (CronTrigger)TriggerBuilder .newTrigger() .withIdentity("mytrigger", "group1") .withSchedule( CronScheduleBuilder.cronSchedule("* * * * * ? *")) .build(); // 创建Scheduler实例 SchedulerFactory sfact = new StdSchedulerFactory(); Scheduler scheduler = sfact.getScheduler(); scheduler.start(); scheduler.scheduleJob(jobDetail, trigger); }}
在线Cron表达式生成工具http://www.pdtools.net/tools/becron.jsp