Quartz2.1.7学习笔记_第1页
Quartz2.1.7学习笔记_第2页
Quartz2.1.7学习笔记_第3页
Quartz2.1.7学习笔记_第4页
Quartz2.1.7学习笔记_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Quartz 2.1.7学习笔记 2015年3月10日星期二1. Quartz简介 Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中。它提供了巨大的灵活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。它有很多特征,如:数据库支持,集群,插件,EJB作业预构建,JavaMail及其它,支持cron-like表达式等等。2. 下载说明3. 如何删除一个job1.1. 获取运行job的jobKey或者自己声明一个JobKey job2_key = job2.getKey();或者:JobKey job2_key = new JobKey(job2, group2);1.2. 删除代码 / 删除job2JobKey job2_key = new JobKey(job2, group2);scheduler.deleteJob(job2_key);1. 触发器1.2. 简单触发器1.3. Cron触发器1.1.1 简介CronTrigger 支持比 SimpleTrigger 更具体的调度,而且也不是很复杂。基于 cron 表达式,CronTrigger 支持类似日历的重复间隔,而不是单一的时间间隔 这相对 SimpleTrigger 而言是一大改进。 Cron 表达式包括以下 7 个字段: 字段名允许的值特殊的值秒0-59, - * /分0-59, - * /时0-23, - * /日1-31, - * / ? L W月1-12 或JAN-DEC, - * / 周1-7 或SUN-SAT, - * / ? L #年(可选)空 或 1970-2199, - * /说明: The * character is used to specify all values. For example, * in the minute field means every minute.翻译:星号“*”代表所有的值。例如,用在分钟字段中,代表所有“每一分”。 The ? character is allowed for the day-of-month and day-of-week fields. It is used to specify no specific value. This is useful when you need to specify something in one of the two fields, but not the other.翻译:问号?只能用在日和周的字段中,用来指定“没有特定的值”。当你要指定两个字段中的一个字段为特殊日期,而另外一个不是,特别有用。注:日和周的字段必须有一个为?,不然会出现异常。CronExpression 0/20 * * * * * is invalid,.Caused by: java.text.ParseException: Support for specifying both a day-of-week AND a day-of-month parameter is not implemented. The - character is used to specify ranges. For example 10-12 in the hour field means the hours 10, 11 and 12.翻译:横-表示范围。 例如,在小时字段中”10-12”表示”10,11和12”3个时间。The , character is used to specify additional values. For example MON,WED,FRI in the day-of-week field means the days Monday, Wednesday, and Friday.翻译:字符,用来列举值。例如,在周字段中”MON,WED,FRI”表示”星期一,星期三,星期五”三天。The / character is used to specify increments. For example 0/15 in the seconds field means the seconds 0, 15, 30, and 45. And 5/15 in the seconds field means the seconds 5, 20, 35, and 50. Specifying * before the / is equivalent to specifying 0 is the value to start with. Essentially, for each field in the expression, there is a set of numbers that can be turned on or off. For seconds and minutes, the numbers range from 0 to 59. For hours 0 to 23, for days of the month 0 to 31, and for months 1 to 12. The / character simply helps you turn on every nth value in the given set. Thus 7/6 in the month field only turns on month 7, it does NOT mean every 6th month, please note that subtlety.翻译:斜杠/用来表示递增值。例如,在秒字段中”0/15”表示”0秒,15秒,30秒,45秒”。又,在秒字段中”5/15”表示”5秒,20秒,35秒,50秒”。在”/”前使用”*”和使用”0”的效果一样。需要注意的是,在每一个字段中,值的范围可能超出。对于秒和分钟,值的范围是0到59,小时范围是0到23,天是0到31(估计这个地方英文有错),月是1到12。”/”能很简单第指定”第几”值。例如在月中指定”7/6”表示7月,而不是每6个月,请注意这一点。注: 关于最后一点再记一点,例如 40/30 * * * * ?运行的结果:SimpleJob says: group1.job1 executing at Tue Mar 10 10:59:40 CST 2015 SimpleJob says: group1.job1 executing at Tue Mar 10 11:00:40 CST 2015SimpleJob says: group1.job1 executing at Tue Mar 10 11:01:40 CST 2015对于这一点写法最好写成 “n/范围最大值”,例如”40/59”,当然也可以直接写个“40”,这样最简单。The L character is allowed for the day-of-month and day-of-week fields. This character is short-hand for last, but it has different meaning in each of the two fields. For example, the value L in the day-of-month field means the last day of the month - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means 7 or SAT. But if used in the day-of-week field after another value, it means the last xxx day of the month - for example 6L means the last friday of the month. You can also specify an offset from the last day of the month, such as L-3 which would mean the third-to-last day of the calendar month.When using the L option, it is important not to specify lists, or ranges of values, as youll get confusing/unexpected results.翻译:字符”L”只能用在日和周字段中。该字符是”last”的简写,但是在这两个字段中表示的意思是不一样的。例如,在日字段中表示月的最后一天-1月是31日,2月是28日(不是闰年)。如果在周字段中单独用“L”的话,仅仅表示“7”或者“星期六”。如果在周字段中和别的值一起用,表示“一个月中最后一个星期几”-例如“6L”表示“一个月中最后一个星期五”。在日字段中叶可以用来设定月最后一天的前几天的偏移量,例如”L-3”表示一个月中倒数第三天。L这个选项,不依赖一个特定的值,但是会得到一个令人捉摸不定的值。 The W character is allowed for the day-of-month field. This character is used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify 15W as the value for the day-of-month field, the meaning is: the nearest weekday to the 15th of the month. So if the 15th is a Saturday, the trigger will fire on Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. However if you specify 1W as the value for day-of-month, and the 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not jump over the boundary of a months days. The W character can only be specified when the day-of-month is a single day, not a range or list of days.The L and W characters can also be combined for the day-of-month expression to yield LW, which translates to last weekday of the month.翻译:字符W只能用在日字段中。用这个字符可以用来确定与给定日期最接近的工作日(周一到周五)。 例如,在日字段中15W是指:和15号最近的哪个工作日。如果15号是星期六,那么触发器将会在14号星期五执行,如果15号市星期日,那么触发器将会在16号星期一执行。如果15号市星期二,那边触发器就在该天15号星期二执行。如果指定1W,即使1号是星期六,触发器会在3号星期一执行,不会跨月执行。W只会表示一个月中的某一天,不能表示多天或连续几天。 L和W可以组合在一起成LW,表示一个月中最后一个工作日。 The # character is allowed for the day-of-week field. This character is used to specify the nth XXX day of the month. For example, the value of 6#3 in the day-of-week field means the third Friday of the month (day 6 = Friday and #3 = the 3rd one in the month). Other examples: 2#1 = the first Monday of the month and 4#5 = the fifth Wednesday of the month. Note that if you specify #5 and there is not 5 of the given day-of-week in the month, then no firing will occur that month. If the # character is used, there can only be one expression in the day-of-week field (3#1,6#3 is not valid, since there are two expressions).The legal characters and the names of months and days of the week are not case sensitive.翻译:#允许用在周字段中,用来表示一个月中的第几星期几。例如6#3表示一个月中的第三个星期五(day 6 = Friday and #3 表示第三)。2#1= 一个月中第一个星期一。4#5= 一个月中第五个星期三。 需要注意的是,如果使用了”#5”,但是如果一个月中没有给定的第五个星期几,那边就不会执行。如果使用了#,那么在周字段中只能使用这一个这样的表达式(“3#1,6#3”是非法的,因为使用了2个)。NOTES: Support for specifying both a day-of-week and a day-of-month value is not complete (youll need to use the ? character in one of these fields). Overflowing ranges is supported - that is, having a larger number on the left hand side than the right. You might do 22-2 to catch 10 oclock at night until 2 oclock in the morning, or you might have NOV-FEB. It is very important to note that overuse of overflowing ranges creates ranges that dont make sense and no effort has been made to determine which interpretation CronExpression chooses. An example would be 0 0 14-6 ? * FRI-MON.特殊字符Cron 触发器利用一系列特殊字符,如下所示: 反斜线(/)字符表示增量值。例如,在秒字段中“5/15”代表从第 5 秒开始,每 15 秒一次。 问号(?)字符和字母 L 字符只有在月内日期和周内日期字段中可用。问号表示这个字段不包含具体值。所以,如果指定月内日期,可以在周内日期字段中插入“?”,表示周内日期值无关紧要。字母 L 字符是 last 的缩写。放在月内日期字段中,表示安排在当月最后一天执行。在周内日期字段中,如果“L”单独存在,就等于“7”,否则代表当月内周内日期的最后一个实例。所以“0L”表示安排在当月的最后一个星期日执行。 在月内日期字段中的字母(W)字符把执行安排在最靠近指定值的工作日。把“1W”放在月内日期字段中,表示把执行安排在当月的第一个工作日内。 井号(#)字符为给定月份指定具体的工作日实例。把“MON#2”放在周内日期字段中,表示把任务安排在当月的第二个星期一。星号(*)字符是通配字符,表示该字段可以接受任何可能的值。1.1.2 示例说明2. 参数传递1.4. 通过job.getJobDataMap.put(),放置参数信息1.5. 通过context.getJobDetail().getJobDataMap()取参数的值3. 异常处理需要抛出异常JobExecutionException ,异常处理可以设定2个状态1.2. 设定setRefireImmediately JobExecutionException e2 = new JobExecutionException(e); / fix denominator so the next time this job run / it wont fail again dataMap.put(denominator, 1); / this job will refire immediately e2.setRefireImmediately(true); throw e2;1.3. 设定setUnscheduleAllTriggers JobExecutionException e2 = new JobExecutionException(e); / Quartz will automatically unschedule / all triggers associated with this job / so that it does not run again e2.setUnscheduleAllTriggers(true); throw e2;4. 节假日处理1.4. 声明节假日 / Add the holiday calendar to the schedule AnnualCalendar holidays = new AnnualCalendar(); / 定义节假日 / fourth of July (July 4) Calendar fourthOfJuly = new GregorianCalendar(2005, 6, 4); holidays.setDayExcluded(fourthOfJuly, true); / halloween (Oct 31) Calendar halloween = new GregorianCalendar(2005, 9, 31); holidays.setDayExcluded(halloween, true); / christmas (Dec 25) Calendar christmas = new GregorianCalendar(2005, 11, 25); holidays.setDayExcluded(christmas, true); / tell the schedule about our holiday calendar sched.addCalendar(holidays, holidays, false, false);注:1、GregorianCalendar参数 0 代表1月,1代表2月,11代表12月1.5. 使用节假日属性 SimpleTrigger trigger = newTrigger() .withIdentity(trigger1, group1) .startAt(runDate) .withSchedule(simpleSchedule() .withIntervalInHours(1) .repeatForever() .modifiedByCalendar(holidays) .build();5. Job监听器1.6. 实现接口JobListener实现接口中的方法l jobToBeExecutedjob调用前执行l jobExecutionVetoedl jobWasExecutedjob执行后执行1.7. 为job注册接口 / Set up the listener JobListener listener = new Job1Listener(); Matcher matcher = KeyMatcher.keyEquals(job.getKey(); sched.getListenerManager().addJobListener(listener, matcher); 6. DateBuilderDateBuilder is used to conveniently create java.util.Date instances that meet particular criteria.DateBuilder 用来创建java.utilDate实例。1.8. evenMinuteDate该函数返回最近的整点分钟时间。 System.out.println(系统时间: + (new Date(); / computer a time that is on the next round minute Date runTime = DateBuilder.evenMinuteDate(new Date(); System.out.println(runTime: + runTime);-运行结果日志系统时间:Tue Mar 10 08:46:52 CST 2015runTime:Tue Mar 10 08:47:00 CST 20151.9. nextGivenSecondDate1.1.3 文档说明publi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论