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

转载 2006年05月19日 11:23:00
定时批处理作业是J2EE企业应用里很重要的一环,用来在晚间进行财务挂账,数据转存,新闻联播等等操作。

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

    <bean id="methodInvokingJobDetail"
        class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject"><ref bean="financeDAO"/></property>
        <property name="targetMethod"><value>confirmOrder</value></property>
    </bean>

    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="methodInvokingJobDetail"/>
        </property>
        <property name="cronExpression">
            <value>0 0 6,12,20 * * ?</value>
        </property>
    </bean>
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list><ref local="cronTrigger"/></list>
        </property>
    </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.
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".

http://blog.csdn.net/tutugogo/archive/2006/05/18/744416.aspx

 

What is Quartz? (什么是Quartz?)

What is Quartz? (什么是Quartz?)
Quartz is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application - from the smallest stand-alone application to the largest e-commerce system. Quartz can be used to create simple or complex schedules for executing tens, hundreds, or even tens-of-thousands of jobs; jobs whose tasks are defined as standard Java components or EJBs. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering.

Quartz 是一个功能齐全的、开源的job时间调度系统,它可以被结合的,或者伴随虚拟的任何J2EE或J2SE程序-从最小的独立的应用程序到最大的e-commerce系统被使用。Quartz可以用来创建简单或复杂的时间调度来执行十、百、千、甚至上万的jobs;获得的job被定义为一个标准的java组件或EJBs. 这个Quartz 时间调度包含很多企业类的特征,例如JTA事务和簇。

The licensing of Quartz versions 1.0 through 1.4.5 is similar to both the BSD and ASF (Apache) public licenses, which means it's free for use, even within commercial products.

Quartz 1.0版本到1.4.5版本的licensing 是类似与BSD和ASF(apache)公共licenses, 它意味着可以免费使用,甚至使用在商业产品中使用。
With the upcoming release of version 1.5.0, Quartz is moving to the Apache 2.0 license.
伴随1.5.0版本的发布,Quartz将转移向apache 2.0 的license.

What can Quartz do for you? (Quartz可以为你做什么?)
If your application has tasks that need to occur at given moments in time, or if your system has recurring maintenance jobs then Quartz may be your ideal solution.

如果你的程序有一些这样的任务,它需要及时地发生在给定时间,或者你如果你的系统有连续维护jobs,那么Quartz可以成为你的理想的解决方案。

Sample uses of job scheduling with Quartz:
Quartz使用job时间调度的范例

Driving Workflow: As a new order is initially placed, schedule a Job to fire in exactly 2 hours, that will check the status of that order, and trigger a warning notification if an order confirmation message has not yet been received for the order, as well as changing the order's status to 'awaiting intervention'.
System Maintenance: Schedule a job to dump the contents of a database into an XML file every business day (all weekdays except holidays) at 11:30 PM.

工作流驱动:作为一个新的订单被初始化放置,调度一个job去工作在正好两个小时内,它将检查订单的状态,并且触发一个警告通知如果订单确认信息没有被接收,又改变订单的状态到"等待干涉"。
系统维护:调度一个job来将数据库转化为XML文件 每商业日期(所有周末除了节假日)在下午11:30。

相关文章推荐

Spring集成Quartz定时器实现定时作业任务

这篇文章有点久远了,记得是第一次出来实习时,大三暑假自己找了一家软件公司实习了很长时间,当时学到很多实践性的东西,这个Quartz就是其中一个,还记得是做OA,一些消息需要定时提醒定时删除,项目框架里...

使用quartz+spring实现简单的可配置化的定时任务(可和数据库配套使用)

首先定义一个job要实现Job接口,以后各种各样的任务都放在这个job中执行: import org.quartz.Job; import org.quartz.JobDataMap; import...
  • IOpfan
  • IOpfan
  • 2016年06月07日 11:28
  • 342

Web项目中使用Spring 3.x + Quartz 2.x实现作业调度详解

Quartz是一个基于Java的作业调度管理的轻量级框架,目前在很多企业应用中被使用,它的作用类似于java.util中的Timer和TimeTask、数据库中的job等,但Quartz的功能更强大、...

Spring Quartz定时作业

  • 2011年11月30日 15:02
  • 2.86MB
  • 下载

spring整合Quartz实现简单的定时功能

Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中。它提供了巨大的灵活性而不牺牲简单性。你能够用它来为执行一个作业而创建简单的或复杂的调度。本...

Quartz + Spring 简单实现定时任务

Quartz + Spring 简单实现定时任务 Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独...

Spring+Quartz实现定时任务的简单方法

1.导入相关的包 quartz-1.6.5.jar 等 2.quartx-config.xml配置

Java作业调度/定时任务 Spring和Quartz整合

Quartz是一个完全由java编写的开源作业调度框架,是OpenSymphony开源组织在Job scheduling领域的开源项目,它可以与JEE应用程序相结合也可以单独使用。Quartz可以用来...

spring mvc + quartz(定时任务调度作业)

第一步:引入jar包 quartz-all-1.7.3.jar(在已经搭建好spring mvc 框架的基础上) 第二步:创建applicationContext-job.xml 定时任务文件 ...

定时作业框架Quartz.net的简单应用

在许多应用场景下我们都希望定时执行一些事务,比较常见的是维护数据库:定时生成汇总数据,定时复制插入数据等。其他特定场景下都存在这些定时任务。如果涉及数据库维护的定时任务,我们尚可以使用数据库自带的数据...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Spring--简单使用quartz实现定时作业
举报原因:
原因补充:

(最多只允许输入30个字)