`
liudaoru
  • 浏览: 1573915 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring--简单使用quartz实现定时作业[z]

    博客分类:
  • java
阅读更多

From: www.blogjava.net/javaora/archive/2005/07/20/8070.aspx

定时批处理作业是J2EE企业应用里很重要的一环,用来在晚间进行财务挂账,数据转存,新闻联播等等操作。

    而在Spring里,已经很好的集成了Quartz,简单到像配cron一样,在xml文件里面配一下时间就可以自动执行,不需要写一行代码。Spring对Quartz大刀阔斧的简化堪称范例,Quartz项目组也许可以学习一下。

      
xml 代码
  1. <bean id="methodInvokingJobDetail"  
  2.        class="org.springframework.scheduling.
  3.                   quartz.MethodInvokingJobDetailFactoryBean">        
  4. <property name="targetObject"><ref bean="financeDAO"/>property>        
  5. <property name="targetMethod"><value>confirmOrdervalue>property>    
  6. bean>  
  7. <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  8.         <property name="jobDetail">
  9.             <ref bean="methodInvokingJobDetail"/>
  10.         property>
  11.         <property name="cronExpression">
  12.             <value>0 0 6,12,20 * * ?value>
  13.         property>
  14. bean>  
  15. <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  16.         <property name="triggers">
  17.             <list><ref local="cronTrigger"/>list>
  18.         property>
  19. bean>  
   

上面这段配置文件规定了在早上6点和晚上8点执行financeDAO对象的confirmOrder()方法.


附:cronExpression配置说明

字段   允许值   允许的特殊字符
  0-59   , - * /
  0-59   , - * /
小时   0-23   , - * /
日期   1-31   , - * ? / L W C
月份   1-12 或者 JAN-DEC   , - * /
星期   1-7 或者 SUN-SAT   , - * ? / L C #
年(可选)   留空, 1970-2099   , - * /

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 fileds, but not the other. See the examples below for clarification.
“?”字符只在日期域和星期域中使用。它被用来指定“非明确的值”。当你需要通过在这两个域中的一个来指定一些东西的时候,它是有用的。看下面的例子你就会明白。
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点”。
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.
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". When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results.

 

分享到:
评论
1 楼 liudaoru 2008-04-01  
Class CronTrigger
java.lang.Object
|
+--org.quartz.Trigger
|
+--org.quartz.CronTrigger
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable, java.io.Serializable

--------------------------------------------------------------------------------

public class CronTrigger
extends Trigger

A concrete Trigger that is used to fire a JobDetail at given moments in time, defined with Unix 'cron-like' definitions.

For those unfamiliar with "cron", this means being able to create a firing schedule such as: "At 8:00am every Monday through Friday" or "At 1:30am every last Friday of the month".

A "Cron-Expression" is a string comprised of 6 or 7 fields separated by white space. The 6 mandatory and 1 optional fields are as follows:
Field Name Allowed Values Allowed Special Characters
Seconds 0-59 , - * /
Minutes 0-59 , - * /
Hours 0-23 , - * /
Day-of-month 1-31 , - * ? / L W C
Month 1-12 or JAN-DEC , - * /
Day-of-Week 1-7 or SUN-SAT , - * ? / L C #
Year (Optional) empty, 1970-2099 , - * /


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 fileds, but not the other. See the examples below for clarification.

The '-' character is used to specify ranges For example "10-12" in the hour field means "the hours 10, 11 and 12".

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".

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". You can also specify '/' after the '*' character - in this case '*' is equivalent to having '0' before the '/'.

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". When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results.

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 month's 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".

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.

The 'C' character is allowed for the day-of-month and day-of-week fields. This character is short-hand for "calendar". This means values are calculated against the associated calendar, if any. If no calendar is associated, then it is equivalent to having an all-inclusive calendar. A value of "5C" in the day-of-month field means "the first day included by the calendar on or after the 5th". A value of "1C" in the day-of-week field means "the first day included by the calendar on or after sunday".

The legal characters and the names of months and days of the week are not case sensitive.

Here are some full examples:
Expression Meaning
"0 0 12 * * ?" Fire at 12pm (noon) every day
"0 15 10 ? * *" Fire at 10:15am every day
"0 15 10 * * ?" Fire at 10:15am every day
"0 15 10 * * ? *" Fire at 10:15am every day
"0 15 10 * * ? 2005" Fire at 10:15am every day during the year 2005
"0 * 14 * * ?" Fire every minute starting at 2pm and ending at 2:59pm, every day
"0 0/5 14 * * ?" Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day
"0 0/5 14,18 * * ?" Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day
"0 0-5 14 * * ?" Fire every minute starting at 2pm and ending at 2:05pm, every day
"0 10,44 14 ? 3 WED" Fire at 2:10pm and at 2:44pm every Wednesday in the month of March. "0 15 10 ? * MON-FRI" Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
"0 15 10 15 * ?" Fire at 10:15am on the 15th day of every month
"0 15 10 L * ?" Fire at 10:15am on the last day of every month
"0 15 10 ? * 6L" Fire at 10:15am on the last Friday of every month
"0 15 10 ? * 6L" Fire at 10:15am on the last Friday of every month
"0 15 10 ? * 6L 2002-2005" Fire at 10:15am on every last friday of every month during the years 2002, 2003, 2004 and 2005
"0 15 10 ? * 6#3" Fire at 10:15am on the third Friday of every month


Pay attention to the effects of '?' and '*' in the day-of-week and day-of-month fields!

NOTES:

Support for the features described for the 'C' character is not complete.
Support for specifying both a day-of-week and a day-of-month value is not complete (you'll need to use the '?' character in on of these fields).
Be careful when setting fire times between mid-night and 1:00 AM - "daylight savings" can cause a skip or a repeat depending on whether the time moves back or jumps forward.


Author:
Sharada Jambula, James House, Contributions from Mads Henderson
See Also:
Trigger, SimpleTrigger, Serialized Form

相关推荐

    quartz-jar.7z

    总结来说,Quartz是一个功能强大的定时任务管理工具,适用于各种Java应用程序,尤其适合与Spring框架结合使用,以实现灵活、稳定且可扩展的定时任务调度解决方案。其丰富的特性和API,使得开发者可以轻松地处理复杂...

    Spring Boot 系列实战合集.7z

    14. **定时任务(Task Scheduling)**:可以使用 Spring 的 @Scheduled 注解来实现定时任务,或者集成 Quartz 进行更复杂的调度。 15. **邮件服务**:通过配置和使用 JavaMailSender 接口,可以方便地发送电子邮件...

    quart-z 两次执行问题

    然而,由于`webapps`目录下的所有应用程序默认都会被加载,这就导致了应用程序被加载了两次,进而使得Spring配置也被加载了两次,最终导致Quart-Z定时任务执行了两次。 #### 三、解决方案 为了解决这个问题,可以...

    java各种job

    本资源包涵盖了Java基础的Job处理、Quartz框架、Spring Scheduling以及Spring与Quartz结合使用时的数据库配置,这些都是Java开发者在实现定时任务时可能会用到的关键技术。 1. **Java基础Job**: 在Java中,我们...

    我的博客实例

    WebQuartz.7z则可能包含了Quartz作业调度的相关配置和类,用于设置和管理定时任务。 综合以上信息,这个博客实例项目展示了一个基于Mybatis和SpringMVC的Web应用开发实例,其中结合了Quartz来实现定时任务。对于...

    Java任务调度 jconch.7z

    Java提供了多种实现任务调度的方法,其中最常用的是Quartz和Spring的TaskExecution和TaskScheduling模块。 Quartz是一个开源的作业调度框架,它可以用来创建、调度和执行计划任务。Quartz允许开发者精确地定义任务...

    毕业论文题目:基于SpringBoot的电商系统的设计与开发.7z

    5. **任务调度**:使用Spring Task或者Quartz实现后台任务调度,如定时促销、订单处理等。 6. **分布式缓存**:集成Redis或Memcached,提高数据访问速度,减轻数据库压力。 7. **消息队列**:结合RabbitMQ或Kafka...

    然之协同办公系统 4.8.7z

    这可能涉及到Quartz、Spring Task等任务调度库的使用。 6. **API设计**:系统可能提供RESTful API,方便与其他系统集成。通过源码,我们可以学习API的设计原则和最佳实践。 7. **模块化与插件化**:为了提高系统的...

    四川九寨运业车辆综合管理系统.7z

    8. **后台服务器**:系统可能基于Java或.NET搭建后端服务,使用Spring Boot或ASP.NET Core框架,实现业务逻辑处理和数据访问。 9. **通信协议**:系统间通信可能采用RESTful API或SOAP协议,保证数据传输的安全性和...

    物流配货网javaweb源码.zip

    8. **调度任务**:Quartz或Spring Task可能用于实现定期的任务调度,比如定时更新货物状态、发送提醒通知等。 9. **API接口**:如果系统需要与其他系统集成,可能涉及RESTful API的设计和实现,通过HTTP协议交换...

    淘淘商城01

    MyBatis作为DAO层,Druid作为数据库连接池,Redis作为缓存,Solr作为搜索引擎,FastDFS作为分布式文件系统,Nginx作为反向代理服务器,Quartz作为定时任务框架,Tomcat作为Web服务器,使用Maven进行项目管理。...

Global site tag (gtag.js) - Google Analytics