Trigger的触发器通用属性:JobKey——job实例的标识,触发器被触发时,该指定的job实例会执行;StartTime——表示触发器的时间表首次被触发的时间,它的值的类型是Java.util.Date;EndTime——指定触发器不再被触发的时间,它的值的类型是Java.util.Date;Job示例代码如下:
package com.luna.timer.quarts;
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 startTime = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("任务执行时间为:"+sf.format(startTime));
Trigger currentTrigger = context.getTrigger();
System.out.println("Start Time is:"+sf.format(currentTrigger.getStartTime()));
System.out.println("End Time is:"+sf.format(currentTrigger.getEndTime()));
JobKey jobKey = currentTrigger.getJobKey();
System.out.println("JobKey info---"+"jobName:"+jobKey.getName()+"jobGroup:"+jobKey.getGroup());
}
}
Trigger示例代码如下:
package com.luna.timer.quarts;
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 {
// 创建一个JobDetail实例与HelloJob类绑定
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build();
//打印当前时间
Date startTime = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("打印当前时间:"+sf.format(startTime));
//获取当前时间3秒后的时间
startTime.setTime(startTime.getTime()+3000);
//获取当前时间6秒后的时间
Date endDate = new Date();
endDate.setTime(endDate.getTime()+6000);
// 创建一个Trigger实例,定义该Job立即执行,且每隔两秒钟执行一次直到永远
Trigger trigger = TriggerBuilder.newTrigger().
withIdentity("mtTrigger", "group1").
startAt(startTime).endAt(endDate).
withSchedule(SimpleScheduleBuilder.simpleSchedule().
withIntervalInSeconds(2).repeatForever()).build();
//创建scheduler实例
SchedulerFactory sFactory = new StdSchedulerFactory();
Scheduler scheduler = sFactory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail,trigger);
}
}
SimpleTrigger:在一个指定时间段内执行一次作业任务,或者是在指定的时间间隔内多次执行作业任务。创建定时任务:距离当前时间4秒之后执行且仅执行一次,代码如下:
package com.luna.timer.quarts;
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 startTime = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("任务执行时间为:"+sf.format(startTime));
System.out.println("Hello World!");
}
}
package com.luna.timer.quarts;
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 {
// 创建一个JobDetail实例与HelloJob类绑定
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build();
//打印当前时间
Date startTime = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("打印当前时间:"+sf.format(startTime));
//获取距离当前时间4秒之后的时间
startTime.setTime(startTime.getTime()+4000L);
// 创建一个Trigger实例,距离当前时间4秒钟后执行且仅执行一次任务
SimpleTrigger trigger = (SimpleTrigger)TriggerBuilder.newTrigger().
withIdentity("mtTrigger", "group1").startAt(startTime).build();
//创建scheduler实例
SchedulerFactory sFactory = new StdSchedulerFactory();
Scheduler scheduler = sFactory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail,trigger);
}
}
创建定时任务:距离当前时间4秒之后执行之后每隔两秒执行一次,距离当前时间6秒之后停止执行,代码如下:
package com.luna.timer.quarts;
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.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class HelloScheduler {
public static void main(String[] args) throws SchedulerException {
// 创建一个JobDetail实例与HelloJob类绑定
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build();
//打印当前时间
Date startTime = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("打印当前时间:"+sf.format(startTime));
//获取距离当前时间4秒之后的时间
startTime.setTime(startTime.getTime()+4000L);
// 创建一个Trigger实例,距离当前时间4秒钟后执行任务,之后每隔两秒执行一次任务
//withRepeatCount方法参数:SimpleTrigger.REPEAT_INDEFINITELY永远执行下去,若设置为具体数字则就执行几次
SimpleTrigger trigger = (SimpleTrigger)TriggerBuilder.newTrigger().
withIdentity("mtTrigger", "group1").startAt(startTime).
withSchedule(SimpleScheduleBuilder.simpleSchedule().
withIntervalInSeconds(2).withRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY)).build();
//创建scheduler实例
SchedulerFactory sFactory = new StdSchedulerFactory();
Scheduler scheduler = sFactory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail,trigger);
}
}
创建定时任务:距离当前时间4秒之后执行之后每隔两秒执行一次,永远执行下去,代码如下:
package com.luna.timer.quarts;
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.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class HelloScheduler {
public static void main(String[] args) throws SchedulerException {
// 创建一个JobDetail实例与HelloJob类绑定
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build();
//打印当前时间
Date startTime = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("打印当前时间:"+sf.format(startTime));
//获取距离当前时间4秒之后的时间
startTime.setTime(startTime.getTime()+4000L);
//获取6秒之后的时间
Date endDate = new Date();
endDate.setTime(endDate.getTime()+6000L);
/**
* 创建一个Trigger实例,距离当前时间4秒钟后执行任务,之后每隔两秒执行一次任务,距离当前6秒之后停止执行
* withRepeatCount方法参数:SimpleTrigger.REPEAT_INDEFINITELY永远执行下去,若设置为具体数字则就执行几次
* 任务只执行两次,由此可见endDate条件是优先于withRepeatCount
*/
SimpleTrigger trigger = (SimpleTrigger)TriggerBuilder.newTrigger().
withIdentity("mtTrigger", "group1").startAt(startTime).endAt(endDate).
withSchedule(SimpleScheduleBuilder.simpleSchedule().
withIntervalInSeconds(2).withRepeatCount(5)).build();
//创建scheduler实例
SchedulerFactory sFactory = new StdSchedulerFactory();
Scheduler scheduler = sFactory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail,trigger);
}
}
注意:重复次数(withRepeatCount)可以为0、正整数或者是SimpleTrigger.REPEAT_INDEFINITELY常量值;重复的执行间隔必须为0或者长整数;一旦被制定了endTime参数,那么它会覆盖重复次数参数的效果。
CornTrigger:基于日历的作业调度器,而不是像SimpleTrigger那样精确指定间隔时间,比SimpleTrigger更常用。基于Cron表达式,用于配置CronTrigger实例,是由7个子表达式组成的字符串,描述了时间表的详细信息。格式: [秒] [分] [小时][日] [月] [周] [年],示例代码如下:
package com.luna.timer.quarts;
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 {
// 创建一个JobDetail实例与HelloJob类绑定
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build();
//打印当前时间
Date startTime = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("打印当前时间:"+sf.format(startTime));
//每秒钟触发一次任务
CronTrigger trigger = (CronTrigger)TriggerBuilder.newTrigger().
withIdentity("mtTrigger", "group1").
withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ? *")).
build();
//创建scheduler实例
SchedulerFactory sFactory = new StdSchedulerFactory();
Scheduler scheduler = sFactory.getScheduler();
scheduler.start();
scheduler.scheduleJob(jobDetail,trigger);
}
}
常用Corn表达式:
Scheduler——工厂模式:所有的Scheduler实例应该由SchedulerFactory来创建,一般包含:StdSchedulerFactory、DirectSchedulerFactory(参数信息需要在代码中维护故不常用)。StdSchedulerFactory使用一组参数来创建和初始化Quartz调度器,配置参数一般存储在quartz.properties文件中,调用getScheduler方法就能创建和初始化调度器对象。Scheduler的主要函数:Data scheduleJob(JobDetail jobDetail,Trigger trigger);返回最近一次任务将要执行的时间,代码如下:
package com.luna.timer.quarts;
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 {
// 创建一个JobDetail实例与HelloJob类绑定
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build();
//打印当前时间
Date startTime = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("打印当前时间:"+sf.format(startTime));
//每天10点15分触发:0 15 10 ? * *
//每天下午的2点到2点59分执行(整点开始,每隔5分钟触发):0 0/5 14 * * ?
//从周一到周五的每天上午10点15分触发:0 15 10 ? * MON-FRI
//每月第三周的星期五(6#3:6代表星期五,#代表第)10点15分开始触发:0 15 10 ? 6#3
//从2016年到2017年每月最后一周的星期五10点15分触发:0 15 10 ? * 6L 2016-2017
CronTrigger trigger = (CronTrigger)TriggerBuilder.newTrigger().
withIdentity("mtTrigger", "group1").
withSchedule(CronScheduleBuilder.cronSchedule("0 15 10 ? * *")).
build();
//创建scheduler实例
SchedulerFactory sFactory = new StdSchedulerFactory();
Scheduler scheduler = sFactory.getScheduler();
scheduler.start();
System.out.println("Schedule time is:"+sf.format(scheduler.scheduleJob(jobDetail,trigger)));
}
}
void start();——启动Scheduler;void standby();——将Scheduler暂时挂起,可以用start()继续执行任务;void shutDown()关闭Scheduler且不能被重启;示例代码如下:
package com.luna.timer.quarts;
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, InterruptedException {
// 创建一个JobDetail实例与HelloJob类绑定
JobDetail jobDetail = JobBuilder.newJob(HelloJob.class).withIdentity("myJob", "group1").build();
//打印当前时间
Date startTime = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("打印当前时间:"+sf.format(startTime));
//每秒钟执行一次,Scheduler执行两秒之后挂起,Scheduler挂起3秒之后重新启动
CronTrigger trigger = (CronTrigger)TriggerBuilder.newTrigger().
withIdentity("mtTrigger", "group1").
withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?")).
build();
//创建scheduler实例
SchedulerFactory sFactory = new StdSchedulerFactory();
Scheduler scheduler = sFactory.getScheduler();
scheduler.start();
System.out.println("Schedule time is:"+sf.format(scheduler.scheduleJob(jobDetail,trigger)));
Thread.sleep(2000L);
scheduler.standby();
Thread.sleep(3000L);
scheduler.start();
/**
* shutDown()可以传入一个Boolean类型的参数,若参数为true:表示等待所有正在执行的job执行完毕之后,再关闭scheduler
* 若参数为false,即和没有传入参数一样,表示直接关闭scheduler,不管是否有Job正在运行
*/
scheduler.shutdown(); //关闭之后不可以重新开启
Thread.sleep(5000L);
scheduler.start();//调用重新开启抛出异常:The Scheduler cannot be restarted after shutdown() has been called.
}
}
线程池属性:threadCount——工作者线程的数目,最少为1,最大不超过100;threadPriority——设置线程池线程的优先级,最小值为1,最大值为10,默认值为5;org.quartz.threadPool.class——org.quartz.simple.SimpleThreadPool;作业存储设置——描述了在调度器实例的生命周期中,job和trigger信息是如何被存储的;插件配置——满足特定需求用到的Quartz插件的配置。