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。

JSP中(servlet)如何使用定时作业Quartz框架

以下介绍的是在JSP或者servlet中使用定时作业Quartz框架,这篇文章是我大学第二次实习时写的,当时带我的师傅维护一个旧业务系统时需要定时作业处理,因为我之前大三实习的时候接触过,知道有这么个...
  • chenleixing
  • chenleixing
  • 2015年01月30日 23:42
  • 5209

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

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

Spring quartz定时任务Service注入问题

转载地址:http://www.cnblogs.com/dupang/p/6063734.html今天想单元测试一下spring中的quartz定时任务,job类的大致结构和下面的SpringQtz1...
  • u012385190
  • u012385190
  • 2016年11月25日 10:16
  • 3606

使用xml配置Quartz,实现定时任务

使用xml配置Quartz,实现定时任务使用xml配置前景以前我是使用java代码配置Quartz任务的,到最好你会发现,这个类使用其他类成员变量会无法自动注入问题 始终是空指针问题,到最后只能手动...
  • JarremDon
  • JarremDon
  • 2017年12月12日 11:00
  • 68

Spring与Quartz集成配置实现简单定时任务调度

可以说是最好的一篇定时任务的介绍 来自:http://www.zuidaima.com/share/2736623139458048.htm 一.代码结构图 二.核心类 J...
  • u013380777
  • u013380777
  • 2017年01月09日 12:33
  • 329

java中如何用Quartz框架来实现定时任务(一)

Quartz介绍: Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单...
  • u013310119
  • u013310119
  • 2017年04月01日 10:01
  • 5698

6. springboot整合quartz实现定时任务

转载:http://www.cnblogs.com/lic309/p/4089633.html 在做项目时有时候会有定时器任务的功能,比如某某时间应该做什么,多少秒应该怎么样...
  • LINABC123000
  • LINABC123000
  • 2017年04月04日 11:55
  • 487

Quartz定时任务简单demo及与Spring整合

本篇博文主要是讲述2.x 版本的quartz下的实现方案,1.x 版本的实现方式大致原理一致,但是具体的实现方式有些不一致,具体体现在获取  scheduler 这个类的方式上有些不同,这里不作过多的...
  • w405722907
  • w405722907
  • 2017年05月18日 10:06
  • 843

ssh中如何实现定时任务(spring对quartz的支持)

实现定时任务:quartz spring中对quartz进行了封装,使得我们可以直接在配置文件中配置bean就可以使用quartz的定时任务 首先在spring中使用quartz需要引入的包: ...
  • qq_18675693
  • qq_18675693
  • 2015年12月27日 20:34
  • 1951

spring3整合quartz2,实现动态添加、修改、暂停、重启定时任务

一、Quartz简介  Quartz大致可分为三个主要的核心:     1、调度器Scheduler:是一个计划调度器容器,容器里面可以盛放众多的JobDetail和Trigger,当容器...
  • liuxiao723846
  • liuxiao723846
  • 2015年07月14日 16:10
  • 2392
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Spring--简单使用quartz实现定时作业
举报原因:
原因补充:

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