Quartz组件学习记录
使用Quartz版本为2.2.3主要是结合Quartz官方给出的example学习Quartz的使用,理解其中的功能。同时结合网上的资料研究一下,方便日后平台使用。具体Quartz是干什么的,google和度娘上一箩筐。Quartz简单的使用都可以直接看example这里主要记录下Quartz的功能= = 可能会用到的Maven org.quartz-sche
使用Quartz版本为2.2.3
主要是结合Quartz官方给出的example学习Quartz的使用,理解其中的功能。同时结合网上的资料研究一下,方便日后平台使用。
具体Quartz是干什么的,google和度娘上一箩筐。
Quartz简单的使用都可以直接看example
这里主要记录下Quartz的功能= = 可能会用到的
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>
使用
简单添加一个计划任务
// First we must get areference to a scheduler
SchedulerFactory sf = newStdSchedulerFactory();
Scheduler sched = sf.getScheduler();
//computer a time that is on the next round minute
Date runTime = evenMinuteDate(new Date());
// definethe job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class).withIdentity("job1","group1").build();
// Triggerthe job to run on the next round minute
Trigger trigger = newTrigger().withIdentity("trigger1","group1").startAt(runTime).build();
// Tellquartz to schedule the job using our trigger
sched.scheduleJob(job, trigger);
// Startup the scheduler (nothing can actually run until the
//scheduler has been started)
sched.start();
1. 循环执行或者特殊时间执行
TriggerBuilder.withSchedule cronSchedule 匹配时刻
simpleSchedule 时间段
注意:
tartAt endAt 当endAt时间在stattAt时间之前时,会执行一次job
2. 同一个job可以有多个trigger
一个trigger只能控制一个job
当一个job已经被添加到 schedule里面后,不能重复添加。如果需要多个trigger,后面的trigger使用forJob(job)
然后启动:sched.scheduleJob(trigger);
3. misfire(需要设置quartz.properties里面的org.quartz.jobStore.misfireThreshold:5000属性)
example5
without repeating
withMisfireHandlingInstructionFireNow(失效之后再恢复并马上执行)
withMisfireHandlingInstructionNextWithRemainingCount(失效之后不处理)
repeating fixed number oftimes 循环执行7次
withMisfireHandlingInstructionFireNow(失效之后,再启动马上执行,总次数还是7次)
如果任务在10点的时候没有触发,但是在10:15分的时候执行了misfire,以后每次正点执行的时间就会较初始值晚15分钟,直到16:15
withMisfireHandlingInstructionNowWithExistingCount(失效之后,再启动之后马上执行,但是起始次数清零,总次数=7+当前misfire执行次数-1)
如果任务在10点没有触发,10:15分执行了misfire,以后执行时间都会较初始值晚15分钟,但是执行次数还是原来的7次,就会到17:15了。
withMisfireHandlingInstructionNextWithRemainingCount(失效之后,不管donothing,总次数还是7次)
withMisfireHandlingInstructionNextWithExistingCount(失效之后,donothgin,总次数=misfire的次数+7)
repeating infinitely
withMisfireHandlingInstructionFireNow
withMisfireHandlingInstructionNowWithRemainingCount
withMisfireHandlingInstructionNowWithExistingCount(每次失效之后,在下个失效节点再执行,直接执行)
withMisfireHandlingInstructionNextWithExistingCount
withMisfireHandlingInstructionNextWithRemainingCount(每次失效之后,在下个定义的时间点再执行,等循环时间 ,有一个合并的效果在里面)
CRON triggers
withMisfireHandlingInstructionIgnoreMisfires(所有misfire的任务会马上执行)
打个比方,如果9点misfire了,在10:15系统恢复之后,9点,10点的misfire会马上执行
withMisfireHandlingInstructionDoNothing(所有的misfire不管,执行下一个周期的任务)
withMisfireHandlingInstructionFireAndProceed(会合并部分的misfire,正常执行下一个周期的任务)
假设9,10的任务都misfire了,系统在10:15分起来了。只会执行一次misfire,下次正点执行。
4.关闭定时任务
schedule.shutdown(flag)
flag为 ture时, 正在运行的job会继续执行完毕,然后释放资源。
flag为 false时,立即结束。
清空
schedule.clear();
删除特定的trigger(已经在执行的job,将继续执行)
sched.unscheduleJob(trigger.getKey());
删除特定的job,并且删除关联的trigger
deleteJob(String jobName,String groupName)
5.时间生成
futureDate(int1,int2) 在当前的时间上,增加时间int1 标识符(int1是指 ss mm D M Y.....)int2
dateOf(0,0, 10, 31, 10); int hour, int minute,int second, int dayOfMonth, int month
evenMinuteDate(newDate()) 下一分钟
DateBuilder.nextGivenSecondDate(null,15) 下一个15秒的倍数 以60秒为基础 0----59 没有就为00
6.设置优先级
withPriority
example14
// Third trigger has priority 10, and willrepeat after 15 seconds
Trigger trigger3 =newTrigger()
.withIdentity("Priority10Trigger15SecondRepeat")
.startAt(startTime)
.withSchedule(simpleSchedule().withRepeatCount(1).withIntervalInSeconds(15))
.withPriority(10)
.forJob(job)
.build();
7.JobListener设置监听器
example9
// Set up the listener
JobListener listener = new Job1Listener();
Matcher<JobKey> matcher =KeyMatcher.keyEquals(job.getKey());
sched.getListenerManager().addJobListener(listener, matcher);
8.停止正在执行的job
example7
sched.interrupt(job.getKey());
job需要实现InterruptableJob接口
9.过滤掉不计算在内的日子
example8
// Add the holiday calendar to the schedule
AnnualCalendar holidays = newAnnualCalendar();
// fourth of July (July 4)
Calendar fourthOfJuly = newGregorianCalendar(2005, 6, 4); 注意这里的月份从0开始计数
holidays.setDayExcluded(fourthOfJuly,true);
// tell the schedule about our holidaycalendar
sched.addCalendar("holidays",holidays, false, false);
或者
/ ③国庆节
Calendar nationalDay = newGregorianCalendar();
nationalDay.add(Calendar.MONTH,10);
nationalDay.add(Calendar.DATE, 1);
holidays.setDayExcluded(fourthOfJuly,true);
10.异常的处理
example6
JobExecutionException e2 = new JobExecutionException(e);
// Quartz will automaticallyunschedule
// all triggers associated withthis job
// so that it does not run again
e2.setUnscheduleAllTriggers(true); 立即停止所有相关这个任务的触发器
// this job will refire immediately
e2.setRefireImmediately(true); 立即重新执行任务
11.job任务数据传输
put:
job1.getJobDataMap().put(ColorJob.FAVORITE_COLOR,"Green");
get:
StringfavoriteColor = data.getString(FAVORITE_COLOR);
JobBuilder.usingJobData(newJobDataMap)
12.注解
@PersistJobDataAfterExecution
此标记说明在执行完Job的execution方法后保存JobDataMap当中固定数据,在默认情况下 也就是没有设置 @PersistJobDataAfterExecution的时候 每个job都拥有独立JobDataMap
@DisallowConcurrentExecution
此标记用在实现Job的类上面,意思是不允许并发执行
这个标志位是指 同一个JobDetail下的同步.
也就是说:
//通过过JobDetail封装ColorJob,同时指定Job在Scheduler中所属组及名称,这里,组名为group1,而名称为job1。
JobDetail job1 = newJob(ColorJob.class)
.withIdentity("job1","group1")
.build();
//第二个任务
JobDetail job2 = newJob(ColorJob.class)
.withIdentity("job2","group1")
.build();
即使ColorJob里面加了注解,job2和job1之间也不会同步。
13. requestsRecovery(不知道有什么用)
JobDetail类中有requestsRecovery属性,默认是false,当设置为true时,重新提交之后Quartz会检测当前的任务时候错过了应该触发的时间,如果错过了会立即触发一个任务。
14.持久化
在执行任务之前需要先检查是否已经执行,但并未完成
boolean flag = sched.checkExists(trigger1.getKey());
Date scheduleTime1 = new Date();
if(!flag){
//将参数传递入任务的数据Map中
job1.getJobDataMap().put(ColorJob.FAVORITE_COLOR,"Green");
job1.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1);
//注册并进行调度
scheduleTime1 =sched.scheduleJob(job1,trigger1);
}else{
//scheduleTime1= sched.scheduleJob(trigger1);
sched.resumeJob(job1.getKey());
}
在src目录下配置 quartz.properties
15.使用指定配置文件
SchedulerFactory schedulerFactory =new StdSchedulerFactory("org/quartz/examples/example14/quartz_priority.properties");
网站
http://www.cronmaker.com/ 生成和解析cron表达式
http://www.boyunjian.com/javadoc/org.apache.servicemix.bundles/org.apache.servicemix.bundles.quartz/2.2.1_1/_/org/quartz/package-summary.html 在线文档
http://student-lp.iteye.com/blog/2093395 配置信息记录数据库(默认是在RAM里面,程序crash之后,信息全部不存在)
http://blog.csdn.net/u010397369/article/details/17465649 配置Spring支持之类的(可能看起来不太灵活)
点击下载文中涉及到的Quartz的小例子
更多推荐
所有评论(0)